Project 8
Due by 3pm (end of office hours) Friday Dec 13
Introduction
Your goal is to creat a rpn (reverse polish notation) calculator that will work with floating point numbers, and supports the following operations{+, -, *, /, AVG, SUM, PROD}. Additionaly your calculator will read from stdin until end of file is reached (ctrl z or ctrl d depending on the machine}. This project should act as a bit of a review of things since the start of the term.
Details
You are welcome to create this project entirely from scratch. However, the book provides 2 examples of rpn calculators that can be used as a starting point, and I have posted a modified version of my own that you are welcome to use as well. See the hints section below for links
All entries will be proper rpn notation for example:
3 5 +
should return 8
3.0 5.0 + 10 0.5 * *
should return 40
3 7 / 5 + 4 5 SUM
Any white space can a be delimiter.
- AVG is just the average
For example 2 2 2 2 AVG is 2
1 1 + 2 2 2 2 AVG is 2
1 2 3 4 5 AVG is 3
- SUM sums all previous entires
For example 2 2 2 2 SUM is 8
1 1 * 2 2 2 2 SUM is 9
1 2 3 4 0 SUM is 10
- PROD is like sum but uses multiplication rather than addition.
For example 2 2 2 2 PROD is 16
1 1 + 2 2 2 2 PROD is 32
1 2 3 4 0 PROD is 0
Hints
if you use
char *string;
double data;
scanf("%s", string);
if ( isdigit(string[0]) || string[0] = '.' ) {
/*I have read a number */
data = atof(s); /*converts the string in s to a double*/
}
My rpn starter set, you will need all 3 files and compile with
gcc stack.c main.c
main.c, polish.h, stack.c
You may want to look at
The standard C libraries in particular the ctype.h listings and the stdlib.h listings. Your book covers the same material but this is a bit more complete.
To keep the testing automate testing create a number of files with a single rpn calculation in each say {test1.txt, test2.txt, ... } and use cat test1.txt | a.out to test your program.
The authors version of an rpn calculator are:
Version 1, Version 2
This page was last modified on .
to the Department of Computer Science