首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

中序表达式转换成后序表达式,并依据后序表达式计算结果,程序出现非法操作,请

2012-03-16 
中序表达式转换成后序表达式,并依据后序表达式计算结果,程序出现非法操作,请高手指点代码如下C/C++ code#i

中序表达式转换成后序表达式,并依据后序表达式计算结果,程序出现非法操作,请高手指点
代码如下

C/C++ code
#include <iostream>#include <stack>#include <queue>#include <string>#include <stdlib.h>using std::iostream;using std::cout;using std::cin;using std::endl;using std::stack;using std::queue;using std::string;void CenToRear(queue<char> &s1,stack<char> &s2,string str)//将前序表达式转换成后序表达式{    char cs;    int index=0;    while(index<str.length())//此处是否越界?    {          cs=str[index];          if(cs>='0'&&cs<='9')            s1.push(cs);          else          {              s1.push(' ');              if(cs=='*' || cs=='/')              {                  if(s2.top()=='*' || s2.top()=='/')                  {                      s1.push(s2.top());                      s2.pop();                  }                  s2.push(cs);              }              if(cs=='+' || cs=='-')              {                  if(s2.empty())                      s2.push(cs);                  else                  {                      while(!s2.empty())                      {                          s1.push(s2.top());                          s2.pop();                      }                      s2.push(cs);                  }                                }          }         index++;     }    while(!s2.empty())    {        s1.push(s2.top());        s2.pop();    }}int main(){    string str;    queue<char> q;    stack<char> s;    char ch;    int value=0;    int index=0;    int ldata=0;    stack<int> sdata;        cout<<"Please enter your expression:\n";    while(cin>>str)    {        CenToRear(q,s,str);        while(!q.empty())        {            if(q.front()>='0'&&q.front()<='9')            {                value*=10;                value+=atoi(&q.front());            }            else            {                if(q.front()==' ')                {                    sdata.push(value);                    value=0;                }                if(q.front()=='*' || q.front()=='/' || q.front()=='+' || q.front()=='-')                {                    ch=q.front();                    switch(ch)//对不同运算符进行相应处理                    {                    case '+':                        ldata=sdata.top();                        sdata.pop();                        ldata+=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '-':                        ldata=sdata.top();                        sdata.pop();                        ldata-=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '*':                        ldata=sdata.top();                        sdata.pop();                        ldata*=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '/':                        ldata=sdata.top();                        sdata.pop();                        ldata/=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    default:                        break;                    }                }            }            q.pop();        }        cout<<"The result is:"<<sdata.top();        cout<<endl;        cout<<"Please enter your expression:\n";    }}





[解决办法]
C/C++ code

#include <iostream>#include <stack>#include <queue>#include <string>#include <stdlib.h>using std::iostream;using std::cout;using std::cin;using std::endl;using std::stack;using std::queue;using std::string;void CenToRear(queue<char> &s1,stack<char> &s2,string str)//将前序表达式转换成后序表达式{    char cs;    int index=0;    while(index<str.length())//此处是否越界?    {          cs=str[index];          if(cs>='0'&&cs<='9')            s1.push(cs);          else          {              s1.push(' ');              if(cs=='*' || cs=='/')//有改动              {                  while (!s2.empty() && (s2.top()=='*' || s2.top()=='/'))                  {                      s1.push(s2.top());                      s2.pop();                  }                  s2.push(cs);              }              if(cs=='+' || cs=='-')//有改动              {                  while (!s2.empty())                  {                      s1.push(s2.top());                      s2.pop();                  }                  s2.push(cs);                              }          }          index++;     }    s1.push(' '); //有改动    while(!s2.empty())    {        s1.push(s2.top());        s2.pop();    }}int main(){    string str;    queue<char> q;    stack<char> s;    char ch;    int value=0;    int index=0;    int ldata=0;    stack<int> sdata;        cout<<"Please enter your expression:\n";    while(cin>>str)    {        CenToRear(q,s,str);#if    0        while (!q.empty())        {            cout << q.front();            q.pop();        }        system("pause");#endif        while(!q.empty())        {            if(q.front()>='0'&&q.front()<='9')            {                value*=10;                value+=q.front()-'0';            }            else            {                if(q.front()==' ')                {                    sdata.push(value);                    value=0;                }                if(q.front()=='*' || q.front()=='/' || q.front()=='+' || q.front()=='-')                {                    ch=q.front();                    switch(ch)//对不同运算符进行相应处理                    {                    case '+':                        ldata=sdata.top();                        sdata.pop();                        ldata+=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '-':                        ldata=sdata.top();                        sdata.pop();                        ldata-=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '*':                        ldata=sdata.top();                        sdata.pop();                        ldata*=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    case '/':                        ldata=sdata.top();                        sdata.pop();                        ldata/=sdata.top();                        sdata.pop();                        sdata.push(ldata);                        ldata=0;                        break;                    default:                        break;                    }                }            }            q.pop();        }        cout<<"The result is:"<<sdata.top();        cout<<endl;        cout<<"Please enter your expression:\n";    }}Please enter your expression:1+2*3+9The result is:16Please enter your expression: 

热点排行