郁闷的C小加(二)

郁闷的C小加(二)

郁闷的C小加(二)

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很高兴。但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值。即先把表达式转换为后缀表达式,再求值。这时又要考虑操作数是小数和多位数的情况。
 
输入
第一行输入一个整数T,共有T组测试数据(T<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数并且小于1000000。
数据保证除数不会为0。
输出
对于每组测试数据输出结果包括两行,先输出转换后的后缀表达式,再输出计算结果,结果保留两位小数。两组测试数据之间用一个空行隔开。
样例输入
[/cpp]2
1+2=
(19+21)*3-4/5=
样例输出
[/cpp]12+=
3.00

1921+3*45/-=
119.20

逆波兰式,中缀表达式,后缀表达式
[cpp]
#include “iostream”
#include “string”
#include “stack”
#include “iomanip”//控制精度
using namespace std;
int first(char ch)
{
switch(ch)
{
case ‘+’:
case ‘-‘:return 1;
case ‘*’:
case ‘/’:return 2;
case ‘(‘:
case ‘@’:
default: return 0;
}
}
void change(string &s1,string &s2)
{
stack s;s.push(‘@’);
int i=0;char ch=s1[i];
while(i=first(ch))
{
s2+=w;s2+=’ ‘;s.pop();
w=s.top();
}
s.push(ch);
ch=s1[++i];
}
else
{
while((ch>=’0’&&ch<='9')||ch=='.') { s2+=ch; ch=s1[++i]; } s2+=' '; } } ch=s.top();s.pop(); while(ch!='@') { s2+=ch;s2+=' '; ch=s.top(); s.pop(); } } void value(string s2) { stack s;double x,y;
int i=0;
for(i=0;i=’0’&&s2[i]<='9') { x=x*10+s2[i]-48;i++; } if(s2[i]=='.') { i++;y=0;double j=10.0; while(s2[i]>=’0’&&s2[i]<='9') { y+=((s2[i]-48)/j);i++;j*=10; } x+=y; } } } s.push(x); } cout <

  • CPP.LA 0
  • Huangea Blog 0
  • rabbit8's blog 2
  • 方舟博客 0
  • 田伟博客 2