C Primer Plus(第六版编程练习)8.11 编程练习 第5题

#include <stdio.h>
int main (void)
    {
    int guess = 1;
    int i = 0;
    int total = 50;
    int k = 0;
    int j = 50;
    char ch;
    printf("Pick an integer from 1 to 100. I will try to guess ") ;
    printf ("it. \nRespond with a c if my guess is right \n") ;
    printf("is bigger than %d?\n",total);
    while ((ch=getchar())!= 'c')  
    {    
        while((getchar())!='\n')
            continue; //当直接输入两个回车时,程序依然是可执行的.
        if(ch=='y'||ch=='n')//这里是关键!把下面一列的的处理都包含在输入正确的前提上
        {
            if(j!=1)
            {
                j=j/2;
                if(j==0)j=1;
                i=total-j;
                k=total+j;
                printf("ch=%c,total=%d,j=%d,i=%d,k=%d\n",ch,total,j,i,k);
                if (ch == 'y')    
                    {
                    total= k;
                    printf("is bigger than %d?\n",total);
                    }
                else if(ch == 'n')
                    {
                    total= i;
                    printf("is bigger than %d?\n",total);
                    }
            }
            else
            {
                if (ch == 'y')
                {
                    total= total+1;
                    printf("is bigger than %d?\n",total);
                }
                else if(ch == 'n')
                {
                    total= total-1;
                    printf("is bigger than %d?\n",total);
                }
            }
        }
        else continue;//如果最开始的输入是错误的就直接进行下一次循环
    }        
    printf("I knew I could do it! \n") ;
    return 0;
}

相关推荐

  1. C Primer Plus()12.9 编程练习 5

    2024-01-02 19:40:01       46 阅读
  2. C Primer Plus()14.18 编程练习 5

    2024-01-02 19:40:01       50 阅读
  3. C Primer Plus()15.9 编程练习 5

    2024-01-02 19:40:01       48 阅读
  4. C Primer Plus()16.18 编程练习 5

    2024-01-02 19:40:01       48 阅读
  5. C Primer Plus()11.13 编程练习 6

    2024-01-02 19:40:01       43 阅读
  6. C Primer Plus()11.13 编程练习 12

    2024-01-02 19:40:01       60 阅读
  7. C Primer Plus()12.9 编程练习 2

    2024-01-02 19:40:01       47 阅读
  8. C Primer Plus()13.11 编程练习 1

    2024-01-02 19:40:01       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-02 19:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-02 19:40:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-02 19:40:01       82 阅读
  4. Python语言-面向对象

    2024-01-02 19:40:01       91 阅读

热门阅读

  1. 91 两个数组的交集II

    2024-01-02 19:40:01       52 阅读
  2. 用g2o实现bundle adjustment

    2024-01-02 19:40:01       61 阅读
  3. 94. 二叉树的中序遍历(“颜色标记法”)

    2024-01-02 19:40:01       52 阅读
  4. 企微群发助手:企业微信营销的得力助手

    2024-01-02 19:40:01       59 阅读
  5. Web网页开发-初识web-笔记

    2024-01-02 19:40:01       58 阅读
  6. 如何使用版本控制工具(如Git)进行代码管理?

    2024-01-02 19:40:01       57 阅读
  7. uni-app、H5+ 下载并保存、打开文件

    2024-01-02 19:40:01       88 阅读
  8. 1303:鸣人的影分身

    2024-01-02 19:40:01       54 阅读
  9. 预编译仓库中的 Helm Chart

    2024-01-02 19:40:01       66 阅读