码迷,mamicode.com
首页 > 其他好文 > 详细

Chapter5记忆力训练

时间:2018-01-27 00:36:50      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:错误   tchar   多个   return   chapter   bsp   显示   class   stdout   

//5-1单纯记忆训练,记忆4位数
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

#define MAX_STAGE 10    //关卡数

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;
}
int main(){
    int stage;
    int success = 0;
    clock_t start, end;
    printf("来记忆一个4位数吧。\n");
    
    start = clock();
    for(stage = 0; stage < MAX_STAGE; stage++){
        int x;        //已读取的值
        int no = rand() % 9000 + 1000;     //需要记忆的数
        
        printf("%d", no);
        fflush(stdout);
        sleep(500);    //问题只提示0.5秒
        
        printf("\r请输入:");
        fflush(stdout);
        scanf("%d", &x);
        
        if(x != no)
            printf("\a回答错误。\n");
        else{
            printf("回答正确。\n");
            success++;
        }
    }
    end = clock();
    
    printf("%d次中答对了%d次。\n", MAX_STAGE, success);
    printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}
//5-2单纯记忆训练,玩家自定义数值位数
#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(), c2;
    do{
        if((c2 = clock()) == (clock_t)-1)
            return 0;
    }while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}
int main(){
    int i, stage;
    int level;
    int success = 0;
    clock_t start, end;
    
    srand(time(NULL));
    printf("数值记忆训练\n");
    do{
        printf("要挑战的等级(%d~%d):", LEVEL_MIN, LEVEL_MAX);
        scanf("%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_MIN * 2];    //已读取的数字串
        
        //因为rand()生成的随机数不会超过5位及以上,所以用字符串来处理
        no[0] = 1 + rand() % 9;        //开头是1-9
        for(i = 1; i < level; i++)
            no[i] = 0 + rand() % 10;    //之后是0-9
        no[level] = \0;
        
        printf("%s", no);    
        fflush(stdout);
        sleep(125 * level);        //问题只提示125 * level 毫秒
        
        printf("\r%*s\r请输入:", level, "");
        scanf("%s", x);
        
        if(strcmp(no, x) != 0)
            printf("\a回答错误。\n");
        else{
            printf("回答正确。\n");
            success++;
        }
    }
    end = clock();
    
    printf("%d次中答对了%d次。\n", MAX_STAGE, success);
    printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}
//5-3单纯记忆训练,大写字母
#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(), c2;
    do{
        if((c2 = clock()) == (clock_t)-1)
            return 0;
    }while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}
int main(){
    int i, stage;
    int level;
    int success = 0;
    clock_t start, end;
    const char ltr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    srand(time(NULL));
    printf("英文字母记忆训练\n");
    do{
        printf("要挑战的等级(%d~%d):", LEVEL_MIN, LEVEL_MAX);
        scanf("%d", &level);
    }while(level < LEVEL_MIN || level > LEVEL_MAX);
        
    printf("来记忆%d个英文字母吧。\n", level);
    
    start = clock();
    for(stage = 0; stage < MAX_STAGE; stage++){
        char mstr[LEVEL_MAX + 1];        //需要记忆的一串英文字母
        char x[LEVEL_MAX * 2];            //读取到的英文字母
        
        for(i = 0; i < level; i++)        //生成作为题目的字符串
            mstr[i] = ltr[rand() % strlen(ltr)];
        mstr[level] = \0;
        
        printf("%s", mstr);
        fflush(stdout);
        sleep(125 * level);
        
        printf("\r%*s\r请输入:", level, "");
        fflush(stdout);
        scanf("%s", x);
        
        if(strcmp(x, mstr) != 0)
            printf("\a回答错误。\n");
        else{
            printf("回答正确。\n");
            success++;
        }
    }
    end = clock();
    
    printf("%d次中答对了%d次。\n", MAX_STAGE, success);
    printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}
//5-4单纯记忆训练,大写和小写字母
#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(), c2;
    do{
        if((c2 = clock()) == (clock_t)-1)
            return 0;
    }while(1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x);
    return 1;
}
int main(){
    int i, stage;
    int level;
    int success = 0;
    clock_t start, end;
    const char ltr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                       "abcdefghijklmnopqrstuvwxyz";    //仅仅此处和5-3不同
    
    srand(time(NULL));
    printf("英文字母记忆训练\n");
    do{
        printf("要挑战的等级(%d~%d):", LEVEL_MIN, LEVEL_MAX);
        scanf("%d", &level);
    }while(level < LEVEL_MIN || level > LEVEL_MAX);
        
    printf("来记忆%d个英文字母吧。\n", level);
    
    start = clock();
    for(stage = 0; stage < MAX_STAGE; stage++){
        char mstr[LEVEL_MAX + 1];        //需要记忆的一串英文字母
        char x[LEVEL_MAX * 2];            //读取到的英文字母
        
        for(i = 0; i < level; i++)        //生成作为题目的字符串
            mstr[i] = ltr[rand() % strlen(ltr)];
        mstr[level] = \0;
        
        printf("%s", mstr);
        fflush(stdout);
        sleep(125 * level);
        
        printf("\r%*s\r请输入:", level, "");
        fflush(stdout);
        scanf("%s", x);
        
        if(strcmp(x, mstr) != 0)
            printf("\a回答错误。\n");
        else{
            printf("回答正确。\n");
            success++;
        }
    }
    end = clock();
    
    printf("%d次中答对了%d次。\n", MAX_STAGE, success);
    printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}
