

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


The CEO of the Computer Sciences Corporation, Dr. Link Queue has decided that the first product in the company’s arsenal of applications will be a text-based numerical calculator. In this assignment, you will develop a simple calculator that takes mathematical expressions from the user and outputs the results. Your calculator will utilize stacks and postfix notation to perform the computations.
Users of your calculator should be able to enter a mathematical numerical expression as input and the program should print out the result as output. You can do this in one of two ways. The first way is to make the calculator a command line program. Thus, the expression would be passed to the program when the executable is run. For example, if your program is named ‘calc’, it might look something like this:
calc 2+5+ Result: 14
The second way is to make the calculator more interactive. You would run the ‘calc’ program and see something like this:
calc
Welcome to the Calculator (type ‘Q’ to quit)
Enter Expression: 2+ Answer: 4
Enter Expression 5* Answer: 40
Enter Expression: Q
You have the option of choosing either method.
As for expressions, we will assume that all expressions are mathematically valid. Thus, you will not have to worry if someone writes an expression such as ‘2+5(5’. You can do error checking of valid expressions for extra credit.
Your calculator must support addition, subtraction, multiplication, and division as well as parentheses. Your operands are limited to the integers, but the output of your calculator may be a real number.
Extra Credit If you finish these requirements, for extra credit you can add the following
There are basically 4 parts to this assignment.
In part one, you will need to create a stack library. You should use the linked list routines from your last assignment to build the library. The structure you should use is
struct stackNode { char token[10]; struct stackNode *next; };
You will need to implement the push, pop, isEmpty, and returnTop functions we discussed in class.
In part two, you will need to be able to read in an expression for further processing. You can use a character array so you can examine individual symbols.
In part three, you will have to write a function that takes the expression and converts it to postfix notation. You should use the pseudocode we discussed in class as a template for your implementation, making use of the functions you wrote in part one. Also, make sure you print out the postfix expression for testing purposes.
Finally, in part four, you will have to write an evaluator function that takes the postfix expression as input and calculates its value. Use the stack library and the procedure we discussed in class for your implementation. The functions found in the header file, “ctype.h” may be useful for determining whether a character is a digit, punctuation, etc…
You should end up having to create 5 new files, stack.h, stack.c, calculator.h, calculator.c, and main.c.