主题:一道表达式求值的题目
Give you a arithmetic expression and you should calculate its result.
You are to write a program that calculate the given expression . For instance, if the expression is 1+12+123*0, you should print its result 13.
Input
There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing only operators (+, -, *). Every expression just contains four numbers and three operators.Multiplication have higher priority then subtraction and addition. All operations with the same priority are computed from left to right . There are no spaces inside the expressions.
Output
Print a single line for every expression. The line must contain the result of the expression.
Sample Input
3
1+12+123*0
0*12+1+123
123-123123+1*99
Sample Output
13
124
-122901
我是这样写的。。。。
#include <stdio.h>
int main()
{
long n, j, i1, i2, i3, i4;
char a, b, c;
scanf("%d",&n); getchar();
for (j=0; j<n; ++j){
scanf("%d%c%d%c%d%c%d", &i1, &a, &i2, &b, &i3, &c, &i4);
if (a=='*') { i2*=i1; i1=0; }
if (b=='*') { i3*=i2; i2=0; }
if (c=='*') { i4*=i3; i3=0; }
if (a=='-') { i2=0-i2; }
if (b=='-') { i3=0-i3; }
if (c=='-') { i4=0-i4; }
printf("%d\n", i1+i2+i3+i4);
}
return 0;
}
但是它给我wrong answer。。。不晓得哪里有问题。。。请大家帮忙看看
You are to write a program that calculate the given expression . For instance, if the expression is 1+12+123*0, you should print its result 13.
Input
There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing only operators (+, -, *). Every expression just contains four numbers and three operators.Multiplication have higher priority then subtraction and addition. All operations with the same priority are computed from left to right . There are no spaces inside the expressions.
Output
Print a single line for every expression. The line must contain the result of the expression.
Sample Input
3
1+12+123*0
0*12+1+123
123-123123+1*99
Sample Output
13
124
-122901
我是这样写的。。。。
#include <stdio.h>
int main()
{
long n, j, i1, i2, i3, i4;
char a, b, c;
scanf("%d",&n); getchar();
for (j=0; j<n; ++j){
scanf("%d%c%d%c%d%c%d", &i1, &a, &i2, &b, &i3, &c, &i4);
if (a=='*') { i2*=i1; i1=0; }
if (b=='*') { i3*=i2; i2=0; }
if (c=='*') { i4*=i3; i3=0; }
if (a=='-') { i2=0-i2; }
if (b=='-') { i3=0-i3; }
if (c=='-') { i4=0-i4; }
printf("%d\n", i1+i2+i3+i4);
}
return 0;
}
但是它给我wrong answer。。。不晓得哪里有问题。。。请大家帮忙看看