zoukankan      html  css  js  c++  java
  • 20-表达式求值

    /*
    表题目内容:

     输入一个由数字、+,-,*,/ 组成的算术表达式,求其值。

    输入描述

    一个表达式,其中每个运算数为正整数

    输出描述

    计算结果,保留2位小数

    输入样例

    1+12*3-4/2

    输出样例

    35.00
     
    */

    #include <iostream>
    #include <cstdio>
    #include <stack>
    using namespace std;
        
    int fu(char ch){
        if(ch == '#' || ch == '=')
            return 0;
        if(ch == '-' || ch == '+')
            return 1;
        if(ch == '*' || ch == '/')
            return 2;    
    }
    
    void qiuzhi(string str){
        int i = 0;
        stack <double>s;  //数字栈 
        stack <char>f;    //符号栈    优先级低的高的进栈,比栈顶低,则弹出栈顶的进行运算,直到比栈顶高才进栈 
        f.push('#');
        while(str[i]){
            int a = 0, flag = 0;
            while(str[i] >= '0' && str[i] <= '9'){
                a = a * 10 + str[i] - '0';
                i++; 
                flag = 1;
            }
            if(flag){ 
            	if(f.top() == '-'){   //不将减法转为加法会有错误,测试样例:1-2-3  结果应该为:-4
            		f.pop();
            		f.push('+');
            		a = -a;
            	} 
                s.push(a);
    		} 
            if(fu(str[i]) >= fu(f.top())){
                f.push(str[i]);  
                i++;    
            }
            int flag1 = 0;
            while(fu(str[i]) < fu(f.top())){
                char ch = f.top();
                f.pop();
                double c, d, e;
                c = s.top(); s.pop();
                d = s.top(); s.pop();
                if(ch == '-')
                    e = d - c;
                else if(ch == '+')
                    e = d + c;            
                else if(ch == '*')
                    e = d * c;
                else if(ch == '/')
                    e = d * 1.0 / c;        
                s.push(e);
    //            f.push(str[i]);    
                flag1 = 1;
            }
            if(flag1){
                if(str[i] == '='){
                    double sum = s.top();
                    printf("%.2lf", sum);
    //                cout << sum;
                    return;
                }
                f.push(str[i]);
                i++;
            }
        }
    }
    
    int main(){
        string str;
        cin >> str;
        str += '=';
        qiuzhi(str);    
    //    cout << str;
        return 0;
    }
    

      

  • 相关阅读:
    DELPHI开发LINUX包
    DELPHI开发LINUX的动态库
    中间件测试工具
    CENTOS7安装OPENSSL
    咏南跨平台中间件简介
    IdHTTPServer开发https服务器
    ServiceStack.Redis常用操作
    ServiceStack.Redis 之 IRedisTypedClient<第四篇>
    Redis常用命令速查 <第二篇>
    ServiceStack.Redis之IRedisClient<第三篇>
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/7429758.html
Copyright © 2011-2022 走看看