• 一个笨拙的猜数游戏代码参考


    直接上代码!!!

    #include <stdio.h>
    #include <stdlib.h>
    
    #define TOP 1000
    #define BOTTOM 0
    
    /*
        由Mr.Blue and Mr.Green制作于2016.7.31 21:17
        本程序采用块状分段,使程序更加简单,但可读性降低,望见谅
    */
    
    int main(int argc, char * argv[])
    {
        int toobig, toosmall, temp;
        char input;
        printf("这是一个笨拙的猜数游戏.请在心里记下一个0~1000之间的数,然后按任意键开始.\n");
        getch();
        //--------------------猜测块---------------------------------------------------------
        passing:{
            toobig = TOP;
            toosmall = BOTTOM;
            temp = (TOP - BOTTOM) / 2;
            while(1){
                printf("%d太大,太小或刚好(b,s or r)\n",temp);
                switch( (input = getchar() ) ){
                    case 'b':{
                        toobig = temp; //将toobig置为temp,因为temp太大
                        temp = temp - (temp - toosmall) / 2; //如果太大,则temp向toosmall折半
                        break;
                    }
                    case 's':{
                        toosmall = temp; //将toosmall置为temp,因为temp太小
                        temp = temp + (toobig - temp) / 2; //如果太小,则temp向toobig折半
                        break;
                    }
                    case 'r':
                        goto loop; //如果正确,跳至结果块
                    default :{ //如果输入有误,重新输入
                        printf("请输入正确选项!\n");
                        fflush(stdin); //清空缓冲区防止getchar()误读,后面做法一致
                        continue;
                        }
                }
                if(temp == toobig || temp == toosmall){ //让程序更聪明
                    printf("敢玩我?嘿嘿,被我看出来了!\n");
                    goto restart;
                }
                fflush(stdin);
            }
        }
        //---------------------结果块-----------------------------------------------
        loop:{ 
            printf("我就知道我会猜出来\n");
        }
        //-------------------------重置块----------------------------------------------
        restart:{ 
            fflush(stdin);
            printf("重新开始吗?(y or n)\n");
            input = getchar();
            if(input == 'y'){ //如果要重玩,就重玩
                fflush(stdin);
                goto passing;
            }
            else if(input == 'n') //不想就关了吧
                exit(0);
            else{ //如果用户不小心输入错误,重新询问
                printf("请输入正确的选项!\n");
                goto restart;
            }
        }
        /////////////////////////////////////////////////////////////////////
        return 0;
    }

    运行截图

      我猜89

      

      我猜873

      

      我玩它

      

  • 相关阅读:
    生成指定规模大小的redis cluster对关系
    根据ip列表模拟输出redis cluster的主从对应关系
    获取redis实例绑定cpu的情况
    通过shell模拟redis-trib.rb info的输出
    又一种获取redis cluster slots分布的小脚本
    第3种方法获取redis cluster主从关系
    Lamada
    Linq笔记
    推荐博文
    SQL server 自带存储过程 sp_rename
  • 原文地址:https://www.cnblogs.com/mrblug/p/5723978.html
Copyright © 2020-2023  润新知