简体   繁体   中英

c++ The evaluation of expression. How to exit the while loop?

I use stack to evaluate an expression. The most important function is below:

double Expression_Eval() 
{ 
SeqStack<char,100> OPTR;                
SeqStack<double,100> OPND;      
OPTR.Push('@');
char ch;
ch=getchar();   
while (ch!='@' || OPTR.GetTop()!='@')
{  
    if (!InOPTR(ch))        
    {  
        int n=ch-'0';
        double num=(double)n;
        OPND.Push(num); 
        ch=getchar();   
    }       
    else                        
    { 
        char pre_op=OPTR.GetTop();
        switch (Precede(pre_op, ch))
        { 
            case '<': OPTR.Push(ch);                          
                      ch=getchar();                         
                      break;
            case '=': OPTR.Pop(); 
                      ch=getchar();                       
                      break;
            case '>': double b=OPND.Pop();  
                      double a=OPND.Pop();   
                      pre_op=OPTR.Pop();
                      OPND.Push(Operate(a, pre_op, b));
                      ch=getchar();                        
                      break;
        }
    }
}
return OPND.GetTop(); 
} 

Then, when I input 8/(5-3)@ , it will not print the result.

I think the loop termination condition ch!='@' || OPTR.GetTop()!='@' is wrong. When I press Enter , getchar() get the last char is CR but not @ .

But, I don't know how to revise it to make my program work.

The other part of my program is below:

#include<iostream>
using namespace std;
template<typename DataType,int StackSize>
class SeqStack
{
private:
DataType data[StackSize];
int top;
public:
SeqStack()
{   top=-1;     }
~SeqStack() {}
void Push(DataType x)
{
    if(top == StackSize-1)
        throw "error";
    data[++top]=x;
}
DataType Pop()
{
    if(top == -1)
        throw "error";
    DataType x=data[top--];
    return x;
}
DataType GetTop()
{
    if(top != -1)
        return data[top];
    else
        cout<<"error";
}
};
bool InOPTR(char ch)
{
if( (ch>='(' && ch<='+') || ch=='-' || ch=='/' )
{
    return true;
}else{
    return false;
}
}
char Precede(char op1, char op2)
{
char pri[7][7]={ {'>','>','<','<','<','>','>'}
                , {'>','>','<','<','<','>','>'}
                , {'>','>','>','>','<','>','>'}
                , {'>','>','>','>','<','>','>'}
                , {'<','<','<','<','<','=','@'}
                , {'>','>','>','>','@','>','>'}
                , {'<','<','<','<','<','@','='} };
int m,n;
switch(op1)
{
case '+': m=0;break;
case '-': m=1;break;
case '*': m=2;break;
case '/': m=3;break;
case '(': m=4;break;
case ')': m=5;break;
case '@': m=6;break;
}
switch(op2)
{
case '+': n=0;break;
case '-': n=1;break;
case '*': n=2;break;
case '/': n=3;break;
case '(': n=4;break;
case ')': n=5;break;
case '@': n=6;break;
}
return pri[m][n];
}
double Operate(double a, char op, double b)
{
double result;
switch(op)
{
case '+': result=a+b; break;
case '-': result=a-b; break;
case '*': result=a*b; break;
case '/': result=a/b; break;
}
return result;
}    
int main()
{
   double r=Expression_Eval();
   cout<<r<<endl;
   return 0;
}

Problem seem to be that '@' is considered a number, but it should be considered an operation:

Use:

bool InOPTR(char ch) {
    if ((ch >= '(' && ch <= '+') || ch == '-' || ch == '/' || ch=='@'){
        return true;
    }
    else {
        return false;
    }
}

Note that '@' is ASCII 64 which is not covered in the ranage '(' to '+' [40-43]

Hope this helps.

You need to consume carriage return or newline character after getchar(); which comes into play when you press enter button.

One trick is as below.

ch=getchar(); 

getchar(); //this getchar to consume CR.

since you have used ch = getchar() many times you have to use above solution at many places.

Better solution to this problem will be to enter string instead of entering single character using getchar() ...

Hope you got what I am trying to say...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM