您的位置:首页 > 编程语言 > C语言/C++

初学C语言交的大作业

2020-02-29 21:53 344 查看

简介

这个小程序是用来锻炼口算与反应速度。
两种模式。共同点是每次都会随机生成两个数,要求你尽可能快地计算两者差的绝对值,当值落在不同区间需要你输入不同的数(1,9,7),若反应超过一定时间,挑战失败。
如果你在第二个模式下用时很短、正确率很高,程序会要求你输入名字存档,生成一个txt文档记录成绩。
其实本程序就是不断地使用循环、判断,加上一点关于时间的命令。放在这里给我自己看吧。

/**********************************
*  Name:1997                     *
*  Accomplished date: 2016.6.19  *
*  Claim: All rights reserved    *
**********************************/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<windows.h>
#define random(x)(rand()%x)

逐字输出,时延55毫秒

void Slowdisplay(char *p)
{
while(*p!=0)
{
printf("%c",*p++);
Sleep(55);
}
}

绝对值小于20,输入1;大于60,输入7;20-60之间,输入9

int Judge(int z,int c,int r)
{
if(z<=20)
{
if(c!='1') printf("Sorry...\n");                                                 //ASCII (1 49; 5 55; 7 57)
else
{
printf("Right!\n");
r++;
}
}
else if(z>60)
{
if(c!='7') printf("Sorry...\n");
else
{
printf("Right!\n");
r++;
}
}
else
{
if(c!='9') printf("Sorry...\n");
else
{
printf("Right!\n");
r++;
}
}
return r;
}

将名字与分数存档

void Save(int m,int s)
{
time_t timer=time(NULL);
FILE*fp;
char ch,filename[11]={'R','e','c','o','r','d','.','t','x','t'};
if((fp=fopen(filename,"a"))==NULL)
{
printf("We can't open the target file.\n");
exit(0);
}
Slowdisplay("Hero! Please leave your name in this game!(end with #):\n");
fprintf(fp,"Model %c    ",m);
ch=getch();
while(ch!='#')
{
fputc(ch,fp);
putch(ch);
ch=getch();
}
fprintf(fp,"\t%d\t%s",s,ctime(&timer));
fclose(fp);
Slowdisplay("\n\nSuccessfully saved......");
system("cls");
}
int main()
{
int m,c,d,x,y,z,t=0,i,r,p,lim=5,s;                                                   //Auxiliaries: d,p
time_t start, end;
char a[10],b[25];
srand((int)time(0));
system("color 5F");                                                                  //Set a friendly color

Slowdisplay("Welcome to 1997, I hope you can enjoy yourself sincerely. Good luck!\n\n");
printf("Introduction:(Please read carefully)\n\nThis game's aim is practise your speed of calculating and reflection. Model 2 is more difficult than Model 1.\n\nYou will see two numbers, both are <=100, and what you should do is figure out the result of |x-y| as soon as possible. If the resluts<=20, please input '1' so that you can continue. If 20<the results<=60, input '9' and if the result>60 input '7'.\n\n** You don't need to press the ENTER after you input the number. **\n\n");
system("pause");
while(t==0)
{
system("cls");
Slowdisplay("Model 1:Normal    Model 2:Difficult\nYour choice: Model ");
m=getch();
printf("%c\n",m);
Sleep(100);
if(m!='1'&&m!='2')
{
printf("error!!!\n");
Sleep(500);
}

while(m=='1')                                                                    //Enter into the Model 1
{
if(t==0)
{
system("cls");
Slowdisplay("Welcome to Model 1 !!\n\n");
}
c=0;d=1;
while(c!=2)
{
printf("What will you do next?\n1 check specific rules    2 play the game    3 imformation of this model    4 change the model    5 quit\nYour choice: ");
c=getch();
printf("%c\n",c);
Sleep(100);
if(c=='1') Slowdisplay("\nTotally 10 questions. You can get high scores if you get more 'right' and cost less time.\n\n");
else if(c=='2') break;
else if(c=='3') printf("\nName:Purple Bean 2.2(LWlZy)\nProductor:Red Bean\nAccomplish date:2016.5.13\nCompared with the second one:\n\t1,Fix a bug which you can't input other number when facing three directions and the score won't <=22\n\t2,Add a function so that you can play more than one time\n\t3,Fix a bug about the rule\n\t4,Add a function which you can record your scores in a file if scores>=80\nClaim: All rights reserved\n\n");
else if(c=='4') {c=2;d=0;m=0;t=0;}                                       //Set the value to end circles
else if(c=='5') {c=2;d=0;m=0;t=1;}
else printf("error!!!\n\n");
}
if(d==1)
{
system("cls");
printf("Ready? Go!\n\n");
Sleep(1200);
time(&start);
for(i=0,r=0;i<10;i++)                                                    //Create 10 questions and judge the input number
{
x=random(101);
y=random(101);
printf("%d :\n%d\t%d\n",i+1,x,y);
z=abs(x-y);
printf("Your answer: ");
a[i]=getch();
printf("%c\n",a[i]);
r=Judge(z,a[i],r);
}
time(&end);
t=difftime(end,start);
s=11*r-2*t+4;                                                            //Transfer into scores
if(s<22) s=22;                                                           //Limit the min and max value artificially
else if(s>100) s=100;
Slowdisplay("\nThe system is calculating your scores, please wait for some seconds......\n");
printf("The time you cost is: %d seconds.\nNumber of \"Right!\": %d.\nYour scores: %d.\n\n", t,r,s);
if(s>=80) Save(m,s);                                                     //If s>=80, record player's name in a files
}
}

while(m=='2')                                                                    //Enter into the Model 2
{
if(t==0)
{
system("cls");
Slowdisplay("Welcome to Model 2 !!\n\n");
}
c=0;d=1;
while(c!=2)
{
printf("What will you do next?\n1 check specific rules    2 play the game    3 imformation of this model    4 change the model    5 quit\nYour choice: ");
c=getch();
printf("%c\n",c);
Sleep(100);
if(c=='1') Slowdisplay("\nTotally 25 questions. The time which leave to you in each question is decreasing as you playing.\n\n");
else if(c=='2') break;
else if(c=='3') printf("\nName:Purple Bean 3\nProductor:Red Bean\nAccomplish date:2016.6.19\nCompared with the first:\n\t1,Player don't need press ENTER after input the answer.\n\t2,Make screen more friendly by clear it.\n\t3,Make the words more simple and can record the real time.\nClaim: All rights reserved\n\n");
else if(c=='4') {c=2;d=0;m=0;t=0;}
else if(c=='5') {c=2;d=0;m=0;t=1;}
else printf("error!!!\n\n");
}
if(d==1)
{
system("cls");
Slowdisplay("Ready? Go!\n");
for(i=0,r=0,p=0;i<25;i++,p++)
{
x=random(101);
y=random(101);
z=abs(x-y);
if(i%5==0)                                                           //Increase the difficult after every 5 question
{
printf("\nLevel %d: You have %ds to answer following question\n",6-lim,lim);
Sleep(2222);
printf("%d :\n%d\t%d\nYour answer: ",i+1,x,y);
}
else printf("%d :\n%d\t%d\nYour answer: ",i+1,x,y);
time(&start);
b[i]=getch();
time(&end);
t=difftime(end,start);                                               //Record the time that player use it to reflect
printf("%c\n",b[i]);
if(t>lim)
{
printf("You are too slow...\n");
i=25;d=0;
}
else if(t<=lim)
{
r=Judge(z,b[i],r);
if(r==p)
{
i=25;d=0;
}
}
if(r%5==0) lim--;                                                    //Minor 1s after 5 questions
}
Slowdisplay("\nThe system is calculating your scores, please wait for some seconds......\n");
s=r*4+2;lim=5;t=1;                                                       //Set values for the next
if(r==25) printf("Your scores: 100.\nCongratulations!\n");
else printf("Your scores: %d.\n\n",s);
if(s>70) Save(m,s);
}
}
}
Slowdisplay("\nThank you for your playing. Goodbye!\n");
system("pause");                                                                     //Convince for the desktop version
return 0;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
两张信纸 发布了5 篇原创文章 · 获赞 0 · 访问量 226 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: