| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1770 人关注过本帖
标题:一个计算小游戏
只看楼主 加入收藏
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
结帖率:94.74%
收藏
 问题点数:0 回复次数:6 
一个计算小游戏
/*
前两天写了一个这样的程序,有很多不足之处。在几位热心朋友的建议与帮助下,新的程序终于诞生了。
它虽然很小,也很简单,但这也是我辛苦的成果。希望诸位能够喜欢。
*/



#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char YN[100]={"您错了。请问您是放弃这道题,还是更改答案?您有一次更改的机会\n"
              "按N退出这道题,按其它键更改。\n"};
char dui[100]={"恭喜您,答对了!\n"};
char caidan[3][100]={{"***********************************************************************\n"},
                     {"您好,欢迎您来玩这个小游戏,它将会验证您的计算能力。 \n"},
                     {"对不起,您输入的字符非法,请重新输入。\n"}};
char jisuanfu[30]={'+','-','*','/','/','/','*','+','+','-','/','*','/','+','-','-','+','*','*','/','/','+','+','-','-','-','*','*','*','*'};

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~菜单*/
char caidan1()
{
        char x;
        char y[]={"请您用大写字符输入您的选择:\n"};
        char caidan_1[100]={"\t\n\tA:初级\n\tB:中级\n\tC:高级\n\tD:游戏说明\n\tE:退出\n\n\n"};
        system("cls");
        printf("%s\t%s\n\n",caidan[0],caidan[1]);
        printf("%s\n",caidan_1);
        printf("\n%s\n\n\n\t%s ",caidan[0],y);
        do
        {
                 scanf("  %c",&x);
                 if(x<65||x>69)
                         printf("\n\t%s\n\t%s",caidan[2],caidan[0]);
        }
        while(x<65||x>69);
        if(x=='E')
                exit(0);
        if(x=='D')
                shuoming();
        if(x>=65&&x<=67)
        {
                printf("\n祝您好运!    \n\n");
                sleep(2);
                return x;
        }




}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~说明*/
shuoming()
{
        char x;
        system("cls");
        printf("\n%s\n\n\n",caidan[0]);
        printf("\t您好,欢迎您来玩这个加、减、乘、除的数学小测试。它分为三个等级:\n"
               "\t初级:  二位数以下的运算,无小数,无负数,可更改一次答案,\n"
               "\t        每题7分,有无错奖20分;\n "
               "\t中级:  二位数以下的运算,结果精确到2位小数,有负数,每题8分,\n"
               "\t        无错奖20分;\n"
               "\t高级:  三位数以下的运算,结果精确到3位小数,有负数,每题20分\n");
       printf("%s\n\n",caidan[0]);
       printf("请问您是要回到主菜单还是要退出?退出按N,其它键回到主菜单:\n\n");
       scanf("  %c",&x);
       if(x=='N'||x=='n')
               exit(0);
       else    caidan1();
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~运算*/
float yunsuan( float a, float b,char jisuanfu)
{
        float c;
        switch(jisuanfu)
        {
                case '+':  c=a+b;  break;
                case '-':  c=a-b;  break;
                case '*':  c=a*b;  break;
                case '/':  c=a/b;
        }
        return   c;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~初级*/
int chuji()
{
       int a,b,i,zh,x,X,fen=0,jieguo,j=1,k,m=0;
       system("cls");
       printf("\t%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               k=0;
               srand(time(NULL));
               a=rand()%99+5;
               srand(time(NULL)+1);
               b=rand()%99+5;
               if(a<b)
               {
                       zh=a;
                       a=b;
                       b=zh;
               }
               while(a%b!=0)
               {
                       a++;
               }
               printf("\n第%d题:%d %c %d = ",j,a,jisuanfu[i],b);
               x=yunsuan(a,b,jisuanfu[i]);   /*~~~~~~~~此处不加int是否能转换过来?*/
               while(k==0)
               {
                       scanf("  %d",&jieguo);
                       puts(" ");
                       if(x==jieguo)
                               printf("%s\n您的分数为%d。\n%s \n\n\n",dui,fen+=7,caidan[0]);
                       else
                       {
                               m++;
                               printf("%s\n%s\n\n",YN,caidan[0]);
                               scanf("  %c",&X);
                               if(X=='N'||X=='n')
                                       break;
                               else
                               {
                                        printf("%d %c %d = ",a,jisuanfu[i],b);
                                        scanf("  %d",&jieguo);
                                        if(x==jieguo)
                                        {
                                                 printf("\n%s\n您的分数为:%d\n%s\n\n",dui,fen+=7,caidan[0]);
                                                 break;
                                        }
                               }
                               printf("非常遗憾,您错了!不加分。\n");
                       }
                       k++;
               }
       }
       if(m==0)
       {
               printf("您无一题出过错,奖励10分。\n");
               fen+=20;
       }
       printf("任意键继续:");
       getch();
       return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~中级*/
int zhongji()
{
       int i,X,fen=0,j=1,k=0;
       float a,b,x,zh,jieguo;
       system("cls");
       printf("%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               srand(time(NULL));
               a=rand()%99+1;
               srand(time(NULL)+1);
               b=rand()%99+1;
               printf("\n第%d题:%f %c %f = ",j,a,jisuanfu[i],b);
               scanf("  %f",&jieguo);
               puts(" ");
               x=yunsuan(a,b,jisuanfu[i]);
               if(x<jieguo)
               {
                       zh=x;
                       x=jieguo;
                       jieguo=zh;
               }
               if(x-jieguo<=0.009)
                       printf("%s\n您的分数为:%d\n\n%s\n\n",dui,fen+=8,caidan[0]);
               else
               {
                       k++;
                       printf("\n非常遗憾,您错了!\n%s\n\n",caidan[0]);
               }
       }
       if(k==0)
       {
               printf("您无一题出错,奖20分。\n\n");
               fen+=20;
       }
       printf("请按任意键:\n");

       return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~高级*/
int gaoji()
{
       int i,fen=0,j=1;
       float jieguo,zh,a,b,x;
       system("cls");
       printf("%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               srand(time(NULL));
               a=rand()%999+1;
               srand(time(NULL)+1);
               b=rand()%999+1;
               a=a/0.03;
               b=b/0.03;
               printf("\n第%d题:%f %c %f = ",j,a,jisuanfu[i],b);
               scanf("  %f",&jieguo);
               puts(" ");
               x=yunsuan(a,b,jisuanfu[i]);
               if(x<jieguo)
               {
                      zh=x;
                      x=jieguo;
                      jieguo=zh;
               }
               if(x-jieguo<=0.0009)
               {
                       printf("%s\n%s\n\n",dui,caidan[0]);
                       fen+=10;
               }
               else
               {
                       printf("\n非常遗憾,您错了!\n");
                       printf("\n%s\n\n",caidan[0]);
               }
       }
       return fen;
}
/*########################################################主函数*/
int main(void)
{
        int fen;
        char x;
        while(1)
        {
                x=caidan1();
                switch(x)
                {
                        case 'A': fen=chuji();
                                  break;
                        case 'B': fen=zhongji();
                                  break;
                        case 'C': fen=gaoji();
                }
                printf("在您这轮测试中,您得了%d分。\n",fen);
                printf("请按任意键:\n");
                getch();

         }
}
搜索更多相关主题的帖子: 小游戏 
2008-03-15 00:18
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
请在中文编译器下测试
2.rar (23.39 KB)
2008-03-15 00:22
nobush
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2008-1-10
收藏
得分:0 
我是小白鼠~
1. 為什麽要求用戶 请您用大写字符输入您的选择
應該是程序設計者處理大小寫問題。

2. 當遇到 对不起,您输入的字符非法,请重新输入。
應該重現顯示菜單,否則用戶多幾次非法輸入,菜單就滾到屏幕外面去了~
而且那一串***沒有對齊,多了一個 \t

3. 好像題目中整數沒有必要顯示為 14.000000 這樣形式吧?

4. 程序沒有及時清空緩衝區,因為輸入答案都使用的scanf。也沒有防錯處理。
比如:我在初級問題中 本應該輸入整數的時候我輸入一個小數如:把   10 輸入為10.001
那麽,本題正確,而下一題報錯,而且沒有輸入 N的機會。

5. 有一個明顯的錯誤就是
高级:  三位数以下的运算,结果精确到3位小数
而程序用 a=a/0.03; 產生運算數,大部分都會大於三位數。
進而引發另一個問題:變量都是float類型,可能出現精度錯誤。
例如:
17433.333984 * 28966.666016 =
如果計算到小數4位 504985563.0599
但是這裡  504985568 也會得到 恭喜您,答对了!

6.最後, 其它键 用scanf處理,對用戶非常不方便。而且也會帶來 第4.中的問題。
大概就是這些了
2008-03-15 17:57
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
回复 3# 的帖子
您好,欢迎您来找错,您的找错就是我前进的动力,是对我最大的支持与关爱。

:)

    您的意见,我仔细看了一下,对您的态度我很感动,您不是简单地回复一些“顶”之类的“条件反射”的话语,而是认真对我的程序进行了测试。非常感谢。

    对您的意见,第一、二、三条,我基本凭自己现在的能力可以改正。第四、五、六条,我应该学过这些知识,只不过没有应用到,您能不能提示一下,或者给个关键词什么的?

    呵呵,辛苦辛苦,劳烦您再打几个字。我这不是作业,请不必怀有抵触心理。

    再次欢迎您来找错。
2008-03-15 19:15
qfyzy
Rank: 2
等 级:论坛游民
威 望:1
帖 子:380
专家分:86
注 册:2008-2-17
收藏
得分:0 
4,每次输入完后,用一个while((c=getchar())!='\n'&&c!=EOF);
不会出现 10 輸入為10.001,本題正確,而下一題報錯,而且沒有輸入 N的機會。
5,改用double
6,呵呵,用getch来做,或是换到vc来做
顺便说一下,用tc可以做出tc3的界面,带有鼠标操作,不过比较麻烦

当对C的经验增加时,它会显的很好用.----Dennis M Ritche如是说
2008-03-15 19:19
hoodlum1980
Rank: 2
来 自:浙江大学
等 级:论坛游民
威 望:2
帖 子:289
专家分:23
注 册:2008-2-24
收藏
得分:0 
[bo]以下是引用 [un]nobush[/un] 在 2008-3-15 17:57 的发言:[/bo]

1. 為什麽要求用戶 请您用大写字符输入您的选择
應該是程序設計者處理大小寫問題。

2. 當遇到 对不起,您输入的字符非法,请重新输入。
應該重現顯示菜單,否則用戶多幾次非法輸入,菜單就滾到屏幕外面去了~
而且那一串***沒 ...

这位网友相当的认真严谨,要赞一下。前面提到的几条我也感觉到了。比如,浮点数,在说明说说了精确到小数点后2位,那么显示数字时没有任何格式控制,而是把浮点数完整显示出来就不太好了。
我也把楼主的代码运行了一下,我主要是把楼主的代码中的中文勉强翻译为英文,这样我就可以在TC下运行并看懂那些信息了。
2008-03-15 23:17
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
收藏
得分:0 
作了一些改进,请大家再提提意见
/*
针对大家提出的意见,已经作出了如下调整:
1:改进了选择的时候只可以输入大写(当初有些懒,在此检讨);
2:输入错误重新输入的时候,又返回到菜单界面;
3:中级已经改进显示形式,强制转换为int型;
4:初级的结果输入改为float 型,另外在每级的输入结束都加上while((c=getchar())!='\n'&&c!=EOF);这样的字符,
   改正了出现的错误情况;
5:输入结果改为double型;
6:其它键输入改为getchar();。

    特别感谢nobush 与qfyzy朋友的意见与建议,对于hoodlum1980朋友提出的意见,我现在还没想到太好的办法来改进。比如说,我试过:int a;
                    float b;
                    a=b*100;
                    b=a/100;
                    这样类似的形式,但也许是我的算法有误,或者是其它原因,没有成功,如果您有好的算法,请告知,谢谢。


       非常感谢大家对我的帮助,特别感谢。也谢谢老帖子的各位朋友的建议与意见。

*/



#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char YN[100]={"您错了。请问您是放弃这道题,还是更改答案?您有一次更改的机会\n"
              "按N退出这道题,按其它键更改。\n"};
char dui[100]={"恭喜您,答对了!\n"};
char caidan[3][100]={{"***********************************************************************\n"},
                     {"您好,欢迎您来玩这个小游戏,它将会验证您的计算能力。 \n"},
                     {"对不起,您输入的字符非法,请重新输入。\n"}};
char jisuanfu[30]={'+','-','*','/','/','/','*','+','+','-','/','*','/','+','-','-','+','*','*','/','/','+','+','-','-','-','*','*','*','*'};

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~菜单*/
char caidan1()
{
        char x;
        char y[]={"请您输入您的选择:\n"};
        char caidan_1[100]={"\t\n\tA:初级\n\tB:中级\n\tC:高级\n\tD:游戏说明\n\tE:退出\n\n\n"};
        do
        {
                 system("cls");
                 printf("\n%s\t%s\n\n",caidan[0],caidan[1]);
                 printf("%s\n",caidan_1);
                 printf("\n%s\n\n\n\t%s ",caidan[0],y);
                 x=getchar();
                /* while((x=getchar())!='\n'&&x!=EOF); */
                 if(x!='a'&&x!='A'&&x!='b'&&x!='B'&&x!='c'&&x!='C'&&x!='d'&&x!='D'&&x!='e'&&x!='E')
                 {
                         printf("\n\t%s\n%s",caidan[2],caidan[0]);
                         sleep(1);
                 }
        }
        while(x!='a'&&x!='A'&&x!='b'&&x!='B'&&x!='c'&&x!='C'&&x!='d'&&x!='D'&&x!='e'&&x!='E');
        if(x=='E'||x=='e')
                exit(0);
        if(x=='D'||x=='d')
                shuoming();
        if(x=='a'||x=='A'||x=='b'||x=='B'||x=='c'||x=='C')
        {
                printf("\n祝您好运!    \n\n");
                sleep(2);
                if(x=='a'||x=='A')
                        x='A';
                else if(x=='b'||x=='B')
                        x='B';
                else  x='C';
                return x;
        }




}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~说明*/
shuoming()
{
        char x;
        system("cls");
        printf("\n%s\n\n\n",caidan[0]);
        printf("\t您好,欢迎您来玩这个加、减、乘、除的数学小测试。它分为三个等级:\n"
               "\t初级:  二位数以下的运算,无小数,无负数,可更改一次答案,\n"
               "\t        每题7分,有无错奖20分;\n "
               "\t中级:  二位数以下的运算,结果精确到2位小数,有负数,每题8分,\n"
               "\t        无错奖20分;\n"
               "\t高级:  三位数以下的运算,结果精确到3位小数,有负数,每题20分\n");
       printf("%s\n\n",caidan[0]);
       printf("请问您是要回到主菜单还是要退出?退出按N,其它键回到主菜单:\n\n");
       x=getchar();;
       if(x=='N'||x=='n')
               exit(0);
       else    caidan1();
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~运算*/
float yunsuan( float a, float b,char jisuanfu)
{
        float c;
        switch(jisuanfu)
        {
                case '+':  c=a+b;  break;
                case '-':  c=a-b;  break;
                case '*':  c=a*b;  break;
                case '/':  c=a/b;
        }
        return   c;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~初级*/
int chuji()
{
       int a,b,c,i,zh,x,X,fen=0,j=1,k,m=0;
       float jieguo;
       system("cls");
       printf("\t%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               k=0;
               srand(time(NULL));
               a=rand()%99+5;
               srand(time(NULL)+1);
               b=rand()%99+5;
               if(a<b)
               {
                       zh=a;
                       a=b;
                       b=zh;
               }
               while(a%b!=0)
               {
                       a++;
               }
               printf("\n第%d题:%d %c %d = ",j,a,jisuanfu[i],b);
               x=yunsuan(a,b,jisuanfu[i]);
               while(k==0)
               {
                       scanf("  %f",&jieguo);
                       puts(" ");
                       while((c=getchar())!='\n'&&c!=EOF);
                       if(x==jieguo)
                               printf("%s\n您的分数为%d。\n%s \n\n\n",dui,fen+=7,caidan[0]);
                       else
                       {
                               m++;
                               printf("%s\n%s\n\n",YN,caidan[0]);
                               while((X=getchar())!='\n'&&X!=EOF);
                               if(X=='N'||X=='n')
                                       break;
                               else
                               {
                                        printf("\n\n%d %c %d = ",a,jisuanfu[i],b);
                                        scanf("  %f",&jieguo);
                                        puts(" ");
                                        while((c=getchar())!='\n'&&c!=EOF);
                                        if(x==jieguo)
                                        {
                                                 printf("\n%s\n您的分数为:%d\n%s\n\n",dui,fen+=7,caidan[0]);
                                                 break;
                                        }
                               }
                               printf("非常遗憾,您错了!不加分。\n%s\n\n",caidan[0]);
                       }
                       k++;
               }
       }
       if(m==0)
       {
               printf("您无一题出过错,奖励10分。\n");
               fen+=20;
       }
       printf("任意键继续:");
       getch();
       return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~中级*/
int zhongji()
{
       int i,X,fen=0,j=1,k=0,c;
       float a,b,x,zh,jieguo;
       system("cls");
       printf("%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               srand(time(NULL));
               a=rand()%99+1;
               srand(time(NULL)+1);
               b=rand()%99+1;
               printf("\n第%d题:%d %c %d = ",j,(int)a,jisuanfu[i],(int)b);
               scanf("  %f",&jieguo);
               while((c=getchar())!='\n'&&c!=EOF);
               puts(" ");
               x=yunsuan(a,b,jisuanfu[i]);
               if(x<jieguo)
               {
                       zh=x;
                       x=jieguo;
                       jieguo=zh;
               }
               if(x-jieguo<=0.009)
                       printf("%s\n您的分数为:%d\n\n%s\n\n",dui,fen+=8,caidan[0]);
               else
               {
                       k++;
                       printf("\n非常遗憾,您错了!\n%s\n\n",caidan[0]);
               }
       }
       if(k==0)
       {
               printf("您无一题出错,奖20分。\n\n");
               fen+=20;
       }
       printf("请按任意键:\n");
       getch();

       return fen;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~高级*/
int gaoji()
{
       int i,fen=0,j=1,c;
       float zh,a,b,x;
       double jieguo;
       system("cls");
       printf("%s\n",caidan[0]);
       srand(time(NULL));
       i=rand()%20+0;
       for(;j<=10;i++,j++)
       {
               srand(time(NULL));
               a=rand()%999+1;
               srand(time(NULL)+1);
               b=rand()%999+1;
               a=a/0.3;
               b=b/0.3;
               printf("\n第%d题:%f %c %f = ",j,a,jisuanfu[i],b);
               scanf("  %lf",&jieguo);
               while((c=getchar())!='\n'&&c!=EOF);
               puts(" ");
               x=yunsuan(a,b,jisuanfu[i]);
               if(x<jieguo)
               {
                      zh=x;
                      x=jieguo;
                      jieguo=zh;
               }
               if(x-jieguo<=0.0009)
               {
                       printf("%s\n%s\n\n",dui,caidan[0]);
                       fen+=10;
               }
               else
               {
                       printf("\n非常遗憾,您错了!\n");
                       printf("\n%s\n\n",caidan[0]);
               }
       }
       return fen;
}
/*########################################################主函数*/
int main(void)
{
        int fen;
        char x;
        while(1)
        {
                x=caidan1();
                switch(x)
                {
                        case 'A': fen=chuji();
                                  break;
                        case 'B': fen=zhongji();
                                  break;
                        case 'C': fen=gaoji();
                }
                printf("在您这轮测试中,您得了%d分。\n",fen);
                printf("请按任意键:\n");
                getch();

         }
}
2008-03-16 00:03
快速回复:一个计算小游戏
数据加载中...
 
   



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

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