国产好吃的方便面:猜数游戏的程序分析

来源:百度文库 编辑:高校问答 时间:2024/05/08 12:44:17
#include time.h
#include stdlib.h
#include stdio.h
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf(do you want to play it.('y' or 'n') \\n);
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf(\\nplease input number you guess:\\n);
start=clock();
a=time(NULL);
scanf(%d,&guess);
while(guess!=i)
{if(guess>i)
{printf(please input a little smaller.\\n);
scanf(%d,&guess);}
else
{printf(please input a little bigger.\\n);
scanf(%d,&guess);}
}
end=clock();
b=time(NULL);
printf(\\1: It took you %6.3f seconds\\n,var=(double)(end-start)/18.2);
printf(\\1: it took you %6.3f seconds\\n\\n,difftime(b,a));
if(var<15)
printf(\\1\\1 You are very clever! \\1\\1\\n\\n);
else if(var<25)
printf(\\1\\1 you are normal! \\1\\1\\n\\n);
else
printf(\\1\\1 you are stupid! \\1\\1\\n\\n);
printf(\\1\\1 Congradulations \\1\\1\\n\\n);
printf(The number you guess is %d,i);
}
printf(\\ndo you want to try it again?(\\yy\\.or.\\n\\)\\n);
if((c=getch())=='y')
goto loop;
}
要详细的程序分析,要每一句每一句的说明,这样才有分加,如果回答的好的话还有100分加!!!希望各位大虾帮帮忙啦,不甚感激!!!!!!!!
很急啊。希望今天有人能搞定(20:00之前)
350分,我买血啊!!!!!!!!!能人帮帮忙!!!小的在这拜服了!!!!!!!!!

/* 花了我不少时间, 还可以吧, 如果用VC++编译, 请包含下面一句 . 你的原程序有错误, 我全改过来了, 并全部程序编译通过.没有比这个更详细吧.*/
/*#include "stdafx.h"*/

#include "time.h"/*时间头文件*/
#include "stdlib.h" /*标准库包含文件*/
#include "stdio.h" /*标准IO包含文件*/
main() /*主程序入口*/
{
char c; /*定义一个字符型的变量, 用来输入要猜的数*/
clock_t start,end; /*定义两个时钟变量,实际上就是一个长整形, 它在time.h中定义为:typedef long clock_t;*/
time_t a,b; /*定义两个时间变量,也是长整形*/
double var; /*定义临时变量, 用于计算猜出数字共花了多少时间*/
int i,guess; /*定义变量来保存随机数和玩家猜的数*/
srand(time(NULL)); /*设置随机数*/
printf("do you want to play it.('y' or 'n') \\n"); /*提示输入y或n开始或结束*/
loop: /*重复输入时程序定位点*/
while((c=getchar())=='y') /*接受键盘输入,是y则一直执行*/
{
i=rand()%100; /*随机数模100为产生随机数.*/
printf("\\nplease input number you guess:\\n"); /*提示输入要猜的数字*/
start=clock(); /*记下开始时程序共使用了多少个CPU即处理器时钟,Calculates the processor time used by the calling process*/
a=time(NULL); /*记下开始时的时间, 格式如年月日时分秒*/
scanf("%d",&guess); /*输入要猜的数字*/
while(guess!=i) /*如果输入的数不等于产生的随机数*/
{
if(guess>i){/*要猜的数大于随机数*/
printf("please input a little smaller.\\n"); /*提示输入小一点的数.*/
scanf("%d",&guess);/*输入小一点的数*/
}
else {/*否则*/
printf("please input a little bigger.\\n"); /*请你输入大一点的数,这句和上句是叫输入与随机数一样大的数.*/
scanf("%d",&guess);/*输入大一点的数.*/
}
}
/*过了上面这个}, 表示找到了与随机数相同的数,*/
end=clock(); /*结束时钟*/
b=time(NULL); /*结束时间,时钟与时间解释见上*/
printf("\\1: It took you %6.3f seconds\\n",var=(double)(end-start)/18.2); /*显示花了多少时间,用的是时钟计算*/
printf("\\1: it took you %6.3f seconds\\n\\n",difftime(b,a)); /*显示花了多少时间,用的是时间计算*/
if(var<15) /*如果花费的时间(秒)小于15秒*/
printf("\\1\\1 You are very clever! \\1\\1\\n\\n"); /*提示你是高手*/
else if(var<25) /*在25秒内*/
printf("\\1\\1 you are normal! \\1\\1\\n\\n"); /*提示你是一般的家伙*/
else /*超过25秒*/
printf("\\1\\1 you are stupid! \\1\\1\\n\\n");/*提示你差的很*/
printf("\\1\\1 Congradulations \\1\\1\\n\\n"); /*提示恭喜*/
printf("The number you guess is %d",i); /*显示你猜的数*/
}
printf("\\ndo you want to try it again?(\\yy\\.or.\\n\\)\\n"); /*提示,还要继续吗?*/
if((c=getchar())=='y')/*输入y,则继续*/
goto loop; /*返回到开始猜数的地方*/
}

