您的位置:首页 > 编程语言 > C语言/C++

打字测试程序代码(C语言)

2008-06-27 12:54 387 查看
其中头文件"key.h"内容见文章最后部分

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <malloc.h>

#include <time.h>

#include <math.h>

#include <bios.h>

#include <dir.h>

#include "key.h"

typedef struct TEXE

{

unsigned char ch;

unsigned char attr;

}Text;

typedef struct

{

char name[25];

float sco;

}record;

int score[6]={0};

int key,myscore=0;

int right=0,wrong=0,last;

double RightRate=1.0,speed=0.0;

struct ffblk ffblk;

time_t StartTime,LeftTime;

void Main_menu();

void Start_test();

void Type_test(char*s);

void End_test();

void Print_score();

void Get_time(char*);

void Manager_paper();

void Edit_paper();

void Score_list();

void texte(char *str);

int main()

{

Main_menu();

return 0;

}

void Main_menu()

{

textmode(C80);

textbackground(WHITE);

clrscr();

window(6,3,75,23);

textbackground(BLACK);

textcolor(WHITE);

clrscr();

gotoxy(20,3);

printf("Welcome To XXX Typing Test ");

gotoxy(27,6);

printf("Main Menu");

gotoxy(24,8);

printf("1 Typing    Text ");

gotoxy(24,10);

printf("2 High    Scores");

gotoxy(24,12);

printf("3 Edit Text Yourself ");

gotoxy(24,14);

printf("4 Exit Test");

gotoxy(24,16);

printf("Please press 1,2,3or4:");

gotoxy(22,18);

cprintf(" CopyRight(c) 2008 XXX");

gotoxy(46,16);

switch(getch())

{

case'1':

{

Start_test();

break;

}

case'2':

{

Score_list();

break;

}

case'3':

{

Manager_paper();

break;

}

case'4':

{

exit(0);

}

default:

Main_menu();

}

}

void Start_test()

{

char str[][20]={"test1.txt","test2.txt","test3.txt","test4.txt"};

FILE *fp;

textmode(C80);

textbackground(WHITE);

clrscr();

window(6,4,75,22);

textbackground(BLACK);

textcolor(WHITE);

clrscr();

gotoxy(20,5);

printf("Choose One Test ");

gotoxy(20,7);

printf("1  1.txt");

gotoxy(20,9);

printf("2  2.txt");

gotoxy(20,11);

printf("3  3.txt");

gotoxy(20,13);

printf("4  4.txt");

gotoxy(20,15);

printf("Choose The Test:");

while(1)

{

switch(getch())

{

case '1': {Type_test(str[0]);break;}

case '2':{Type_test(str[1]);break;}

case '3':{Type_test(str[2]);break;}

case '4':{Type_test(str[3]);break;}

}

}

}

void Type_test(char *str)

{

int x,y,i=1,j=3;

char c;

Text t;

FILE *fp;

textmode(C80);

textbackground(BLACK);

textcolor(YELLOW);

clrscr();

if((fp=fopen(str,"r"))==NULL)

{

gotoxy(20,13);

printf("Open file error!  Press any key to back");

getch();

Main_menu();

return;

}

StartTime=time(NULL);

myscore=0;

right=0;wrong=0;

last=1;

printf("ESC :Back   END :End Type_test");

Print_score();

window(1,3,80,13);

textbackground(WHITE);

clrscr();

while((c=fgetc(fp))!=EOF)

putchar(c);

putchar(0);

window(1,15,80,25);

textbackground(WHITE);

clrscr();

while(1)

{

if(bioskey(1))

{

key=bioskey(0);

switch(key)

{

case ESC:

x=wherex(); y=wherey();

window(20,8,65,10);

textbackground(BLUE); clrscr();

printf("Quit Test?Type 'y' To Quit:");

c='/0';

scanf("%c",&c);

if(c=='y')

{

fclose(fp);

End_test(); Main_menu();

return;

}

else if(c!='/0')

{

window(20,8,60,10);

textbackground(GREEN);clrscr();

window(1,15,80,25);

gotoxy(x,y);

}

break;

case END:

End_test();

return;

case BACKSPACE:

case KEY_LEFT:

x=wherex(); y=wherey();

gotoxy(x-1,y);

i--;

if(i<1) i=1;

gettext(i,j,i,j,&t);

t.attr=0x27;

puttext(i,j,i,j,&t);

if(last==0) wrong--;

else right--;

RightRate=(double)right/(right+wrong)*100;

clreol();

break;

case ENTER:

c = '/n';

printf("/n");

i=1;j++;

break;

default:

gettext(i,j,i,j,&t);

if(t.ch=='/0'){End_test();return;}

c = key&0xff;

if(c==t.ch){t.attr=0x2f;right++;last=1;}

else {t.attr=0x4f;wrong++;last=0;}

RightRate=(double)right/(right+wrong)*100;

printf("%c",c);

puttext(i,j,i,j,&t);

i++;

if(i>80){i=1;j++;}

}

}

x=wherex(); y=wherey();

delay(40);

Print_score();

window(1,15,80,25);

textbackground(YELLOW);

gotoxy(x,y);

}

}

