enter the equation then :-
1- change the equation from infix to postfix then print the result
2- Solve the equation and then print the result
Allows the use of:
stack
queue
strtok
array
The numbers you enter from one status or more than one status
# include <stdio.h>
# include <iostream.h>
# include <math.h>
# define SIZE 100
class Stack
{
private:
char st[SIZE];
int Top;
public:
Stack()
{
Top=0;
}
~Stack()
{
}
char pop()
{
if(!isEmpty())
{
--Top;
return st[Top];
}
else
{
cout<<"Sorry ,This is Empty Stack \n\n";
return NULL;
}
}
void push(char v)
{
if(isFull())
{
cout<<"Sorry,Stack is Overload \n\n";
}
else
{
st[Top]=v;
Top++;
}
}
int isFull()
{
return Top>=SIZE;
}
int isEmpty()
{
return Top<=0;
}
};
class EvalStack
{
private:
int Ev[SIZE];
int Top;
public:
EvalStack()
{
Top=0;
}
~EvalStack()
{
}
int pop()
{
if(!isEmpty())
{
--Top;
return Ev[Top];
}
else
{
cout<<"Sorry ,This is Empty Stack \n\n";
return NULL;
}
}
void push(int v)
{
if(isFull())
{
cout<<"Sorry,Stack is Overload \n\n";
}
else
{
Ev[Top]=v;
Top++;
}
}
int isFull()
{
return Top>=SIZE;
}
int isEmpty()
{
return Top<=0;
}
};
int isOperator(char T)
{
if(T=='+'|| T=='/'|| T=='*' || T=='-' || T=='^' || T=='%')
return 1;
elsereturn 0;
}
int prio(char T)
{
if(T== '/' || T=='*' || T=='%')
return 100;
elseif(T=='^')
return 1000;
elseif(T=='+' || T=='-')
return 50;
elseif(T==')' || T=='(')
return 1500;
}
int Evaluat(int x, int y,char z )
{
int result;
if(z=='+')
{
result=x+y;
return result;
}
elseif(z=='-')
{
result=y-x;
return result;
}
elseif(z=='*')
{
result=x*y;
return result;
}
elseif(z=='/')
{
result=y/x;
return result;
}
elseif(z=='%')
{
result=pow(y,x);;
return result;
}
elseif(z=='^')
{
result=fmod(y,x);
return result;
}
}
void main()
{
EvalStack Eval;
Stack S;
int i=0;
char exp[100];
char postFix[100];
printf("Please enter Expression to evaluate:\n");
gets(exp);
char *p=exp;
while(*p)
{
char T=*p++;
if(!isOperator(T))
{
// cout<<T;
postFix[i]=T;
i++;
}
elseif(isOperator(T))
{
if(S.isEmpty())
S.push(T);
else
{
char v=S.pop();
while(prio(T)<= prio(v)&&S.isEmpty()S)
{
// cout<<v;
postFix[i]=v;
i++;
v=S.pop();
}
if(prio(T)>prio(v))
S.push(v);
S.push(T);
}
}
}
while(!S.isEmpty())
{
char z=S.pop();
postFix[i]=z;
i++;
// cout<<z;
cout<<"\n\n";
}
postFix[i]=NULL;
cout<<postFix<<"\n";
int j=0;
while(postFix[j]!=NULL)
{
if(!isOperator(postFix[j]))
{
Eval.push(postFix[j]-'0');
}
else
{
char z=postFix[j];
int x=Eval.pop();
int y=Eval.pop();
// int f=x-'0';
// int s=y-'0';
int result=Evaluat(x,y,z);
Eval.push(result);
}
j++;
}
int q=Eval.pop();
cout<<"The Final result for this Expression:"<<" "<<exp<<" "<<"is equal:"<<q<<"\n";
if(!Eval.isEmpty())
cout<<"syntax Erorr"<<"\n";
}
This program, which I worked, but this program to number from one status and does not contain arcs I want the program can contain arcs and numbers from more than one status
Do you have to write your own stack? Because your assignment says that it allows the use of stack and standard C++ libraries already have a stack class: http://cplusplus.com/reference/stl/stack/