在实际应用中,用户不一定会按照程序的指示行事,当用户输入的值与程序的期望不符实,程序就会出错甚至停止运行。 要解决这样的问题,就需要提前想到程序运行时用户可能会给出什么样的值,然后用关系表达式来解决。
现在需要一个程序,计算特定范围内所有整数的平方和。程序限制范围的上限和下限分别为10000000和-10000000
/*checking.c -- 输入验证*/
#include <stdio.h>
#include <stdbool.h>
//验证输入是否是一个整数
long get_long(void);
//验证范围的上下限是否有效
bool bad_limit(long begin, long end, long low, long high);
//计算范围a~b之间的整数平方和
double sum_squares(long a, long b);
int main (void)
{
const long MAX = -10000000; //上限
const long MIN = +10000000; //下限
long start;
long stop;
double answer;
printf("This program computes the sum of the squares of integers in a range.\n"
"The lower dound should not be less than -10000000 and the upper bound\n"
"should not be more than +10000000.\n"
"Enter the limits (enter 0 for both linits ro quit):\n");
printf("lower limit:");
start = get_long();
printf("upper limit:");
stop = get_long();
while (start != 0 || stop != 0){
if(bad_limit(start, stop, MIN, MAX)){
printf("Please try again.\n");
}else{
answer = sum_squares(start, stop);
printf("The sum of the squares of the integers");
printf("from %ld to %ld is %g\n", start, stop, answer);
}
printf("Enter the limits (enter 0 for both limits to quit):\n");
printf("lower limit:");
start = get_long();
printf("upper limit:");
stop = get_long();
}
printf("Done.\n");
return 0;
}
long get_long(void){
long input;
char ch;
while(scanf("%ld", &input) != 1){
while((ch = getchar()) != 1){
putchar(ch); //处理错误输入
}
printf("is not an integer.\n");
printf("Please enter an integer value, such as 25, -178, or 3:");
}
return input;
}
double sum_squares(long a, long b){
double total = 0;
long i;
for(i = a; i <= b; i++){
total += (double) i * (double) i;
}
return total;
}
bool bad_limit(long begin, long end, long low, long high){
bool not_good = false;
if(begin > end) {
printf("%ld isn't smaller than %ld.\n", begin, end);
not_good = true;
}
if(begin < low || end < low){
printf("Values must be %ld or greater.\n", low);
not_good = true;
}
if(begin > high || end > high){
printf("Values must be %ld or less.\n", high);
not_good = true;
}
return not_good;
}
This program computes the sum of the squares of integers in a range.
The lower dound should not be less than -10000000 and the upper bound
should not be more than +10000000.
Enter the limits (enter 0 for both linits ro quit):
lower limit: low
lowis not an integer.
Please enter an integer value, such as 25, -178, or 3: 3
upper limit: a big number
a big numberis not an integer.
Please enter an integer value, such as 25, -178, or 3: 12
Values must be 10000000 or greater.
Values must be -10000000 or less.
Please try again.
Enter the limits (enter 0 for both limits to quit):
lower limit: 80
upper limit: 10
80 isn't smaller than 10.
Values must be 10000000 or greater.
Values must be -10000000 or less.
Please try again.
Enter the limits (enter 0 for both limits to quit):
lower limit: 0
upper limit: 0
Done.
虽然程序的核心部分只有很短的一部分(sum_squares()函数),他主管了程序的计算,但是一个程序的完整还需要其他部分的配合,比如判断什么时候 程序可以进行计算,什么时候要对用户说什么都需要靠那些来完成。
程序遵循模块化思想,使用模块化思想,使用独立的函数来验证管理显示。程序越大,使用模块化就越重要。
程序由main()函数管理程序流,为其他函数委派任务。它使用get_long()获取值、while循环处理值、badlimits()函数检查值是否有效、sum_squres()函数处理实际计算。