/* 表达式思想
运算符优先级:
注意:函数参数从右至左计算
(4) '*''/'>(3)'+'-'>(2)'('>(1)')'
如果符号位栈顶为空,或者即将入栈的符号为‘(’则压入栈
如果即将入栈的符号不是')'且优先级高于栈顶元素,则符号位入栈
如果即将入栈符号为‘)’,则符号栈出栈一次,数字栈出栈俩次,进行运算,结果压入数字栈,直到遇到第一个左括号
如果即将入栈符号等级低于栈顶元素,栈顶元素出栈,数字栈元素出栈俩次,进行运算,结果压入数字栈,继续比较,直到符号栈为空,或优先级高于栈顶元素
所有元素和符号入栈后判断是否运算结束,即符号栈是否为空,不为空则从栈顶开始依此计算。
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
double expcal(double a,double b,char c);
char popop(char b[]);//运算符出栈
double popnum(double b[]);//数字出栈
double getnum(double b[]);//取栈顶数字
double popnum(double b[]);//出栈栈顶数字
void pushnum(double a,double b[]);//数字入栈
void pushop(char a,char b[]);//符号入栈
int isop(char a);//判断是否为符号
int isbig(int a,int b);//判断a运算级别是否大于b
char getop(char b[]);//取栈顶运算符
int oplevel(char a);//判断运算符等级
int optop=0;//指向符号栈内数量
int numtop=0;//指向数字栈内数量
int main()
{
int len;
double tmpnum;
char s[10000];
int i;
int j;
int flag;
double c,d;
char op[10000];//存放运算符
double num[10000];//存放double型数字
char tmp[100000];
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
memset(s,0,sizeof(s));
scanf("%s",s);
len=strlen(s);
for (i=0;i<len;i++)
{
if (i==len-1)//如果为等号就结束
{
break;
}
flag=1;
j=0;
if (isop(s[i]))
{
while (flag)
{
if (optop==0||s[i]=='(')
{
pushop(s[i],op);
flag=0;
}
else
{
if (isbig(oplevel(s[i]),oplevel(getop(op))))
{
pushop(s[i],op);
flag=0;
}
else
{
if (s[i]==')')
{
while (getop(op)!='(')
{
c=popnum(num);
d=popnum(num);
tmpnum=expcal(c,d,popop(op));
pushnum(tmpnum,num);
}
popop(op);
flag=0;
}
else
{
// printf("%c op1",getop(op));
c=popnum(num);
d=popnum(num);
tmpnum=expcal(c,d,popop(op));
pushnum(tmpnum,num);
}
}
}
}
}
else
{
while (!isop(s[i]))
{
tmp[j++]=s[i++];
}
i--;
tmp[j]='\0';
num[numtop++]=atof(tmp);
}
}
// printf("%lf num1 %lf num2 %lf num3 ",num[0],num[1],num[2]);
// printf("%c op1 %c op2 ",op[0],op[1]);
// printf("%d optop",optop);
while (optop)
{
c=popnum(num);
d=popnum(num);
tmpnum=expcal(c,d,popop(op));
pushnum(tmpnum,num);
}
printf("%.2lf \n",tmpnum);
}
return 0;
}
double expcal(double a,double b,char c)
{
switch (c)
{
case'+': return a+b;
case'-': return b-a;
case'*': return a*b;
case'/': return b/a;
default: return -1;
}
}
char popop(char b[])
{
return b[--optop];
}
double popnum(double b[])
{
return b[--numtop];
}
double getnum(double b[])
{
return b[numtop-1];
}
char getop(char b[])
{
return b[optop-1];
}
void pushnum(double a,double b[])
{
b[numtop++]=a;
}
void pushop(char a,char b[])
{
b[optop++]=a;
}
int isop(char a)
{
if (a=='('||a==')'||a=='*'||a=='/'||a=='+'||a=='-')
{
return 1;
}
else
return 0;
}
int isbig(int a,int b)
{
if (a>b)
{
return 1;
}
else
{
return 0;
}
}
int oplevel(char a)
{
switch (a)
{
case'+':
case'-':return 3;
case'*' :
case'/' :return 4;
case'(' :return 2;
case')' :return 1;
default: return -1;
}
}