# 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";
}
for this questiom
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
Figures that fall may be from one status or more
Possible equation contains arcs
I Analyzed the question, but I analyzed the question for number from status one and does not contain arcs .. Please help me
1. Abstain from using gets -- it has several flaws in design. Use getline instead. Also, if you are using C++ then it would be better to use C++ streams instead of a mix of both C and C++.
2. Line 219-225. The loop condition is not proper. You require the stack to be empty to enter the loop and yet you pop items from the stack inside the loop, even though it is empty?
3. I would suggest you to include more comments in your file in the future.
4. You really shouldn't be using strtok(). The assignment requires you to use it? I'm surprised.
5. In standard C++ the header file should be <iostream> not <iostream.h>.
6. Rather than using fixed length C Strings, use the std::string class from <string>. It, at least, is easier to use.
7. Use switch cases wherever you can, instead of using a long if... else if... branch.
8. Organise the implementations of Stack in different headers and source files. E.g., Stack.h, Stack.c.
Finally, a post script:
In deciphering expressions I would suggest you first learn a bit about BNF. It will be of immense help to you if you wish to tokenize and handle such expressions in an easier way.
i know > I analyzed the question and the code above in the subject, but I need anyone that I want him to become adjusted to the number from status of one or more .. and solve the problem arcs
Edit: irrelevant if the arc actually means parenthesis. It doesn't seem like I can delete this post.
Are you having trouble with parsing the user input or with calculating?
Parsing user input text can be tricky, that is why I had suggested the use of BNF in my earlier post.
I have a problem in that the solution >> can enter without equivalent arcs and I want to enter the equation with arcs .. And that the figures in the equation introduced from one status, such as 2. 3. 5, and I want it from one or more status such as 2. 45. 66. 676