我不知道是否有人问过这个问题,但是这是我的: 如果我有以下代码;总价值是多少? 因为输出总是在+ ve值而不是-ve,或者请指导我在哪里错了.Thankyou。 P.S:Iam使用Turbo C 3.o编译器。
void subtract (void)
{
float f1;
float f2=0.0;
float f3=0.0;
float total;
printf("Enter numbers to be subtract:'q' to quit.\n ");
while (scanf("%f",&f1)==1)
{
f3=f1+f2;
total=f3-f1;
printf("Enter another # to be subtract:'q' to quit.\n ");
scanf("%1.0f",&f1);
}
printf("Subtraction Total = %1.0f",total);
getch();
}
//我现在正在使用像3.6-9.2这样的简单减法,我没有得到-5.6而是得到9(这个例子)
void subtract (void)
{
float f1;
float f2;
int status1,status2;
float total;
printf("Enter first number to subtract:'n' to quit.\n ");
status1=scanf("%f",&f1);
printf("Enter second number to be subtract from first:'n' to quit.\n ");
status2=scanf("%f",&f2);
while (status1==1 && status2==1)
{
total = f1 - f2;
printf("total=%1.2f \n",total);
printf("Enter first number to subtract:'n' to quit.\n ");
status1=scanf("%f",&f1);
printf("Enter second number to be subtract from first:'q' to quit.\n ");
status2=scanf("%f",&f2);
}
printf("Subtraction Total = %1.1f",total);
getch();
}
答案 0 :(得分:3)
列出所有内容的问题太多了。但是,这里有一些让你开始:
f2
永远不会设置为零以外的任何内容。scanf()
两次,两者都试图读入相同的变量。printf()
中使用的格式说明符。答案 1 :(得分:0)
我通过以下方式完成了我想要的事情;谢谢大家
void减法(无效) { float f1; 浮f2; int status1,status2; 浮动总数;
printf("Enter first number to subtract:'n' to quit.\n ");
status1=scanf("%f",&f1);
printf("Enter second number to be subtract from first:'n' to quit.\n ");
status2=scanf("%f",&f2);
while (status1==1 && status2==1)
{
total = f1 - f2;
printf("total=%1.2f \n",total);
printf("Enter first number to subtract:'n' to quit.\n ");
status1=scanf("%f",&f1);
printf("Enter second number to be subtract from first:'q' to quit.\n ");
status2=scanf("%f",&f2);
}
printf("Subtraction Total = %1.1f",total);
getch();
}