练习5-1
//编写一个“记忆力训练”程序,玩家需按相反的顺序输入已记忆的整数的各个数位,例如提示的题目是5892,那么玩家就必须输入2985.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define LEVEL_MIN 2
#define LEVEL_MAX 6
int success=0; //因为在不同函数都用到了这三个变量,所以直接设置成全局变量比较方便。
clock_t start, end;
/*---等待x毫秒---*/
int sleep (unsigned long x)
{
clock_t c1 = clock(), c2;
do {
if ( (c2 = clock()) == (clock_t)-1)
return 0;
} while (1000.0 * (c2-c1)/ CLOCKS_PER_SEC < x);
return 1;
}
void result_show (int level, int max_stage)
{
end = clock();
printf("\n%d个%d位数的数字中答对了%d个。\n",max_stage,level,success);
printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
}
void version1 (int level, int max_stage)
{
start = clock();
int stage;
double y1 = pow(10,level); //pow函数可以进行幂运算,注意要先赋值为double型再强制转换,否则会有误差!
double y2 = pow(10,level-1);
/*
printf("\ny1=%d,y2=%d。\n",(int)y1,(int)y2);
*/
for (stage = 0; stage < max_stage; stage ++) {
int x;
int no = (int)y2 + rand()%((int)y1-(int)y2); //根据设置位数的不同产生不同范围的数字,比如三位数就生成100~999的随机数
int ans=0;
printf("\n第%d关卡开始!!\n", stage + 1);
printf("%d",no);
fflush(stdout);
sleep(300*level);
printf("\r%*s\r",level,"");
fflush(stdout);
printf("这个数字反过来是:");
scanf("%d",&x);
int n = level;
while(no>0) {
//求出顺序相反的数字
double y = pow(10,n-1);
ans += no%10 * (int)y ;
no /= 10;
n--;
}
printf("正确答案是%d。\n",ans);
printf("你的答案:");
if (ans != x)
printf("\a × \n");
else {
printf(" √ \n");
success++;
}
}
}
void version2 (int level,int max_stage)
{
start = clock();
int stage,i,j;
for (stage = 0; stage < max_stage; stage++) {
char no[LEVEL_MAX+1];
char ans[LEVEL_MAX+1];
char x[LEVEL_MAX*2];
printf("\n第%d关卡开始!!\n", stage + 1);
no[0] = '1' + rand() % 9;
for (i = 1; i < level; i++)
no[i] = '0' + rand() % 10;
no[level] = '\0';
printf("%s", no);
fflush(stdout);
sleep(300 * level);
printf("\r%*s\r",level,"");
fflush(stdout);
printf("这个数字反过来是:");
scanf("%s",x);
for (i=0; i<level/2; i++) {
//互换x[i]、x[level-1-i]
char temp = no[i];
no[i] = no[level-1-i];
no[level-1-i] = temp;
}
printf("正确答案是%s。\n",no);
printf("你的答案:");
if (strcmp(no, x) != 0)
printf("\a × \n");
else {
printf(" √ \n");
success++;
}
}
}
int main()
{
int opt;
int max_stage;
int level;
srand(time(NULL));
printf("记忆训练开始!!\n");
printf("请输入原数值倒过来的值。\n");
printf("如题目为5892,则你要输入2985。\n");
do {
printf("\n要挑战的数字位数(%d~%d):", LEVEL_MIN, LEVEL_MAX);
scanf("%d", &level);
} while (level < LEVEL_MIN || level > LEVEL_MAX);
do {
printf("训练次数:");
scanf("%d", &max_stage);
} while (max_stage <= 0);
printf("\n哪个版本?\n");
printf("1---用int型整数来处理。\n");
printf("2---用字符串来处理。\n");
version_choose:
printf("请选择:");
scanf("%d",&opt);
if (opt == 1)
version1(level, max_stage);
else if (opt == 2)
version2(level, max_stage);
else {
printf("\aERROR!!!\n");
goto version_choose;
}
result_show(level,max_stage);
return 0;
}
练习5-2
//编写一个“记忆力训练”程序,玩家记忆整数后需回答出其中某个数位的数值,例如题目为5982,当程序询问“从左到右第3位的数字是什么:”,玩家就必须输入8。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define LEVEL_MIN 2
#define LEVEL_MAX 6
int success=0; //因为在不同函数都用到了这三个变量,所以直接设置成全局变量比较方便。
clock_t start, end;
/*---等待x毫秒---*/
int sleep (unsigned long x)
{
clock_t c1 = clock(), c2;
do {
if ( (c2 = clock()) == (clock_t)-1)
return 0;
} while (1000.0 * (c2-c1)/ CLOCKS_PER_SEC < x);
return 1;
}
void result_show (int level, int max_stage)
{
end = clock();
printf("\n%d个%d位数的数字中答对了%d个。\n",max_stage,level,success);
printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
}
void version1 (