A Graphing Calculator

View as PDF

Submit solution

Points: 17 (partial)
Time limit: 2.0s
Memory limit: 64M

Author:
Problem type

Graphing calculators are devices capable of plotting graphs, given a user-defined function. Given a function f(x), the view window (domain and range), and an x-increment (described later), replicate the functionality of a graphing calculator by plotting the graph in the output.

For every coordinate in the domain and range, graph layout will consist as follows:

  • Empty space will be represented by a period (.)
  • The origin (0, 0) will be represented as a +
  • X and Y axis lines will be represented by - and |, respectively
  • The actual points where the graph exists will be represented by a #

For example, the function f(x) = x + 3 would look like:

.....|...#.
.....|..#..
.....|.#...
.....|#....
.....#.....
....#|.....
...#.|.....
--#--+-----
.#...|.....
#....|.....

To get full points, your program must also allow for x-increment specification. An x-increment defines the difference between every value of x in f(x), essentially signifying the precision of the output of the graphing program. For example, with an x-increment of 1, your program should only find the value of f(x) for every integer x, such as -1, 0, 1, and so on. Plotting f(x) = x^2 with an x-increment of 1 would produce such result:

..........|..........
.......#..|..#.......
..........|..........
..........|..........
..........|..........
..........|..........
........#.|.#........
..........|..........
..........|..........
.........#|#.........
----------#----------
..........|..........
..........|..........

where only the rounded result of f(x) for every integer x is displayed.

However, with an x-increment of 0.01, f(x) = x^2 really begins to take shape:

.......#..|..#.......
.......#..|..#.......
.......#..|..#.......
.......#..|..#.......
.......##.|.##.......
........#.|.#........
........#.|.#........
........#.|.#........
........##|##........
.........#|#.........
---------###---------
..........|..........
..........|..........

Note that you must round your output.

Input Specification

The first line will contain a string representing f(x). The string is guaranteed to only have the following possibly space-separated characters: x, +, -, *, /, ^, (, ), ., and the digits from 0-9. Note that multiplication will be represented with the *, so 2x (instead of 2 * x) will not be valid input. However, placing a negative sign before a number is valid and should be accounted for (e.g. -x).

The next line of input will contain the integers L, R, B, T, and the number I. The first four (L, R, B, T) represent the domain and range of the graph, or the left, right, bottom, and top values of the viewing window, where L \le x \le R and B \le f(x) \le T, and 0 \le R-L, T-B \le 100. I (0.01 \le I \le 1) specifies the x-interval of f(x). For 75% of the points, I = 1.

Keep in mind that you must obey proper operation order. For 25% of the points, the input will not contain any brackets.

Output Specification

Graph f(x) using the appropriate view port.

Sample Input 1

x - 5
-10 10 -10 10 1

Sample Output 1

..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|.........#
..........|........#.
..........|.......#..
..........|......#...
..........|.....#....
----------+----#-----
..........|...#......
..........|..#.......
..........|.#........
..........|#.........
..........#..........
.........#|..........
........#.|..........
.......#..|..........
......#...|..........
.....#....|..........

Sample Input 2

(x - 5) * (x + 1)
-20 20 -15 15 1

Sample Output 2

....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
..................#.|.....#..............
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
-------------------#+----#---------------
....................|....................
....................|....................
....................|....................
....................|....................
....................#...#................
....................|....................
....................|....................
....................|#.#.................
....................|.#..................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................
....................|....................

Sample Input 3

(x-5)*(x-3)/(x-5)
-10 10 -10 10 0.01

Sample Output 3

..........|..........
..........|..........
..........|..........
..........|.........#
..........|........#.
..........|.......#..
..........|......#...
..........|.....#....
..........|....#.....
..........|...#......
----------+--#-------
..........|.#........
..........|#.........
..........#..........
.........#|..........
........#.|..........
.......#..|..........
......#...|..........
.....#....|..........
....#.....|..........
...#......|..........

Sample Input 4

x^3/100
-10 10 -10 10 0.01

Sample Output 4

..........|.........#
..........|........##
..........|........#.
..........|........#.
..........|.......##.
..........|.......#..
..........|......##..
..........|.....##...
..........|....##....
..........|...##.....
------#########------
.....##...|..........
....##....|..........
...##.....|..........
..##......|..........
..#.......|..........
.##.......|..........
.#........|..........
.#........|..........
##........|..........
#.........|..........

Comments

There are no comments at the moment.