| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1150 人关注过本帖
标题:c primer plus 程序清单8.8 分析不出 请教
只看楼主 加入收藏
wjl0522
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2013-7-16
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:9 
c primer plus 程序清单8.8 分析不出 请教
程序清单8.8 菜单技术
#include <stdio.h>
char get_choice (void):
char get_first (void):
int get_int (void):
void count(void):
int main (void)
{
    int choice;
    void count (void);
   
    while ((choice = getchoice ()) != 'g')
    {
        switch
        {
        case 'a':printf("Buy low.sell high\n");
             break;
        case 'b':ptuchar('\a'):
             break;
        case 'c':count();
             break;
        defalut:printf("program error!\n");
             break;
        }
    }
    printf("Bye .\n");
    return 0;
}
void count (void)
{
    int n,i;
    printf("count how far?Enter an integer:\n");
    n = get_int();
    for (i = 1;i <= n; i++)
    printf("%d\n",i);
    while (getchar() != '\n')
    continue;
}

char get_choice(void)
{
    int ch;
    printf("Enter the letter of your choice: \n");
    printf("a.advice    b.bell\n");
    printf("c.count        d.quit\n");
    ch = get_first();
    while ((ch < 'a' || ch > 'c')&& ch != 'q' )
    {
        printf("please respond with a,b,c,or q.\n");
        ch = get_first;
    }
    return ch;
}

char get_first(void)
{
    int ch;
   
    ch = getchar();
    while (getchar() != ‘\n’)
        continue;
    return ch;
}

int get_int (void)
{
    int input;
    char ch;
    while (scanf("%d",&input) != 1)
    {
        while((ch = getchar()) != '\n')
        putchar(ch);
        printf("  is not an integer.\nplease enter an");
        printf("integer value,such as 25,-178,pr 3: ");
    }
    return input:
}
搜索更多相关主题的帖子: include choice count 
2013-07-16 14:33
wjl0522
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2013-7-16
收藏
得分:0 
//分析
//第一次输入a
//第二次输入count
//第三次输入d
//第四次输入q

--------------------------------------------------------------------
//那么开始分析
//第一次
//进入get_choice 将值赋予给choice
                                                    Enter the letter of your choice:
                                                    a.advice    b.bell
                                                    c.count        d.quit
                                                    a           
//进入get_first将值赋予给ch

^^^^^^^^^^
//int ch;        //get_first专属ch 独立的 跟其他的同名不同人
//ch = getchar();    //因为是空 但我不知到应该不应该载入我先前输入的a  假设载入a 执行下一段
//while (getchar() != ‘\n’)    //当a不等于换行 真 ..执行下一段   如果是我翻译的意思应该是while (ch != '\n')
//continue;    //break是跳出当前循环.continue是重新循环...唉...啥意思呢
//return ch;    //将a保存赋予给ch  get_first == ch;
^^^^^^^^^^

//while ((ch < 'a' || ch > 'c')&& ch != 'q' ) //判断为假,跳过
//将a赋予给ch     get_choice == ch
//while ((choice = getchoice ()) != 'g')    //a != g  真 执行下一段
//switch (a)                                               
                                                    Buy low.sellhigh
//跳出switch重新到while ((choice = getchoice ()) != 'g')

//第二次
                                                    Enter the letter of your choice:
                                                    a.advice    b.bell
                                                    c.count        d.quit
                                                   
                                                    count   
                                                      
//进入get_first
//感觉有点糊涂 思路不清晰 前面分析就感觉有错 先将错纠错吧...
//虽然输入是count但是因为是char 只能保存一个c
//回到get_choice进行下一判断
//while ((ch < 'a' || ch > 'c')&& ch != 'q' )    //假 跳过 执行下一语句
//switch(c)   

//进入 count()
                                                    count how far?Enter an integer

//进入 get_int
//很混乱了
//当count != 1   
//当count != \n 真
//我给出的答案是错的 但先这样将错纠错吧..
                                                    count is not an integer
                                                    please enter an integer value,such as 25,-178,or                                                         3:xx
