C语言程序设计--记忆力测试试题及注释

发布时间:   来源:文档文库   
字号:
#include#include#include#include#include#include
#defineMAX8/*设最大的数字个数是十个*/
voidinit(void/*该函数的功能是为了画出周围的那个背景矩形*/{
setcolor(LIGHTRED;setlinestyle(0,0,3;line(100,50,540,50;line(100,50,100,420;line(100,420,540,420;line(540,50,540,420;setlinestyle(0,0,0;}
intmain({
inta,b,i,j,size,ch,temp,side;chars[30],out[7],in[7],c;void*p=NULL;a=DETECT;
initgraph(&a,&b,"";setbkcolor(BLUE;
/*准备工作:复制出一个小矩形,方便后面粘贴*/
size=imagesize(150,150,180,180;/*初始出复制图形的大小*/rectangle(150,150,180,180;/*初始出一个合适的矩形*/p=malloc(size;if(!preturn;
getimage(150,150,180,180,p;/*复制出图形*/cleardevice(;/*因为是准备工作,所以要清屏*//*欢迎界面*/init(;
setcolor(RED;
settextstyle(1,0,0;
outtextxy(240,200,"welcome!";settextstyle(1,0,3;
outtextxy(180,250,"pressanythingtostart!";getch(;
cleardevice(;

init(;
settextstyle(0,0,0;/*游戏开始!*/
j=3;/*初始开始的时候是三个数字*/while(j{
cleardevice(;init(;
setcolor(RED;
outtextxy(120,105,"Pleaserememberthefollowing(1--100figures:";srand(time(0;/*确保没次运行产生的随机数不一样,用时间作随机种子*/setcolor(GREEN;
for(i=0;i每次产生j个矩形和随机数*/
{
putimage(120+50*i,120,p,COPY_PUT;/*输出开始时复制的矩形*/ch=rand(%99+1;/*产生随机数*/
out[i]=ch;/*将随机数存放在字符数组中,方便输入输出的比较*/sprintf(s,"%d",ch;/*ch存放在s字符数组中*/
outtextxy(128+50*i,130,s;/*将数字输出在屏幕上的矩形中*/sleep(1;/*延迟一秒,方便玩家记忆*/}
out[j]='\0';
outtextxy(120,180,"Youwillhavefivesecondsforyoutoremember...";sleep(5;
cleardevice(;/*清屏,让玩家输入刚刚让玩家记忆的数字*/init(;
outtextxy(120,105,"Pleaseenterthenumberyousee:";
setcolor(GREEN;i=0;temp=0;side=128;while(i{
if(!temp
putimage(120+50*i,120,p,COPY_PUT;/*输出开始时复制的矩形*/c=getch(;/*输入数字*/
if(temp!=0&&c=='\r'/*如果已经输入且已经按回车确定,就将数字存储到in数组中*/
{
in[i++]=temp;temp=0;
side=128+50*i;/*按回车确定好输入的数字就跳到下一个矩形相应的位置

*/
}else
if(c>='0'&&c<='9'&&temp<10{
temp=temp*10+(c-'0';sprintf(s,"%c",c;outtextxy(side,130,s;
side+=7;/*输入数字就在屏幕上显示,并将连续输入的数字组合成一个数字*/
}}
in[j]='\0';/*输入结束*/
if(!strcmp(out,in/*如果输入的数字和原来输出是一样的,说明记对了*/{
j++;
if(j==MAX/*如果全部记对了,就输出Congratulations!*/{
cleardevice(;
settextstyle(1,0,0;setcolor(RED;
outtextxy(200,200,"Congratulations!";}}
if(strcmp(out,in/*如果猜错了*/{
setcolor(RED;
outtextxy(110,185,"Thecorrectansweris:";/*输出正确答案*/i=0;
while(out[i]!='\0'{
putimage(120+50*i,200,p,COPY_PUT;sprintf(s,"%d",out[i];outtextxy(126+50*i,210,s;i++;}
/*让玩家选择是否继续玩,1重新开始,0退出*/
outtextxy(113,238,"Youhaveansweredwrong,1torestart,0toexit...";
c=getch(;
while(c!='1'&&c!='0'{
if(c!='\r'/*输入的不是12,且不时回车时,就让玩家再次输入*/

}
{
setcolor(GREEN;
outtextxy(188,85,"Wrongchoice!!!Pleasere-select...";}
c=getch(;}
if(c=='0'/*0就输出Gameover!并退出*/{
cleardevice(;
settextstyle(1,0,0;setcolor(RED;
outtextxy(240,200,"Gameover!";break;}
if(c=='1'/*1就重新开始,重设j的值为3,即方框个数为三个*/j=3;}}sleep(2;closegraph(;return0;

本文来源:https://www.2haoxitong.net/k/doc/f11f600c844769eae009edc8.html

《C语言程序设计--记忆力测试试题及注释.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式