#include<stdio.h>
#include<stdbool.h>
/*验证输入为一整数*/
long get_long(void)
{
long input;
char ch;
while (scanf("%ld",&input) != 1)/*输入不是整数*/
{
while ((ch = getchar()) != '\n')
{
putchar(ch);/*输出错误的输入*/
}
printf(" is not an integer.\nPlease enter an integer value,such as 25,-178,or 3: ");
}
return input;
}
/*验证范围上下限是否有效*/
bool bad_limits(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;
}
/*计算整数平方和*/
double sum_squares(long a, long b)
{
double total = 0;
long i;
for (i = a; i
特定范围所有整数平方和(包含判断输入是否正确为整数)
最新推荐文章于 2022-09-07 16:10:50 发布
#include<stdio.h>#include<stdbool.h>/*验证输入为一整数*/long get_long(void){ long input; char ch; while (scanf("%ld",&input) != 1)/*输入不是整数*/ { while ((ch = getchar()) != '\n') { putchar(ch);/*输出错误的输入*/ } printf(" is not an int.
摘要由CSDN通过智能技术生成