【发布时间】:2017-09-06 05:57:12
【问题描述】:
我必须用 C 语言编写一个程序,它可以获取未定义数量的双精度值,并将它们全部打印出来,根据数字增加一定的百分比。当用户输入负值时程序应该停止。当我使用少量数字时一切正常,但当输入包含大量数字时,程序只打印最后一个。
这是我的代码:
#include <stdio.h>
int main()
{
double ins=0; //the input
while(ins>=0){
scanf("%lf",&ins);
if(ins<0){break;}
else{
if(ins<500){printf("%.2lf ",ins*1.15);}
else if(ins<=1000){printf("%.2lf ",ins*1.10);}
else {printf("%.2lf ",ins*1.05);}
}
}
return 0;
}
附加信息:使用 GCC 编译器。
程序应为特定输入提供的输出示例。
Input:
4003.31 1212.35 3414.31
4257.1 1394.37 1217.28
3602.85 4218.58 4994.8
1133.82 1086.48 2117.43
2253.86 3827.71 2170.16
1161.27 3069.77 1338.08
2791.99 3709.33 180.43
4555.77 318.58 1912.24
158.68 2106.49 4439.56
1247.34 -0.79
Output I should get:
4203.48 1272.97 3585.03 4469.96
1464.09 1278.14 3782.99 4429.51
5244.54 1190.51 1140.80 2223.30
2366.55 4019.10 2278.67 1219.33
3223.26 1404.98 2931.59 3894.80
207.49 4783.56 366.37 2007.85 182.48
2211.81 4661.54 1309.71
我可以怎样做才能使程序正常工作,不仅是少量,而且像上面这样的数量?
编辑:我通过上述输入得到的输出是“1309.71”,这只是我应该收到的完整输出的最后一个数字。
【问题讨论】:
-
but when the input consists of a larger amount of numbers the program prints just the last ones- 你的问题对我来说并不完全可以理解/清楚。你能提供你目前得到的输出吗? -
只是我应该收到的完整输出的最后一个数字。在这种情况下 1309.71
-
您是否尝试过使用调试器 - 以便找出输入丢失的位置?因为代码不是很大,应该不难找出来。
-
就我而言,代码似乎按预期工作。