3.4 设计程序
在第3章的最后,要应用所学的内容,建立一个有用的程序。
3.4.1 问题
我们要编写一个简单的计算器,进行加、减、乘、除操作,在执行除操作时,还要确定其余数。这个程序必须能以自然的方式进行计算,例如5.6 * 27 或3 + 6。
3.4.2 分析
本程序涉及的所有数学知识都很简单,但输入过程会增加复杂性。我们需要检查输入,确保用户没有要求计算机完成不可能的任务。还必须允许用户一次输入一个计算式,例如:
34.87 + 5
或者
9 * 6.5
编写这个程序的步骤如下:
(1) 获得用户要求计算机执行计算所需的输入。
(2) 检查输入,确保输入是可以理解的。
(3) 执行计算。
(4) 显示结果。
3.4.3 解决方案
本节列出解决该问题的步骤。
1. 步骤1
获得用户输入是很简单的,可以使用printf()和scanf(),所以需要<stdio.h>头文件。这里介绍的唯一的新知识是获得输入的方式。如前所述,可以让用户更自然地输入每个数字和要执行的操作,而不是逐个输入它们。可以这么做是因为scanf()允许这么做,这里在列出程序的第一部分之后讨论其细节。下面是读取输入的程序代码:
/*Program 3.11 A calculator*/
#include <stdio.h>
int main(void)
{
double number1 = 0.0; /* First operand value a decimal number */
double number2 = 0.0; /* Second operand value a decimal number */
char operation = 0; /* Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
/* Plus the rest of the code for the program */
return 0;
}
scanf()函数在读取数据方面相当聪明。其实并不需要在一行上输入每个数据项,只要在输入的每一项之间流出一个或多个空白即可。(按空格键、Tab键或回车键,都可以创建空白字符)。
2. 步骤2
接着,检查输入是否正确。最明显的检查是要执行的操作是否有效。有效的操作有+、–、/和 %,所以需要检查输入的操作是否是其中的一个。
还需要检查第二个数字,如果操作是/或 %,第二个数字就不能是0。如果右操作数是0,这些操作就是无效的。这些操作都可以使用if语句来完成,switch语句则为此提供了一种更好的方式,因为它比一系列if语句更容易理解。
/*Program 3.11 A calculator*/
#include <stdio.h>
int main(void)
{
double number1 = 0.0; /* First operand value a decimal number */
double number2 = 0.0; /* Second operand value a decimal number */
char operation = 0; /* Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
/* Code to check the input goes here */
switch(operation)
{
case '+': /* No checks necessary for add */
break;
case '-': /* No checks necessary for subtract */
break;
case '*': /* No checks necessary for multiply */
break;
case '/':
if(number2 == 0) /* Check second operand for zero */
printf("\n\n\aDivision by zero error!\n");
break;
case '%': /* Check second operand for zero */
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
break;
default: /* Operation is invalid if we get to here */
printf("\n\n\aIllegal operation!\n");
break;
}
/* Plus the rest of the code for the program */
return 0;
}
当运算符是%时,将第二个操作数转换为一个整数,所以仅检查第二个操作数是否为0是不够的,还必须检查number2在转换为long时,其值是否为0。
3. 步骤3和4
检查了输入后,就可以计算结果了。这里有一个选择。可以在switch中计算每个结果,存储它们,在执行完switch后输出它们,也可以在每个case中输出结果。这里采用第二种方式。需要添加的代码如下:
/*Program 3.11 A calculator*/
#include <stdio.h>
int main(void)
{
double number1 = 0.0; /* First operand value a decimal number */
double number2 = 0.0; /* Second operand value a decimal number */
char operation = 0; /* Operation - must be +, -, *, /, or % */
printf("\nEnter the calculation\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
/* Code to check the input goes here */
switch(operation)
{
case '+': /* No checks necessary for add */
printf("= %lf\n", number1 + number2);
break;
case '-': /* No checks necessary for subtract */
printf("= %lf\n", number1 - number2);
break;
case '*': /* No checks necessary for multiply */
printf("= %lf\n", number1 * number2);
break;
case '/':
if(number2 == 0) /* Check second operand for zero */
printf("\n\n\aDivision by zero error!\n");
else
printf("= %lf\n", number1 / number2);
break;
case '%': /* Check second operand for zero */
if((long)number2 == 0)
printf("\n\n\aDivision by zero error!\n");
else
printf("= %ld\n", (long)number1 % (long)number2);
break;
default: /* Operation is invalid if we get to here */
printf("\n\n\aIllegal operation!\n");
break;
}
return 0;
}
注意,在执行取模运算时,将两个数字从double转换为long。这是因为在C语言中,%运算符只能用于整数。剩下的就是试运行代码了,下面是输出:
Enter the calculation
25*13
= 325.000000
下面是另一个例子:
Enter the calculation
999/3.3
= 302.727273
下面是另一个例子:
Enter the calculation
7%0
Division by zero error!





