#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.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=0;
int len;
printf("\t welcome to the words test stuition\n");
printf("============================================\n");
printf("please input the fname:");
scanf("%s",fname);
if((fp=fopen(fname,"r"))==NULL)
{
printf("opened error");
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 i;
}
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("please choice;1---chinese to eng0-----eng to chin");
scanf("%d",&testType);
fflush(stdin);
for(i=0;i<size;i++)
{
if(!testType)
{
printf("%d.english--%s--means:\n",i+1,word[i].english);
}
else
{printf("%d.chin--%s--means:\n",i+1,word[i].chinese);
}
srand(time(NULL));
count=0;
while(count<=4)
{
t=rand()%size;
if(t==i)
{
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("\nA.%s\n",word[xm[0]].chinese);
printf("\nB.%s\n",word[xm[1]].chinese);
printf("\nC.%s\n",word[xm[2]].chinese);
printf("\nD.%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("please input your choice:");
choice=getchar();
fflush(stdin);
if(choice>='a'&&choice<='z')
{
choice=choice-'a'+'A';
}
if(choice-'A'==t)
{
printf("sorry,you are wromg");
score++;
}
else
{
printf("sorry,you are worng");
printf("the correct answer is:%c.\n",'A'+t);
}
getchar();
}
printf("the game is over. amoutis %d. correct is %d\n\nwelcome next time\n",size,score);
}
int main(void)
{
int size=init();
test(size);
getchar();
return 0;
}