会打代码的扫地王大爷 2017-03-06 09:38 采纳率: 33.3%
浏览 1853

输入表达式,输出值! (中缀表达式)

//输入表达式,输出值! (中缀表达式)
//都单步还是想不通输入1 + 2 00的时候那个运算符栈为什么会输出是00,2

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

bool in_out(char in,char out)//true  in 
{
   if(out=='(')
       return true;
   else if(out==')'&&in!='(')
       return false;
   else if(in=='('&&out!=')')
       return true;
   else if(in=='('&&out==')')
       return false;

   if((in=='+'||in=='-')&&(out=='*'||out=='/'))
   {
      return true;
   }

   return false;

}

int main()
{
    cout<<"请输入中缀表达式,每个数字或字符用空格隔开"<<endl;
    char op[100];
    stack<char*>    yunsuan;
    stack<double>  number;


    while(cin>>op)
    {
      if(strcmp(op,"00")==0)
          break;

      if(op[0]!='+'&&op[0]!='-'&&op[0]!='*'&&op[0]!='/'&&op[0]!='('&&op[0]!=')')
      {
          number.push(atof(op));
          continue;
      }


      if(yunsuan.empty())
      {
             yunsuan.push(op);
             continue;
      }


       char in;

       in=yunsuan.top()[0];
       char out=op[0];
       bool res=in_out(in,out);

       if(res)
        {
           yunsuan.push(op);
           continue;
        }

        while(!res)
        {
           if(in=='('&&out==')')
            {
               yunsuan.pop();////////
              // res=true;
               //continue;
               break;
            }
           else
           {
                   double n2=number.top();
                   number.pop();
                   double n1=number.top();
                   number.pop();


                       switch(out)
                         {
                          case '(': 

                             break;

                         case ')':

                             break;


                         case '+':
                             number.push(n1+n2);

                             break;


                         case '-':
                             number.push(n1-n2);
                             break;

                         case '*':
                            number.push(n1*n2);
                             break;

                         case '/':
                            number.push(n1/n2);
                             break;

                         default:cout<<"error"<<endl;
                      }
                   yunsuan.pop();
                   //number.push();

                   in=yunsuan.top()[0];
                   res=in_out(in,out);
           }
        }

          yunsuan.push((char*)(""+out));
      }


    while(!yunsuan.empty())
    {
        cout<<yunsuan.top()<<endl;
        yunsuan.pop();
        /*
       char out=yunsuan.top()[0];

             double n2=number.top();
                   number.pop();
                   double n1=number.top();
                   number.pop();


                       switch(out)
                         {
                          case '(': 

                             break;

                         case ')':

                             break;


                         case '+':
                             number.push(n1+n2);

                             break;


                         case '-':
                             number.push(n1-n2);
                             break;

                         case '*':
                            number.push(n1*n2);
                             break;

                         case '/':
                            number.push(n1/n2);
                             break;

                         default:cout<<"error"<<endl;
                      }
                   yunsuan.pop();
                   */

    }

    cout<<number.top()<<endl;


   return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2017-03-06 15:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法