标题:关于 c primer plus 8.7程序的疑问
只看楼主
海猫猫
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2016-9-4
结帖率:100%
已结贴  问题点数:20 回复次数:5 
关于 c primer plus 8.7程序的疑问
书上的代码如下:
#include<stdio.h>
#include<stdbool.h>
long gat_long(void);

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

double sum_squares(long a, long b);

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

    printf("This program computes the sum of the squares of "
           "integers in a range.\nThe lower bound should not "
           "be less than -10000000 and\nthe upper bound "
           "should not be more than +10000000.\nEnter the "
           "limits(enter 0 for both limits to quit):\n "
           "lower limit: ");
    start = get_long();
    printf("upper limit: ");
    stop = get_long();
    while (start != 0 || stop ! = 0)
    {
        if (bad_limits(stare, 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 (entre 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 = getchat() != '\n')
            putchar(ch);
        printf(" is not an integer.\nplease enter an ");
        printf("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_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;
}


问题1:关于long get_long(void)这个函数里面的 第二个while什么时候会用到?或者说什么条件下会出发?不是很清楚这个函数的流程....


搜索更多相关主题的帖子: should double include start 
2016-12-03 10:29
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
得分:10 
while (scanf("%ld",&input) != 1)        //当这个读取失败的时候就用到while ((ch = getchat() != '\n')来清除缓冲流里的数据。
                                       //也就说只要 scanf("%ld",&input) != 1条件成立,那么while ((ch = getchat() != '\n')就会运行
    {
        while ((ch = getchat() != '\n')
            putchar(ch);

[此贴子已经被作者于2016-12-3 10:51编辑过]

2016-12-03 10:49
枯_草
Rank: 2
等 级:论坛游民
帖 子:17
专家分:44
注 册:2016-11-12
得分:10 
while (scanf("%ld",&input) != 1)//使用scanf()的返回值作为判断条件,
    {//如果scanf()没有成功读取一个long型值则执行循环
        while ((ch = getchat() != '\n')//用来丢弃输入行的剩余内容
            putchar(ch);//清空缓冲区
2016-12-03 22:24
海猫猫
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2016-9-4
得分:0 
回复 2楼 linlulu001
我可以这么理解吗?输入一个Low scanf返回一个1,然后里面那个while就会启动而且读取这个low并且用putchar输出吗?里面的getchar读取的是上个scanf的Low吗?
2016-12-05 09:36
海猫猫
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2016-9-4
得分:0 
看来我对scanf()和getchar()函数的理解还不够,所以才会导致我问这个问题吧?。。。
2016-12-05 09:43
海猫猫
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2016-9-4
得分:0 
总之谢谢大家的帮助,我自己在琢磨琢磨吧。
2016-12-05 09:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-471921-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.570875 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved