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;
	}
}

Don't use postfix both hold the expression and act as a stack to evaluate the expression. I would suggest declaring an array of type double called myStack to use in evaluating the expression.

The elements of postfix are all type char. Since the compiler recognizes type char by the integer value assigned to it in some character set, it is possible/legal to add/subtract two char variables, though you have to know what you are doing to make it do what you intend it to. It's best if you convert a given char to an int before doing routine mathematical calculations. This can usually be done by subtracting char zero from whatever char you have. This works when characters 0 through 9 are listed sequentially in the character set used, but most character sets do that.

Your char by char evaluation of the expression means that evaluating the expression will be most straight forward when any numerical input is limited to single digit values. Your char by char approach could be extended to more sophisticated expression, but it would take some work. If you want to allow more than single digit integers or decimal values to be used as part of the input expression then viewing the expression as a series of substrings separated by some delimitng value (the space char would be a convenient delimiting value) would probably make more sense. Each substring can be separated/parsed/viewed a stand alone string which is convenient since strings can pretty readily be determined to represent numerical values or mathematical operators and if it is a string representing a numerical value it can be converted to that numerical value by using a stringstream. Conceiveably the substrings could represent more than just (multidigit) numerical values and mathematical operators, of course, but being able to do that is probably beyond what you were asked to do.

the same solution in a simpler way

#include<iostream>
#include<stack>
using namespace std;

bool evalPostfix(char *postfix, double &result, char *error);
bool isop(char c);
int main()
{
        char post[100];
        char err[100];
        cout<<"Enter postfix expr:";
        cin>>post;

        double result;
        if(evalPostfix(post, result, err))
                cout<<"result="<<result<<endl;
        else
                cout<<err<<endl;
        return 0;
}

bool evalPostfix(char *postfix, double &result, char *error)
{
        stack<double> stk_post;
        double val1, val2;
        while(*postfix)
        {
                if(isdigit(*postfix))
                {
                        stk_post.push((double)(*postfix-'0'));
                }
                else if(isop(*postfix))
                {
                        if(stk_post.size()<2)
                        {
                                strcpy(error, "INVALID POSTFIX EXPR.");
                                return false;
                        }
                        val1 = stk_post.top();
                        stk_post.pop();
                        val2 = stk_post.top();
                        stk_post.pop();
                        switch(*postfix)
                        {
                                case '+':
                                        stk_post.push(val2+val1);
                                        break;
                                case '-':
                                        stk_post.push(val2-val1);
                                        break;
                                case '*':
                                        stk_post.push(val2*val1);
                                        break;
                                case '/':
                                        stk_post.push(val2/val1);
                                        break;
                                case '%':
                                        stk_post.push((double)((int)val2%(int)val1));
                                        break;
                                default:
                                        strcpy(error, "Unhandled operator.");
                                        return false;
                        }
                }
                else
                {
                        strcpy(error, "Unexpected character.");
                        return false;
                }
                postfix++;
        }
        if(stk_post.size()!=1)
        {
                strcpy(error, "Invalid postfix Expression");
                return false;
        }
        result = stk_post.top();
        strcpy(error, "NO ERROR");
        return true;
}
bool isop(char c)
{
        if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%')
                return 1;
        return 0;
}

Edited 7 Years Ago by dkalita: n/a

This article has been dead for over six months. Start a new discussion instead.