//因为get_int是整数型 count为5个字母                                    12345
//while (getchar != '\n')  当输入的count不等于换行 跳出 回到最初循环...
                                                                                                           

  
2013-07-16 14:34
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:0 
二楼的分析很了然了,按照他说的理解即可

思考赐予新生,时间在于定义
2013-07-16 15:56
wjl0522
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2013-7-16
收藏
得分:0 
回复 3楼 love云彩
....二楼和一楼都是我啊...我分析的有错误  所以才想请教下
2013-07-16 17:19
q13678986740
Rank: 4
等 级:业余侠客
帖 子:114
专家分:245
注 册:2012-7-7
收藏
得分:10 
ch=getchar();
while (getchar() != '\n')
    continue; 
假设你输入ahead,第一个getchar()读取a后赋值给ch,输入流剩下的head\n 逐个被循环中的getchar()读取,在continue的作用下跳过本次循环,那就相当于每读取一个丢弃一个,直到回车结束。最终ch只保留第一个字符,剩下的字符都被丢弃。
2013-07-16 22:21
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:0 
你哪一步不明白的?

思考赐予新生,时间在于定义
2013-07-16 22:31
wjl0522
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2013-7-16
收藏
得分:0 
回复 5楼 q13678986740
我又反复思考
本来return ch
是保存 ahead
但是因为char get_first 是char 保存单个字符
所以最后结果是只保存了一个a 不知道这样理解对不对
2013-07-17 17:07
wjl0522
Rank: 1
等 级:新手上路
帖 子:17
专家分:2
注 册:2013-7-16
收藏
得分:0 
回复 6楼 love云彩
对不起回复的比较晚

void count (void)
{
    int n,i;
    printf("count how far?Enter an integer:\n");
    n = get_int();
    for (i = 1;i <= n; i++)
    printf("%d\n",i);
    while (getchar() != '\n')
    continue;
}
int get_int (void)
{
    int input;
    char ch;
    while (scanf("%d",&input) != 1)
    {
        while((ch = getchar()) != '\n')
        putchar(ch);
        printf("  is not an integer.\nplease enter an");
        printf("integer value,such as 25,-178,pr 3: ");
    }
    return input:
}

//这一段
//假设输入count
//进入case c
count how far?Enter an integer:
//进入get_int
//判断while (scanf("%d",&input) != 1)
//c != 1    o != 1    u != 1 n!= 1 t !=
//while((ch = getchar()) != '\n')
//count 都不等于换行符
//显示count is not an integer.            //书上是 two is not an integer 疑问就在这
//please enter an integer value,such as 25,-178,pr 3:

其他地方我叶不知道分析的对不对请指教
2013-07-17 17:19
q13678986740
Rank: 4
等 级:业余侠客
帖 子:114
专家分:245
注 册:2012-7-7
收藏
得分:0 
回复 7楼 wjl0522
其实我也是用这本书,忘记在哪里会讲到输入流的问题了。据我的理解是,当你键入一串字符时,getchar()读取第一个字符,剩下的字符停留在输入流中,如果再有个getchar()那么你就不需要输入,他会自动读取输入流中剩下字符的第一个。 也就是说输入的一串字符会保留在输入流中,然后根据输入语句的需要进行读取的。
2013-07-18 10:17
q13678986740
Rank: 4
等 级:业余侠客
帖 子:114
专家分:245
注 册:2012-7-7
收藏
得分:0 
回复 6楼 love云彩
再补充下,ch只是读取了ahead里面的首个字符 。因为剩下的字符存在输入流中,进入while循环后逐个被getchar读取,而你没有将其返回值赋值给ch,也就是说没对他操作,那么字符就被读取丢弃了。
2013-07-18 10:23
快速回复:c primer plus 程序清单8.8 分析不出 请教
数据加载中...
 
   



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

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