用C语言写了一个用来背单词的系统,需要自己先把要背的单词利用记事本写入创建的文件中,然后打开此代码串,运行该代码进行单词的背写。
把源码站在下面
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define CODE 0x3b
#define MAXLEN 100
typedef struct
{
char *english;
char *chinese;
}Word;
Word word[MAXLEN];
int testType = 0;
int init()
{
FILE *fp;
char fname[50];
char line[MAXLEN];
char *p;
char *word1;
char *word2;
int i=0;
int j;
int len;
printf("\t欢迎使用英语单词标准化测!\n");
printf("=============================================\n");
printf("请输入测试用的单词的库文件名:");
scanf("%s",fname);
if((fp = fopen(fname,"rb")) ==NULL)
{
printf("不能打开单词库文件%s,请检查!",fname);
exit(0);
}
fflush(stdin);
while(fgets(line,MAXLEN,fp))
{
for(j=0;j<strlen(line);j++)
{
if(line[j]!='\n')
{
line[j] = line[j]^CODE;
}
}
p = strchr(line,'\n');
if(p!=NULL)
{
*p ='\0';
}
p=strchr(line,'=');
word1 =line;
word2 =p;
*p='\0';
++word2;
len=strlen(word1);
word[i].english = (char *)malloc(len+1);
strcpy(word[i].english,word1);
len = strlen(word2);
word[i].chinese = (char *)malloc(len+1);
strcpy(word[i].chinese,word2);
i++;
}
fclose(fp);
return 1;
}
void endtest(int size)
{
int i;
for(i=0;i<size;i++)
{
free(word[i].english);
word[i].english=NULL;
free(word[i].chinese);
word[i].chinese = NULL;
}
}
void test(int size)
{
int i,j,t;
int flag;
char choice;
int xm[4];
int count = 0;
int score = 0;
printf("请选择测试类型(1-中文找英文 0-英文找中文):");
scanf("%d",&testType);
fflush(stdin);
for(i=0;i<size;i++)
{
system("cls");
if(!testType)
{
printf("%d.英文%s的中文含义是:\n",i+1,word[i].chinese);
}
else
{
printf("%d.中文%s的英文含义是:\n",i+1,word[i].chinese);
}
srand(time(NULL));
count= 0;
while (count<=4)
{
t = rand()%size;
if(t==1)
{
continue;
}
flag = 0;
for(j =0;j<count;j++)
{
if(xm[j]==t)
{
flag=1;
}
}
if(!flag)
{
xm[count] = t;
count++;
}
}
t =rand()%4;
xm[t]=i;
if(!testType)
{
printf("\tA. %s\n",word[xm[0]].chinese);
printf("\tB. %s\n",word[xm[1]].chinese);
printf("\tC. %s\n",word[xm[2]].chinese);
printf("\tD. %s\n",word[xm[3]].chinese);
}
else
{
printf("\tA. %s\n",word[xm[0]].english);
printf("\tB. %s\n",word[xm[1]].english);
printf("\tC. %s\n",word[xm[2]].english);
printf("\tD. %s\n",word[xm[3]].english);
}
printf("请输入你的选择:");
choice = getchar();
fflush(stdin);
if(choice>='a'&&choice<='z');
{
choice = choice-'a'+'A';
if(choice-'A'==t)
{
printf("恭喜你答对了!\n");
score++;
}
else
{
printf("抱歉你答错啦!");
printf("正确答案是%c。\n",'A'+t);
}
}
getchar();
}
printf("本次测试结束,您共测试了%d道题,答对了%d道!\n\n欢迎下次继续使用!\n",size,score);
endtest(size);
}
int main(int argc, char *argv[])
{
int size = init();
test(size);
getchar();
return 0;
}