void End_test()

{

FILE *fp;

int i,j,u;

record *rec;

float nn;

record recs[10],*pop;

myscore=(RightRate/2+LeftTime/6);

rec->sco=myscore;

window(15,7,65,18);

textbackground(8);

textcolor(13);

clrscr();

fp=fopen("score.txt","r");

for(i=0;!feof(fp)&&i<10;i++)

fread(&recs[i],sizeof(record),1,fp);

fclose(fp);

if(i<10){

gotoxy(5,3);

cprintf("rightrate=%f  score=%f  ",RightRate,rec->sco);

gotoxy(4,5);

cprintf("Please input you name  :");

gotoxy(6,7);

scanf("%s",rec->name);

fp=fopen("score.txt","a");

fwrite(rec,sizeof(record),1,fp);

fclose(fp);

return;

}

u=0;

nn=recs[0].sco;

for(j=1;j<i;j++)

if(nn>recs[j].sco){

u=j;

nn=recs[j].sco;

}

if(rec->sco>nn){

fp=fopen("score.txt","w");

gotoxy(5,3);

cprintf("Please input you name  :");

gotoxy(4,5);

cprintf("rightrate=%f  score=%f  ",RightRate,rec->sco);

gotoxy(6,7);

scanf("%s",rec->name);

for(i=0;i<u;i++)

fwrite(&recs[i],sizeof(record),1,fp);

fwrite(rec,sizeof(record),1,fp);

i++;

for(;i<10;i++)

fwrite(&recs[i],sizeof(record),1,fp);

fclose(fp);

return;

}

gotoxy(5,3);

cprintf("I'm sorry you can't go to our list !");

delay(2200);

getch();

Main_menu();

}

void Print_score()

{

char stime[8];

window(1,2,80,2);

textbackground(RED);

clrscr();

Get_time(stime);

printf("Type Speed:%3.2f Letters/Min    Right Rate:%2.2f%%  Left Time:%s",speed,RightRate,stime);

}

void Get_time(char *stime)

{

LeftTime=time(NULL);

LeftTime-=StartTime;

if(LeftTime/60.0) speed=(right+wrong)/(LeftTime/60.0);

LeftTime=300-LeftTime;

sprintf(stime,"%2ld:%2ld",LeftTime/60,LeftTime%60);

}

void Manager_paper()

{

textmode(C80);textbackground(BLACK);textcolor(WHITE);clrscr();

gotoxy(12,9); printf("Choose your operate:");

gotoxy(12,10); printf("E/e : Edit");

gotoxy(12,11); printf("ESC : Back");

while(1)

{

key = bioskey(0);

switch(key)

{

case ESC:

Main_menu();

return;

case KEY_e:

case KEY_E:

Edit_paper();

return;

}

delay(100);

}

}

void Edit_paper()

{

int c;

char str[][20]={"test1.txt","test2.txt","test3.txt","test4.txt"};

textmode(C80);textbackground(WHITE);clrscr();

window(6,4,75,22);textbackground(BLACK);textcolor(WHITE);clrscr();

gotoxy(18,4); printf("Choose Paper");

gotoxy(20,7); printf("1 : 1.txt");

gotoxy(20,9); printf("2 : 2.txt");

gotoxy(20,11); printf("3 : 3.txt");

gotoxy(20,13);printf("4 : 4.txt");

gotoxy(20,15);printf("choose the text'number to edit:");

scanf("%d",&c);

switch(c)

{

case 1:{texte(str[0]);break;}

case 2:{texte(str[1]);break;}

case 3:{texte(str[2]);break;}

case 4:{texte(str[3]);break;}

}

}

void texte(char *str)

