C语言代码纠错:计算整数平方和的程序

本文将分析一段用于计算给定范围内整数平方和的C语言代码,指出其中的错误并提供解决方案。

**原始代码:**c#include <stdio.h>#include <stdbool.h>long get_long(void);

bool bad_limits(long begin, long end, long low, long high);

double sum_squares(long a, long b);

int main(){ const long MIN = -1000000L; const long MAX = +1000000L; long start; long stop; double answer; printf('This program computes the sum of the squares of integers in a range. The lower bound should not be less than -1000000 and the upper bound should not be more than 1000000. Enter the limits (enter 0 for both limits to quit): '); printf('low limit: '); start = get_long(); printf('upper limits: '); stop = get_long(); while (start != 0|| stop != 0){ if (bad_limits(start,stop,MIN,MAX)) printf('Please try again. '); else { answer = sum_squares(start, stop); printf('The sum of the squares of the intesgers '); printf('from %ld to %ld is %g ',start,stop,answer); } printf('Enter the limits (enter 0 for both limits to quit): low limits: '); start = get_long(); printf('upper limit: '); stop = get_long(); } printf('Done !'); return 0;} double sum_squares(long a, long b){ double total =0; int i; for(i=a;i<=b:i++){ total += (double)i * (double)i; } return total; } bool bad_limits(long begin, long end, long low, long high){ bool not_good = false; if(begin>end){ printf('%ld is not smaller than %ld. ', begin ,end); not_good=true; } if (begin<low||end<low){ printf('Values too small.'); not_good = true; } if (begin>high||end>high){ printf('Values too big.'); not_good = true; } return not_good; }

错误分析:

  1. sum_squares 函数中的循环条件错误: for(i=a;i<=b:i++) 应该改为 for(i=a; i<=b; i++),使用分号 ; 分隔循环条件。2. main 函数中缺少引号: 第32行和第39行的 printf 语句中,字符串末尾缺少一个单引号。3. get_long 函数缺失: 代码中没有提供 get_long 函数的定义,需要补充该函数的实现。

**修正后的代码:**c#include <stdio.h>#include <stdbool.h>

long get_long(void);bool bad_limits(long begin, long end, long low, long high);double sum_squares(long a, long b);

int main() { const long MIN = -1000000L; const long MAX = +1000000L; long start; long stop; double answer;

printf('This program computes the sum of the squares of integers in a range.

'); printf('The lower bound should not be less than -1000000 and '); printf('the upper bound should not be more than 1000000. '); printf('Enter the limits (enter 0 for both limits to quit): '); printf('low limit: '); start = get_long(); printf('upper limit: '); stop = get_long();

while (start != 0 || stop != 0) {        if (bad_limits(start, stop, MIN, MAX))            printf('Please try again.

'); else { answer = sum_squares(start, stop); printf('The sum of the squares of the integers '); printf('from %ld to %ld is %g ', start, stop, answer); } printf('Enter the limits (enter 0 for both limits to quit): low limits: '); start = get_long(); printf('upper limit: '); stop = get_long(); }

printf('Done!');    return 0;}

double sum_squares(long a, long b) { double total = 0; int i;

for (i = a; i <= b; i++) {        total += (double)i * (double)i;    }    return total;}

bool bad_limits(long begin, long end, long low, long high) { bool not_good = false;

if (begin > end) {        printf('%ld is not smaller than %ld.

', begin, end); not_good = true; } if (begin < low || end < low) { printf('Values too small. '); not_good = true; } if (begin > high || end > high) { printf('Values too big. '); not_good = true; }

return not_good;}

long get_long(void) { long input; char ch;

while (scanf('%ld', &input) != 1) {        while ((ch = getchar()) != '

') putchar(ch); printf(' is not an integer. Please enter an integer value, such as 25, -178, or 3: '); } return input;}

总结:

通过仔细分析代码并进行必要的修正,我们可以修复这段C语言代码中的错误,使其能够按照预期计算给定范围内整数的平方和。

C语言代码纠错:计算整数平方和的程序

原文地址: https://www.cveoy.top/t/topic/NQC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录