注册 登录
编程论坛 VC++/MFC

打字测速程序

无水之冰 发布于 2010-04-11 14:49, 856 次点击
打字程序主要是让选择的文本内容隔行显示,关键是要在空行的区域输入文字,这个怎么实现?各位大侠不吝赐教一下
1 回复
#2
小海豚2010-11-17 10:35
C/C++的代码
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

int main()
{
    FILE* fp;
    long t, fpos, maxpos;
    double start,finish;
    char buf_out[100];
    char ch_ans = 'Y';
    int ch_file,i,count;
    fp = fopen("1.txt", "r");
    fseek(fp, SEEK_SET,SEEK_END);
    maxpos = ftell(fp);
    while(ch_ans != 'n')
    {
        count = i = 0;
        srand((unsigned)time(&t));
        fpos = rand()%maxpos;
        fseek(fp,fpos,SEEK_SET);
        while((ftell(fp)<maxpos)&&(ch_file=fgetc(fp)!= ' '))
            ;
        if(ftell(fp) == maxpos)
            fseek(fp, 0, SEEK_SET);
        fscanf(fp,"%s ", buf_out);
        printf("output:%s\ninput: ", buf_out);
        fflush(stdin);
        start = (double)clock();
        while(((ch_ans=getchar()) != ' ') && ch_ans != '\n')
        {
            if((buf_out[i] != '/0') && buf_out[i++] == ch_ans)
                count ++;
        }
        finish = (double)clock();
        printf("correct: %d time used: %4.2fms\ntest again?(y/n) ",count,finish-start);
        fflush(stdin);
        ch_ans = getchar();
    }
    fclose(fp);
    return 0;
}

1