郁闷的C小加(三) 409

郁闷的C小加(三)

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很高兴。但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值。即先把表达式转换为前缀和后缀表达式,再求值。这时又要考虑操作数是小数和多位数的情况。
输入
第一行输入一个整数T,共有T组测试数据(T<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数并且小于1000000。
数据保证除数不会为0。
输出
对于每组测试数据输出结果包括三行,先输出转换后的前缀和后缀表达式,再输出计算结果,结果保留两位小数。
样例输入
2
1+2=
(19+21)*3-4/5=
样例输出
+ 1 2 =
1 2 + =
3.00
- * + 19 21 3 / 4 5 =
19 21 + 3 * 4 5 / - =
119.20
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stack>
using namespace std;
stack<char>sta;
stack<double>dou;
char s[1005],bef[1005];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int i,k=0;
		scanf("%s",&s[1]);
		int len = strlen(&s[1]);
		s[0] = '(';
		s[len] = ')';
		for(i=len;i>=0;i--)
		{
			if(s[i]==')')
			{
				sta.push(s[i]);
			}
			else if(s[i]=='(')
			{
				while(sta.top()!=')')
				{
					bef[k++] = ' ';
					bef[k++] = sta.top();
					sta.pop();
				}
				sta.pop();
			}
			else if(s[i]=='+' || s[i]=='-')
			{
				while(sta.top()=='*' || sta.top()=='/')
				{
					bef[k++] = ' ';
					bef[k++] = sta.top();
					sta.pop();
				}
				sta.push(s[i]);
				bef[k++] = ' ';
			}
			else if(s[i]=='*' || s[i]=='/')
			{
				sta.push(s[i]);
				bef[k++] = ' ';
			}
			else
			{
				bef[k++] = s[i];
			}
		}
		for(i=k-1;i>=0;i--)
		{
			printf("%c",bef[i]);
		}
		printf(" =\n");
		for(i=0;i<=len;i++)
		{
			if(s[i]=='(')
			{
				sta.push(s[i]);
			}
			else if(s[i]==')')
			{
				while(sta.top()!='(')
				{
					double a1,a2,a3;
					a1 = dou.top();
					dou.pop();
					a2 = dou.top();
					dou.pop();
					switch(sta.top())
					{
						case '+':a3 = a2 + a1;break;
						case '-':a3 = a2 - a1;break;
						case '*':a3 = a2 * a1;break;
						case '/':a3 = a2 / a1;break;
					}
					dou.push(a3);
					printf(" %c",sta.top());
					sta.pop();
				}
				sta.pop();
			}
			else if(s[i]=='+' || s[i]=='-')
			{
				while(sta.top()!='(')
				{
					double a1,a2,a3;
					a1 = dou.top();
					dou.pop();
					a2 = dou.top();
					dou.pop();
					switch(sta.top())
					{
						case '+':a3 = a2 + a1;break;
						case '-':a3 = a2 - a1;break;
						case '*':a3 = a2 * a1;break;
						case '/':a3 = a2 / a1;break;
					}
					dou.push(a3);
					printf(" %c",sta.top());
					sta.pop();
				}
				sta.push(s[i]);
				printf(" ");
			}
			else if(s[i]=='*' || s[i]=='/')
			{
				if(sta.top()=='*')
				{
					double a1,a2;
					a1 = dou.top();
					dou.pop();
					a2 = dou.top();
					dou.pop();
					dou.push(a2*a1);
					printf(" %c",sta.top());
					sta.pop();
				}
				else if(sta.top()=='/')
				{
					double a1,a2;
					a1 = dou.top();
					dou.pop();
					a2 = dou.top();
					dou.pop();
					dou.push(a2/a1);
					printf(" %c",sta.top());
					sta.pop();
				}
				sta.push(s[i]);
				printf(" ");
			}
			else if(s[i]>='0' && s[i]<='9')
			{
				double d = 0;
				int f = 0,b = i;
				while(s[i]>='0' && s[i]<='9' || s[i]=='.')
				{
					printf("%c",s[i]);
					if(s[i]=='.')
					{
						b = i;
						f = 1;
					}
					else
					{
						d = d * 10 + s[i] - '0';
					}
					i++;
				}
				i--;
				if(f)
				{
					dou.push(d/pow(10,i-b));
				}
				else
				{
					dou.push(d);
				}
			}
		}
		printf(" =\n");
		printf("%.2lf\n",dou.top());
	}
	return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值