//5-5加一训练,记忆多个数值并输入这些数值加1后的值
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_STAGE 10    //关卡数
#define LEVEL_MIN 2        //最低等级(位数)
#define LEVEL_MAX 6    //最高等级(位数)

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;
}
int main(){
    int i, stage;
    int level;
    int success = 0;
    int score[MAX_STAGE];    //所有关卡的答对数量
    clock_t start, end;
    
    srand(time(NULL));
    printf("加一训练开始\n");
    printf("来记忆2位的数值\n");
    printf("请输入原数值加1后的数值\n");
    do{
        printf("要挑战的等级(%d~%d):", LEVEL_MIN, LEVEL_MAX);
        scanf("%d", &level);
    }while(level < LEVEL_MIN || level > LEVEL_MAX);

    start = clock();
    for(stage = 0; stage < MAX_STAGE; stage++){
        int no[LEVEL_MAX];    //要记忆的数
        int x[LEVEL_MAX];    //已读取的值
        int seikai = 0;        //本关卡的答对数量
        
        printf("\n第%d关卡开始!!\n", stage + 1);
        for(i = 0; i < level; i++){        //生成level个两位数
            no[i] = rand() % 90 + 10;
            printf("%d ", no[i]);
        }
        fflush(stdout);
        sleep(300 * level);
        printf("\r%*s\r", 3 * level, "");
        fflush(stdout);
        
        for(i = 0; i < level; i++){        //读取答案
            printf("第%d个数:", i + 1);
            scanf("%d", &x[i]);
        }
        for(i = 0; i < level; i++){        //判断对错并显示
            if(x[i] != no[i] + 1)
                printf("X ");
            else {
                printf("O ");
                seikai++;
            }
        }
        putchar(\n);
        
        for(i = 0; i < level; i++)        //显示正确答案
            printf("%2d", no[i]);
        printf(" ... 答对了%d个。\n", seikai);
        score[stage] = seikai;
        success += seikai;
    }
    end = clock();
    
    printf("%d次中答对了%d次。\n", MAX_STAGE, success);
    for(stage = 0; stage < MAX_STAGE; stage++)
        printf("第%2d关卡:%d\n", stage + 1, score[stage]);
    printf("用时%.1f秒。\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}
//5-9读取想要的数,把最后10个存入元素个数为10的数组
#include<stdio.h>
#define MAX 10
int main(){
    int i;
    int a[MAX];
    int cnt = 0;    //读取到的个数
    int retry;
    printf("请输入整数。\n");
    do{
        if(cnt >= MAX){
            for(i = 0; i < MAX - 1; i++)    //把a[1]到a[MAX - 1]往前移动一个位置
                a[i] = a[i + 1];
        }
        printf("第%d个整数:", cnt + 1);
        scanf("%d", &a[cnt < MAX ? cnt : MAX - 1]);
        cnt++;
        
        printf("是否继续?(Yes...1/No...0): ");
        scanf("%d", &retry);
    }while(retry == 1);
    
    if(cnt <= MAX)
        for(i = 0; i < cnt; i++)
            printf("第%2d个:%d\n", i + 1, a[i]);
    else
        for(i = 0; i < MAX; i++)
            printf("第%2d个:%d\n", cnt - MAX + 1 + i, a[i]);
    return 0;
}
//5-10读取想要的数,把最后10个存入元素个数为10的数组,不移动元素
#include<stdio.h>
#define MAX 10
int main(){
    int i;
    int a[MAX];
    int cnt = 0;
    int retry;
    printf("请输入整数。\n");
    do{
        printf("第%d个整数:", cnt + 1);
        scanf("%d", &a[cnt++ % MAX]);
        
        printf("是否继续?(Yes...1/No...0): ");
        scanf("%d", &retry);
    }while(retry == 1);
    i = cnt - MAX;
    if(i < 0) i = 0;
    
    for(; i < cnt; i++)
        printf("第%2d个:%d\n", i + 1, a[i % MAX]);
    return 0;
}

 

Chapter5记忆力训练

标签:错误   tchar   多个   return   chapter   bsp   显示   class   stdout   

原文地址:https://www.cnblogs.com/leosirius/p/8361879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!