#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[1005];
char changestr[2010];
double stack_n[1005];
char stack_c[1005];
int count_c = -1;
int count_n = -1;
char pop_c(){
return stack_c[count_c--];
}
char top_c(){
if(-1 == count_c)
return 0;
return stack_c[count_c];
}
void push_c(char t){
stack_c[++count_c]=t;
}
double top_n(){
if(-1 == count_n)
return 0;
return stack_n[count_n];
}
double pop_n(){
return stack_n[count_n--];
}
void push_n(double t){
stack_n[++count_n]=t;
}
int level(char ch){
switch(ch)
{
case '+':
case '-':return 1;
case '*':
case '/':return 2;
default:
return 0;
}
}
void change()
{
int i;
int j=0;
for(i=0; i<strlen(str);)
{
switch(str[i])
{
case '(':push_c('(');i++;break;
case ')':
while(top_c()!='('&&count_c!=-1)
{
changestr[j++] = pop_c();
changestr[j++] = ' ';
}
pop_c();
i++;break;
case '+':
case '-':
case '*':
case '/':
while(level(str[i])<=level(top_c()))
{
changestr[j++] = pop_c();
changestr[j++] = ' ';
}
push_c(str[i]);
i++;break;
case '=':i++;break;
default:
{
while(str[i]<='9'&&str[i]>='0'||str[i]=='.')
changestr[j++] = str[i++];
changestr[j++] = ' ';
}
}
}
while(count_c!=-1)
{
changestr[j++] = pop_c();
changestr[j++] = ' ';
}
}
double result(){
int i;
int len = strlen(changestr);
double x;
double y;
for(i=0; i<len; i++)
{
switch(changestr[i])
{
case '+':x = pop_n();x += pop_n();i++;break;
case '-':x = pop_n();x = pop_n()-x;i++;break;
case '*':x = pop_n();x *= pop_n();i++;break;
case '/':x = pop_n();x = pop_n()/x;i++;break;
default:
x = 0;
while(changestr[i]<='9'&&changestr[i]>='0')
{
x= x*10 + changestr[i] - '0';
i++;
}
if(changestr[i] == '.')
{
double k = 10.0;
y = 0;
i++;
while(changestr[i]<='9'&&changestr[i]>='0')
{
y+=((changestr[i]-'0')/k);
i++;
k*=10;
}
x+=y;
}
}
push_n(x);
}
return top_n();
}
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
count_c = -1;
count_n = -1;
scanf("%s",str);
change();
printf("%.2f\n",result());
}
return 0;
}
网友评论