/*Input
Input contains multiple test cases. The first line of the input is a single
integer T (0<T<1000) which is the number of test cases. T test cases follow.
Each test case contains a char C (+,-,*, /) and two integers
A and B(0<A,B<10000).Of course, we all know that A and B are operands
and C is an operator.
Output
For each case, print the operation result. The result should be rounded to 2
decimal places If and only if it is not an integer.
Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2
Sample Output
3
-1
2
0.50*/
#include <stdio.h>
#include <math.h>
int main()
{
int i,j;
float b1,b2;
float a[10];
double d;
double *e = &d;
char c;
scanf("%d",&i);
for(j = 0;j < i;j++)
{
scanf("%s %f %f",&c,&b1,&b2);
switch(c)
{
case '+':{
a[j] = b1 + b2;
break;
}
case '-':{
a[j] = b1 - b2;
break;
}
case '*':{
a[j] = b1 * b2;
break;
}
case '/':{
a[j] = b1 / b2;
break;
}
}
}
for(j = 0;j < i;j++)
{
if(modf(a[j],e) < 1e-6)
printf("%d\n",(int)a[j]);
else
printf("%.2f\n",a[j]);
}
}
在写这个程序的时候我遇到的问题:
1.当输入格式是%f或者%lf的时候,分配的是浮点型的空间,所以输入int型数据时是按浮点型数据存储的,也就是说会有小数点并且小数点后面全是零。
2.为了方便输出,我用了一个单精度的数组存储得数,但是为了满足题目的输出格式,我使用了modf函数(modf(double val,double *iptr)),使用DEVC++编译器时,我发现val可以是float型,而iptr必须是double型。