1918 Problem A 简单计算器

问题 A: 简单计算器

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

题目描述

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

输入

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

输出

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

样例输入

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

样例输出

12178.21
#include<iostream>  
#include<stack>  
#include<string>  
#include<stdlib.h>
#include<cstdio>
using namespace std;  

/*判断符号间的优先关系函数 
*1表示>,0表示=,-1表示< 
*c1栈内的算符,c2栈外的算符 

此题于7.1 1918再次出现
用了结构体 map queue写法
*/  
int Judge(char c1,char c2)
{
    int a1,a2;
    if('+'==c1||'-'==c1) a1 = 3;
    if('*'==c1||'/'==c1)a1 = 5;
    if('('==c1) a1 = 1;
    if(')'==c1) a1 = 7;
    if('#'==c1) a1 = 0;

    if('+'==c2||'-'==c2)a2 = 2;
    if('*'==c2||'/'==c2)a2 = 4;
    if('('==c2) a2 = 6;
    if(')'==c2) a2 = 1;
    if('#'==c2) a2 = 0;
    if(a1>a2) return 1;
    if(a1==a2) return 0;
    if(a1<a2) return -1;
}
//符号运算函数  
double run(char c ,double d1,double d2)  
{  
    switch (c)  
    {  
    case '+':  
        return d1+d2;  
        break;  
    case '-':  
        return d1-d2;  
        break;  
    case'*' :  
        return d1*d2;  
        break;  
    case '/':  
        return d1/d2;  
        break;  
    default:  
        return 0.0;  
        break;  
    }  
}  
int main()  
{  
	char op[8] = "+-*/()#";
	string str;
	while (getline(cin, str), str != "0") {
		for (string::iterator it = str.begin(); it != str.end(); it++) {
			if (*it == ' ') str.erase(it);
		}
		//给表达式字符串str添加'#'结束标识符  
		str.append(1, '#');
		stack<char> OPTR;//运算符栈  
		stack<double> OPND;//操作数栈  
		int a = -1;
		//先将#符号入栈  
		OPTR.push('#');
		while (true)
		{
			int b = a + 1;
			a = str.find_first_of(op, a + 1);
			if (a == string::npos) break;
			if (a != b)
			{
				string ss(str, b, a - b);
				double d = atof(ss.c_str());
				//数据先入栈  
				OPND.push(d);
			}
			//运算符优先级比较  
			int ju = Judge(OPTR.top(), str[a]);
			if (-1 == ju)//栈外优先级大直接入栈  
			{
				OPTR.push(str[a]);
			}
			if (0 == ju)//栈内外优先级相等则出栈  
			{
				OPTR.pop();
			}
			if (1 == ju)//栈内优先级大,出栈进行运算  
			{
				double d1 = OPND.top();
				OPND.pop();
				double d2 = OPND.top();
				OPND.pop();
				d1 = run(OPTR.top(), d2, d1);
				//运算结果入栈  
				OPND.push(d1);
				OPTR.pop();
				a--;
			}
		}
		//删除表达式最后的'#'结束标识符
		str.erase(str.length() - 1, 1);
		printf("%.2f\n", OPND.top());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值