pls help me make this program into a simpler, COMPLETE stack class program(with file.h,file.cpp and tester.cpp).
here's the program
#include <ctype.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "stack1.h"
double read_and_evaluate(istream& ins);
void evaluate_stack_tops(Stack<double>& numbers, Stack<char>& operations);
int main()
{
double answer;
cout<<"type a fully parenthesized arithmetic expression:"<<endl;
answer=read_and_evaluate(cin);
cout<<"that evaluates to "<<answer<<endl;
return EXIT_SUCCESS;
}
double read_and_evaluate(istream& ins)
{
const char DECIMAL='.';
const char DIGHT_PARENTHESIS=')';
Stack<double> numbers;
Stack<char> operations;
double numbers;
char symbols;
while(ins && ins.peek()!='\n')
{
if(isdigit(ins.peek())||(ins.peek()==DECIMAL))
{
ins>>number;
numbers.push(number);
}
else if(strchr("+-*/",ins.peek())!=NULL)
{
ins>>symbol;
operations.push(symbol);
}
else if(ins.peek()==RIGHT_PARENTHESIS)
{
ins.ignore();
evaluate_stack_tops(numbers,operations);
}
else
ins.ignore();
}
return numbers.pop();
}
void evaluate_stack_tops(Stack<double>& numbers, Stack<char>& operations)
{
double operand1,operand2;
operand2=numbers.pop();
operand1=numbers.pop();
switch(operations.pop())
{
case'+':numbers.push(operand1+operand2);
break;
case'-':numbers.push(operand1-operand2);
break;
case'*':numbers.push(operand1*operand2);
break;
case'/':numbers.push(operand1/operand2);
break;
}
}
all helps would be highly appreciated..
Last edited on