【发布时间】:2014-01-10 09:33:02
【问题描述】:
我正在使用 C 编写一个计算器程序
/*
* sum.c
ch 2
1.Write a program in “SUM.C” which reads two integers and prints out the sum,
the difference and the product. Divide them too, printing your answer to two
decimal places. Also print the remainder after the two numbers are divided.
Introduce a test to ensure that when dividing the numbers, the second number
is not zero.
What happens when you add two numbers and the sum is too large to fit into
the data type you are using? Are there friendly error messages?
*
* Created on: Jan 10, 2014
* Author: salahuddin
*/
#include<stdio.h>
int main(void)
{
int first,second;
printf("Please enter two number a,b");
scanf("%i,%i",&first,&second);
printf("sum=%i, difference=%i, product=%i, ",
first+second,first-second,first*second);
if(second!=0)
printf("division=%.2lf, ",(double)first/(double)second);
else
printf("remainder=%i",first%second);
return 0;
}
输入除 0 以外的数字时效果很好 当我输入 2,0 作为输入时,什么都没有出现
我尝试使用 eclipse 中的调试器对其进行调试,这两个变量的值为 2,0 但是不打印计算结果直接退出程序?
谁能说出问题出在哪里?
【问题讨论】:
-
@A4L - OP 在这里谈论 2 个数字,而不是一个浮点数。
-
您需要处理 所有 NaN 案例,例如1/0 1%0 等
-
我输入了 2,0,因为两个输入必须用 , 分隔,
-
@OliCharlesworth 2 % 0 在哪里?
-
@Salahuddin 在这里:
first%second