// APCS
// Stacks & Queues
//
#include <iostream.h>
#include "apstring.h"
#include "apqueue.h"
#include "apstack.h"
#include <ctype.h>
#include <math.h>

int stack_priority(char ch);
int infix_priority(char ch);
double evaluate (double v1, double v2, char op);

int main()
{
int quit, j=0;
float var;
apstring infix, postfix;
apstack <char> operators;
apstack <char> tempstack;
apstack <double> value;
apqueue <char> postfix;
char item, v1, v2, result;

// begin loop
do
{
// get expression
cout << "Enter the expression and end with =\n"
<< "For example : A + B - C = \n";
cin.ignore(20, \n);
getline(cin, infix);
operators.push('=');
// convert from infix to postfix by
// using the two priority functions to
// determine when to add operators to the stack
// and when to add them to the postfix queue
// Bonus: the expression is entered as a generic
// equation so that real numbers can be used for
// evaluation
while (infix[j] != '=')
{
if((infix[j] <='Z') && (infix[j] >='A'))
postfix.enqueue(infix[j]);
else if(infix[j] == ')')
{
if(!operators.isEmpty())
operators.pop(item);
while(item != '(')
{
postfix.enqueue(item);
operators.pop(item);
}
}
else if (ch == '=')
{
while(!operators.isEmpty())
{
operators.pop(item);
postfix.enqueue(item);
}
}
else
{
if(!operators.isEmpty())
operators.pop(item);
while(stack_priority(item) >= infix_priority(infix[j]))
{
postfix.enqueue(item);
operators.pop(item);
}
operators.push(item);
operators.push(infix[j]);
}
j++;
}
// print out the postfix expression by using a
// temporary stack and popping
tempstack = operators;
while(!tempstack.isEmpty())
{
tempstack.pop(item);
postfix += item + ' ';
}
cout << postfix << endl;
// evaluate postfix by allowing the user
// to enter any real values for the variables
while (postfix[j] != '=')
{
if((postfix[j] <= 'Z') && (postfix[j] >'A'))
{
cout << "Enter any real number for the variable"
<< postfix[j] << endl;
cin >> var;
value.push(var);
}
else
{
value.pop(v2);
value.pop(v1);
result = evaluate(v1, v2, postfix[j]);
value.push(result);
}
j++;
}
tempstack.pop(result);
cout << "The answer is " << result;
// end
cout <<"Quit? 0 to quit.\n";
cin >> quit;
}
while(quit !=0);
return quit;
}

int infix_priority(char ch)
{
switch(ch)
{
case '*': return 2;
case '/': return 2;
case '+': return 4;
case '-': return 4;
case '(': return 5;
case ')': return 0;
case '^': return 4; // according to the text
case '%': return 4; // same as division
case '=': return 0;
}
}

int stack_priority(char ch)
{
switch(ch)
{
case '*': return 1;
case '/': return 1;
case '+': return 3;
case '-': return 3;
case '(': return 0;
case ')': return -1; // -1 indicates undefined!
case '^': return 3; // from text
case '%': return 3; // division
case '=': return 0;
}
}

double evaluate (double v1, double v2, char op)
{
// assuming that the expression is valid
switch(op)
case '*': return v1*v2;
case '/':
if(int(v1)==v1) && (int(v2)==v2))
return v1%v2;
else
return v1/v2;
case '+': return v1+v2;
case '-': return v1-v2;
case '^': return pow(v1, v2);
case '%':
if(int(v1)==v1) && (int(v2) == v2))
return v1%v2;
}

Hosted by www.Geocities.ws

1