I am trying to make a postfix evaluation and i can't solve the problem about my program. Thanks ahead. here is the C++ Code.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
string postfix;
int Top, capacity, a;
char token;
void construct ();
bool empty ();
void push (char item);
int top ();
char pop ();
void main ()
{
bool quit=false;
int select;
double x, y, z;
welcome:
system ("cls");
cout<<"\t\t\tW E L C O M E !\n\n";
cout<<"This program will take a Postfix Expression\nEvaluate it and then Display the result.\n\n";
selection:
do
{
cout<<"Make a selection:\n";
cout<<"1. Evaluate a Postfix Expression.\n";
cout<<"2. Quit.\n";
cin>>select;
if (select==1)
{
system ("cls");
cout<<"Enter a Postfix Expression:\n";
cin>>postfix;
construct ();
for (a=0; a<postfix.length(); a++)
{
token=postfix[a];
if (isdigit(token))
{
cout<<"\n"<<token<<" will be stored to myStack "<<a<<"\n";
push (token);
}
else if (token=='+')
{
x=top ();
pop ();
y=top ();
pop ();
cout<<"Evaluate "<<y<<" + "<<x;
z=y+x;
push (z);
}
else if (token=='-')
{
x=top ();
pop ();
y=top ();
pop ();
cout<<"Evaluate "<<y<<" - "<<x;
z=y-x;
push (z);
}
else if (token=='*')
{
x=top ();
pop ();
y=top ();
pop ();
cout<<"Evaluate "<<y<<" * "<<x;
z=y*x;
push (z);
}
else if (token=='/')
{
x=top ();
pop ();
y=top ();
pop ();
cout<<"Evaluate "<<y<<" / "<<x;
z=y/x;
push (z);
}
else
{
cout<<"Invalid Postfix Expression!\n";
goto selection;
}
}
cout<<"\n"<<postfix<<" is equal to "<<z;
}
else if (select==2)
{
system ("cls");
cout<<"Thank You for using this program. Goodbye!\n\n";
quit=true;
}
else
{
system ("cls");
cout<<"\t\tE R R O R !\n\tPlease enter a valid selection\n";
Sleep (3000);
goto welcome;
}
} while (!quit);
}
void construct ()
{
capacity=20;
postfix[capacity];
Top=-1;
}
bool empty ()
{
if (Top==-1)
return true;
else
return false;
}
void push (char item)
{
if (Top<capacity-1)
{
Top++;
postfix[Top]=item;
}
}
int top ()
{
char value;
if (Top!=-1)
{
value=postfix[Top];
return value;
}
else
return '0';
}
char pop ()
{
char value;
if (Top!=-1)
{
value=postfix[Top];
Top--;
return value;
}
else
{
cout<<"The Stack is empty!\n";
return 0;
}
}