刚开始学c语言,遇到一题,就是明解c语言中级第五章5.1第二问,数组的问题,就是不知道为什么只能输入第一步,之后就自动出结果了。
求大神授业解惑,感激涕零。
代码如下,结果在后面。
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_STAGE 10
#define LEVEL_MIN 3
#define LEVEL_MAX 20
int sleep(unsigned long x)
{
clock_t c1 = clock();
clock_t c2;
do {
if ((c2 = clock()) == (clock_t)-1)
return 0;
} while (1000.0*(c2 - c1) / CLOCKS_PER_SEC < x);
return 1;
}
int main(void)
{
int i,stage;
int level;
int success = 0;
int tmp;
clock_t start, end;
srand(time(NULL));
printf("数值记忆训练。\n");
do {
printf("要挑战的等级(%d~%d): ",LEVEL_MIN, LEVEL_MAX);
scanf_s("%d", &level);
} while (level<LEVEL_MIN || level>LEVEL_MAX);
printf("来记忆一个%d位数值吧!\n",level);
start = clock();
for (stage = 0; stage < MAX_STAGE; stage++) {
char no[LEVEL_MAX + 1];
char x[LEVEL_MAX * 2];
no[0] = '1' + rand() % 9;
for (i = 1; i < level; i++)
no[i] = '0' + rand() % 10;
no[level] = '\0';
printf("%s", no);
fflush(stdout);
fflush(stdin);
sleep(125 * level);
printf("\r %*s \r请输入:", level, "");
scanf_s("%s", x, strlen(x));
for (i = 0; i < level / 2; i++) {
tmp = no[i];
no[i] = no[level - 1 - i];
no[level - 1 - i] = tmp;
}
if (strcmp(no, x) != 0) {
printf("\a回答错误!\n");
printf("正确答案是:%s\n", no);
}
else {
printf("回答正确!\n");
success++;
}
}
end = clock();
printf("%d次中答对了%d次。\n", MAX_STAGE, success);
printf("用时%.lf秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
return 0;
}
