codeup 1918 简单计算器

问题 A: 简单计算器

时间限制: 1 Sec  内存限制: 32 MB
提交: 401  解决: 192
[提交][状态][讨论版][命题人:外部导入]

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

样例输入

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出

12178.21


用作对栈和队列的小练习。
啊!九月的考试求不挂!!!
/**********************
author: yomi
date: 18.7.29
ps:
string tmp = "";
tmp+=s.top();//s.top()为char型字符
此写法tmp不会有任何问题
然而,
string tmp=s.top()+"";
则会乱码,注意,考场上千万别紧张出岔子。
**********************/
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
char s[250];
int main()
{
    string a;
    map<char, int>mmap;
    mmap['+'] = 0;
    mmap['-'] = 0;
    mmap['*'] = 1;
    mmap['/'] = 1;

    while(getline(cin, a)){
        if(a == "0"){
            break;
        }
        ///去掉a中的全部空格
        for(string::iterator iter=a.begin(); iter!=a.end(); ++iter){
            if(*iter == ' '){
                a.erase(iter);
            }
        }
        ///中缀表达式转后缀表达式
        int len = a.length();
        double ans = 0, tmp=0;
        stack<char>s;
        while(!s.empty()){
            s.pop();
        }
        queue<string>q;
        while(!q.empty()){
            q.pop();
        }
        string t = "";
        for(int i=0; i<len; i++){
            if(isdigit(a[i])){
                t+=a[i];
            }
            else{
                q.push(t);
                t = "";
                ///compare with the element in stack
                if(s.empty()){
                    s.push(a[i]);
                }
                else if(mmap[a[i]] > mmap[s.top()]){
                    s.push(a[i]);
                }
                else{
                    while(!s.empty()){
                        if(mmap[a[i]] <= mmap[s.top()]){
                            string tmp ="";
                            tmp+= s.top();
                            q.push(tmp);
                            s.pop();
                        }
                        else{
                            break;
                        }
                    }
                    s.push(a[i]);
                }
            }
        }
        q.push(t);
        while(!s.empty()){
            string tmp = "";
            tmp+=s.top();
            q.push(tmp);
            s.pop();
        }
        ///计算后缀表达式的值
        stack<double>s1;
        while(!s1.empty()){
            s1.pop();
        }
//        while(!q.empty()){
//            cout << q.front() << ' ';
//            q.pop();
//
//        }
        double n1 = 0.00, n2 = 0.00;
         while(!q.empty()){
            string t = q.front();
            if(t!="+" && t!="-" && t!="*" && t!="/"){
                stringstream is1;
                is1.str(t);
                double n;
                is1 >> n;
                s1.push(n);
            }
            else{

                n1 = s1.top();
                s1.pop();
                n2 = s1.top();
                s1.pop();
                double tmp = 0.00;
                if(t == "+")
                        tmp = n1+n2;
                if(t == "-")
                        tmp = n2-n1;
                if(t == "*")
                        tmp = n1*n2;
                if(t == "/")
                        tmp = n2/n1;
                s1.push(tmp);
            }
            q.pop();
        }
        printf("%.2f\n", s1.top());

    }

    return 0;
}
/**
3+45/3*2   ---->   3 45 3 / 2 * +
36

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0
**/

 


posted @ 2018-07-30 19:41  深圳地铁Princess  阅读(639)  评论(0编辑  收藏  举报