{

FILE *fp;

int x,y,i=0;

char buf[880],c='/0';

if((fp=fopen(str,"w"))==NULL)

{

textmode(C80);textbackground(BLACK);clrscr();

gotoxy(20,13);printf("Open file error! Press any key to Back.");

getch();

Manager_paper();

return;

}

textmode(C80); window(1,1,80,25);textbackground(BLUE); clrscr();

while(key=bioskey(0))

{

switch(key)

{

case ESC:

x=wherex(); y=wherey();

window(20,8,60,10);textbackground(BLUE);clrscr();

printf("Save file?type y to save:");

scanf("%c",&c);

if(c=='y')

{

buf[i++]='/0';

fputs(buf,fp);

fclose(fp);

Manager_paper();

return;

}

else if(c!='/0')

{

window(20,8,60,10),textbackground(BLUE);clrscr();

window(1,1,80,25);gotoxy(x,y);

}

break;

case BACKSPACE:

case KEY_LEFT:

x=wherex();y=wherey();

gotoxy(x-1,y);

clreol();

i--;

break;

case ENTER:

buf[i++]='/n';

printf("/n");

break;

default:

buf[i++]=key&0xff;

printf("%c",key&0xff);

}

}

}

void Score_list()

{

int i,k,n;

float max;

record a[10];

float score[10];

FILE *fp;

char ch;

window(1,1,80,25);

textbackground(1);

clrscr();

window(1,1,80,3);

textcolor(RED);

clrscr();

gotoxy(30,2);

cprintf("Seek The rank");

window(1,4,80,25);

textcolor(14);

clrscr();

fp=fopen("score.txt","r");

for(i=0;!feof(fp);i++)

fread(&a[i],sizeof(record),1,fp);

fclose(fp);

gotoxy(20,1);

cprintf("   rank      name      score     ");

for(n=0;n<i-1;n++){

max=0;

while(a[max].sco==0)

max++;

for(k=max+1;k<i;k++)

if(a[max].sco<a[k].sco)

max=k;

gotoxy(21,3+2*n);

cprintf("  %d   %10s     %3.3f  ",n+1,a[max].name,a[max].sco);

delay(300);

a[max].sco=0;

}

delay(1800);

getch();

window(12,9,68,14);

textbackground(15);

textcolor(4);

clrscr();

Main_menu();

}

头文件“key.h”:

#define  KEY_UP  18432
#define  KEY_DOWN 20480
#define  KEY_LEFT 19200
#define  KEY_RIGHT 19712
#define  BACKSPACE 3592
#define  ENTER  7181
#define  ESC  283
#define  END  20224

#define F1 15104
#define F2 15360
#define F3 15616
#define F4 15872
#define F5 16128
#define F6 16384
#define F7 16640
#define F8 16896
#define F9 17152
#define F10 17408
#define F11 17664
#define F12 17920

#define KEY_a 7777
#define KEY_b 12386
#define KEY_c 11875
#define KEY_d 8292
#define KEY_e 4709
#define KEY_f 8550
#define KEY_g 8807
#define KEY_h 9064
#define KEY_i 5993
#define KEY_j 9322
#define KEY_k 9579
#define KEY_l 9836
#define KEY_m 12909
#define KEY_n 12654
#define KEY_o 6255
#define KEY_p 6512
#define KEY_q 4209
#define KEY_r 4978
#define KEY_s 8051
#define KEY_t 5236
#define KEY_u 5749
#define KEY_v 12150
#define KEY_w 4471
#define KEY_x 11640
#define KEY_y 5497
#define KEY_z 11386
#define KEY_A 7745
#define KEY_B 12354
#define KEY_C 11843
#define KEY_D 8260
#define KEY_E 4677
#define KEY_F 8518
#define KEY_G 8775
#define KEY_H 9032
#define KEY_I 5961
#define KEY_J 9290
#define KEY_K 9547
#define KEY_L 9804
#define KEY_M 12877
#define KEY_N 12622
#define KEY_O 6223
#define KEY_P 6480
#define KEY_Q 4177
#define KEY_R 4946
#define KEY_S 8019
#define KEY_T 5204
#define KEY_U 5717
#define KEY_V 12118
#define KEY_W 4439
#define KEY_X 11608
#define KEY_Y 5465
#define KEY_Z 11354

#define KEY_1 20273
#define KEY_2 20530
#define KEY_3 20787
#define KEY_4 19252
#define KEY_5 19509
#define KEY_6 19766
#define KEY_7 18231
#define KEY_8 18488
#define KEY_9 18745
#define KEY_0 21040
#define NUM_1   561
#define NUM_2   818
#define NUM_3  1075
#define NUM_4  1332
#define NUM_5  1589
#define NUM_6  1846
#define NUM_7  2103
#define NUM_8  2360
#define NUM_9  2617
#define NUM_0  2864
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: