首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c primer 中有个程序的函数部分不是太清楚,麻烦大家给讲解上,多谢

2012-09-22 
c primer 中有个程序的函数部分不是太清楚,麻烦大家给讲解下,谢谢!这个程序的流程 看的有点晕乎 ,三个函数

c primer 中有个程序的函数部分不是太清楚,麻烦大家给讲解下,谢谢!
这个程序的流程 看的有点晕乎 ,三个函数也不知道具体含义 
/* checking.c -- validating input */
#include <stdio.h>
#include <stdbool.h>
// validate that input is an integer
int get_int(void);
// validate that range limits are valid
bool bad_limits(int begin, int end, int low, int high);
// calculate the sum of the squares of the integers
// a through b
double sum_squares(int a, int b);
int main(void)
{
  const int MIN = -1000; // lower limit to range
  const int MAX = +1000; // upper limit to range
  int start; // start of range
  int stop; // end of range
  double answer;
   
  printf("This program computes the sum of the squares of "
  "integers in a range.\nThe lower bound should not "
  "be less than -1000 and\nthe upper bound should not "
  "be more than +1000.\nEnter the limits (enter 0 for "
  "both limits to quit):\nlower limit: ");
  start = get_int();
  printf("upper limit: ");
  stop = get_int();
  while (start !=0 || stop != 0) 
  {
  if (bad_limits(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 %d to %d is %g\n", start, stop, answer);
  }
  printf("Enter the limits (enter 0 for both "
  "limits to quit):\n");
  printf("lower limit: ");
  start = get_int();
  printf("upper limit: ");
  stop = get_int();
  }
  printf("Done.\n");
   
  return 0;
}

int get_int(void)
{
int input;
char ch;

while (scanf("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}

return input;
}

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

for (i = a; i <= b; i++)
total += i * i;

return total;
}

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

if (begin > end)
{
printf("%d isn't smaller than %d.\n", begin, end);
not_good = true;
}
if (begin < low || end < low)
{
printf("Values must be %d or greater.\n", low);
not_good = true;
}
if (begin > high || end > high)
{
printf("Values must be %d or less.\n", high);
not_good = true;
}

return not_good;
}
还有这个return 啥意思,while (scanf("%d", &input) != 1) 这句 的具体含义是什么呢,我分不清楚scanf语句什么时候是返回值什么时候是 赋值

谢谢大家!!!

[解决办法]
return 向函数返回一个值
scanf的返回值是成功赋值后的值,就是input的值
[解决办法]
while (scanf("%d", &input) != 1)

这句话未来判断while循环条件,执行 scanf 函数。

通过scanf input如果正确接收了一个整型的值,,,,

scanf 函数返回1,那么while 循环不执行。



如果收到一个 字符型或其他,,不是整型,scanf返回0,,执行while循环,进行输入出错处理

关于scanf 



[解决办法]

C/C++ code
bool bad_limits(int begin, int end, int low, int high){bool not_good = false;//。。。。。。return not_good;//向该函数返回一个值,并且结束本次函数执行cout<<"Hello "<<endl;//本函数这句话不会输出} 

热点排行