• 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 }
  • 相关阅读:
    Object之克隆对象clone 和__clone()函数
    Object之魔术函数__toString() 直接输出对象引用时自动调用
    Object之魔术函数__call() 处理错误调用
    Git关联远程GitHub仓库
    python制作查找单词翻译的脚本
    用python处理文本,本地文件系统以及使用数据库的知识基础
    基于序列化技术(Protobuf)的socket文件传输
    Python核心编程——Chapter16
    gdb初步窥探
    unp学习笔记——Chapter1
  • 原文地址:https://www.cnblogs.com/yaling/p/3054509.html
Copyright © 2020-2023  润新知