/* rpn-calc.c  --  Reverse Polish Notation calculator (postfix) */

#include <stdio.h>
#include "stack.h"
#include "getch.h"
#include "getop.h"
#include "str2double.h"

#define EXPR_STR_SZ  80

int main(int argc, char *argv[])
{
	char s[EXPR_STR_SZ];
 	int type;
	double op2;

	while((type = getop(s)) != EOF) {
		switch(type) {
			case NUM:
				push(str2double(s));
			break;

			case '+':
				push(pop() + pop());
			break;

			case '*':
				push(pop() * pop());
			break;

			case '-':
				op2 = pop();
				push(pop() - op2);
			break;

			case '/':
				op2 = pop();
				if(op2 != 0.0) {
					push(pop() / op2);
				} else {
					printf("Division by zero!\n");
				}
			break;

			case '\n':
				printf("\t [%.4lf]\n", pop());
			break;

			default:
				printf("Strange type=%d !!\n", type);
			break;
		}
	}
	
	return(0);
}

