Input Input contains multiple test cases.

/*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型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值