zoukankan      html  css  js  c++  java
  • NYOJ 267(郁闷的C小加(二)) 后缀表达式求值

    悲剧了好几次,应该先判断栈是否为空,才能取栈顶元素,应该记住啊!

      1 #include <iostream>
      2 #include <stack>
      3 #include<string>
      4 #include<cstdlib>
      5 #include<cstdio>
      6 #include<algorithm>
      7 using namespace std;
      8 stack<double> num;
      9 stack<char> sc;
     10 double exp(double n1, double n2, char ch)
     11 {
     12     switch(ch){
     13         case '+':return n1+n2;
     14         case '-':return n2-n1;
     15         case '*':return n1*n2;
     16         case '/':return n2/n1;
     17         default: break;
     18     }
     19 }
     20 
     21 bool isnum(char c)
     22 {
     23     if((c >= '0' && c <= '9') || c == '.')
     24         return true;
     25     return false;
     26 }
     27 
     28 bool pre(char c1, char c2)
     29 {
     30     if(c1 == ')' || c1 == '=')
     31         return false;
     32     else if(c2 == '(')
     33         return true;
     34     else if(c1 == '+' || c1 == '-')
     35         return false;
     36     else if((c1 == '*' || c1 == '/') && (c2 == '*' || c2 == '/'))
     37         return false;
     38     return true;
     39 }
     40 
     41 void out()
     42 {
     43     double n1,n2;
     44     n1 = num.top(); num.pop();
     45     n2 = num.top(); num.pop();
     46     cout<<sc.top();
     47     num.push(exp(n1,n2,sc.top()));
     48     sc.pop();
     49 }
     50 void slove(string s)
     51 {
     52     int i,j; char a[19];
     53     while(!sc.empty()) sc.pop();
     54     while(!num.empty()) num.pop();
     55     for(i=0; i<s.length(); ++i)
     56     {
     57         if(isnum(s[i]))
     58         {
     59             j = 0;
     60             while(isnum(s[i]))    a[j++] = s[i++];
     61             a[j] = '\0';  cout<<a;
     62             num.push(atof(a));  --i;
     63         }
     64         else
     65         {
     66             if(sc.empty() || pre(s[i],sc.top()))    sc.push(s[i]);
     67             else{
     68                 if(s[i] == ')')
     69                 {
     70                     while(sc.top() != '(')  out();
     71                     sc.pop();
     72                 }
     73                 else if(s[i] == '=')
     74                 {
     75                     while(!sc.empty())    out();
     76                 }
     77                 else
     78                 {
     79                     while(!sc.empty() && !pre(s[i],sc.top()) )    out();
     80                     sc.push(s[i]);
     81                 }
     82             }
     83         }
     84     }
     85     cout<<'='<<endl;
     86     printf("%.2f\n",num.top());
     87     num.pop();
     88 }
     89 
     90 int main()
     91 {
     92     int t;
     93     cin>>t;
     94     while(t--)
     95     {
     96         string s;
     97         cin>>s;
     98         slove(s);
     99         if(t>=1)
    100             cout<<endl;
    101     }
    102     return 0;
    103 }
  • 相关阅读:
    正向代理和反向代理
    CFree5构建中止问题
    [转]Prototype核心成员的反思
    解决js文件乱码问题
    【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法
    rem 搭配 less 适配
    中国近代史-蒋廷黻
    docker 生成镜像 上传到docker hub
    CMD 执行 Python 脚本 出现假死状态
    vue 创建项目的两种方式
  • 原文地址:https://www.cnblogs.com/yaling/p/3054509.html
Copyright © 2011-2022 走看看