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

2024-01-02 19:24:43

#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;
}

文章来源:https://blog.csdn.net/apple_50569014/article/details/135252046
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。