#include time.h
#include stdlib.h
#include stdio.h
main()
{char c; /*定义一个字符型的变量去接收输入*/
clock_t start,end; /*变量记录时间的开始和结束*/
time_t a,b; /*定义变量来保存时间点*/
double var; /*定义辅助变量计算时间*/
int i,guess; /*定义变量来保存随机数和玩家猜的数*/
srand(time(NULL)); /*设置随机数种子*/
printf(do you want to play it.('y' or 'n') \\n); /*输出提示信息*/
loop: /*多次玩猜数字游戏的起点*/
while((c=getchar())=='y') /*接收输入并判断是否愿意游戏,如果输入y则进入游戏,否则不执行此循环体*/
{
i=rand()%100; /*生成100以内的随机整数*/
printf(\\nplease input number you guess:\\n); /*输出提示信息*/
start=clock(); /*保存开始时间*/
a=time(NULL); /*保存开始时间*/
scanf(%d,&guess); /*接收输入*/
while(guess!=i) /*判断输入的数字是否于随机数相等,不等时进入循环,相等时不执行此循环体*/
{if(guess>i) /*判断输入的数字是否比随机数大,大则执行*/
{printf(please input a little smaller.\\n); /*输出提示信息*/
scanf(%d,&guess);} /*再次输入数字*/
else /*如果输入的数字比随机数小则执行*/
{printf(please input a little bigger.\\n); /*输出提示信息*/
scanf(%d,&guess);} /*再次输入数字*/
}
end=clock(); /*当玩家猜正确数字时退出上面循环,记录此时时间点*/
b=time(NULL); /*保存结束时间*/
printf(\\1: It took you %6.3f seconds\\n,var=(double)(end-start)/18.2); /*输出猜数字用的时间*/
printf(\\1: it took you %6.3f seconds\\n\\n,difftime(b,a)); /*输出时间差额*/
if(var<15) /*如果猜数字用的时间小于15秒*/
printf(\\1\\1 You are very clever! \\1\\1\\n\\n); /*输出提示信息*/
else if(var<25) /*如果猜数字的时间大于15秒但是小于25秒*/
printf(\\1\\1 you are normal! \\1\\1\\n\\n); /*输出提示信息*/
else /*如果猜数字的时间大于25秒*/
printf(\\1\\1 you are stupid! \\1\\1\\n\\n); /*输出提示信息*/
printf(\\1\\1 Congradulations \\1\\1\\n\\n); /*输出提示信息*/
printf(The number you guess is %d,i); /*输出你所猜得的数字*/
}
printf(\\ndo you want to try it again?(\\yy\\.or.\\n\\)\\n); /*输出提示信息询问是否想再玩一次?*/
if((c=getch())=='y') /*如果输入y则程序跳转到loop:行进行又一次猜数字游戏,输入其他则退出程序*/
goto loop; /*跳转程序*/
}

只有我这样的好人才肯花那么多时间去帮你注释,希望看懂还是看不懂都给个旗子!
#include time.h 设置时间要用这个头文件
#include stdlib.h 这个是字符串要用的头文件
#include stdio.h 输入输出要用这个头文件
main() 主函数,不要是不懂啊
{char c; 定义一个字符型变量C
clock_t start,end; 定义计时变量
time_t a,b; 定义时间变量
double var; 定义双精度型变量
int i,guess; 定义整型变量
srand(time(NULL)); 调用time函数,srand是随机的意思
printf(do you want to play it.('y' or 'n') \\n); 输出:do you want to play it(‘y’ or ‘n’),n\\是换行
loop: 这个是为goto 能找到这里用的
while((c=getchar())=='y') 当你输入y时
{
i=rand()%100; 从1-100随机取得一个数字存在i中。
printf(\\nplease input number you guess:\\n); 换行,再输出please input number you guess,换行
start=clock(); 调用clock()函数
a=time(NULL); 调用时间函数
scanf(%d,&guess); 输入你要猜的数字
while(guess!=i) 如果你猜错了
{if(guess>i) 如果猜的数字大于随机产生的那个数
{printf(please input a little smaller.\\n); 输出:please input a little smaller,换行
scanf(%d,&guess);} 输入你要猜的数字
else 否则,如果猜的数字小于随机产生的那个数。
{printf(please input a little bigger.\\n); 输出please input a little bigger,换行
scanf(%d,&guess);} 输入你要猜的数字
}
end=clock(); 调用计时函数
b=time(NULL); 调用时间函数
printf(\\1: It took you %6.3f seconds\\n,var=(double)(end-start)/18.2); 输出:It took you %6.3f seconds(注意,%6.3f会替换为var变量的值)
printf(\\1: it took you %6.3f seconds\\n\\n,difftime(b,a));
if(var<15) 输出:It took you %6.3f seconds(注意,%6.3f会替换为difftime变量的值)

printf(\\1\\1 You are very clever! \\1\\1\\n\\n); 这行不用说了吧
else if(var<25) 如果var小于25
printf(\\1\\1 you are normal! \\1\\1\\n\\n); 这行不用说了吧
else这行不用说了吧
printf(\\1\\1 you are stupid! \\1\\1\\n\\n); 这行不用说了吧
printf(\\1\\1 Congradulations \\1\\1\\n\\n); 这行不用说了吧
printf(The number you guess is %d,i); 输出The number you guess is %d(注意,%d被替换为i的值了)

晕~考试不会考这样的题的。。。
去找点简单的做做会用就够了

简单来说就是随机产生一个数字,让用户猜
然后提示你大了或是小了
最后输出时间
时间长了还骂你笨
最后问你还要玩吗

我真佩服2楼 的