C语言 汉字转拼音
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <windows.h> #define N 100 int cishu(char *a,char *b) { int n=0; while(*a) { if(*a==*b) n++; a++; } return n; } char * retpinyin() { static char name[10]; srand((unsigned)time(NULL)); int iRange1 = 0xf7 - 0xb0; int iRange2 = 0xfe - 0xa1; char iCode1 = rand()%iRange1 + 0xb0; char iCode2 = rand()%iRange2 + 0xa1; char chh[3] = {iCode1,iCode2,0}; strcpy(name,chh); return name; } int StrReplace(char strRes[],char from[], char to[]) { int i,flag = 0; char *p,*q,*ts; for(i = 0; strRes[i]; ++i) { if(strRes[i] == from[0]) { p = strRes + i; q = from; while(*q && (*p++ == *q++)); if(*q == '\0') { ts = (char *)malloc(strlen(strRes) + 1); strcpy(ts,p); strRes[i] = '\0'; strcat(strRes,to); strcat(strRes,ts); free(ts); flag = 1; } break; } } return flag; } int main() { int zongshu=0,zhengque=0,cuo=0; int zheng=0,bz=0; int a=0; FILE *fp; char str[N + 1],ch[2],ch1[20],ch2[20],chx[10],str1[N+1]; char * chh; char *p; //判断文件是否打开失败 if ( (fp = fopen("duizhao1.txt", "rt")) == NULL ) { puts("Fail to open file!"); exit(0); } while(1) { if(zongshu%5==0 && zongshu!=0) { system("cls"); if(zheng==2) { printf("%s\n",str,str1); } printf("共计做对=%d个,错误=%d\n",zhengque,cuo); } zheng=0; chh=retpinyin(); //chh="阿"; printf("%s\n",chh); scanf("%s",&chx); if(strcmp(chx,"1")==0) break; while( fgets(str, N, fp) != NULL ) { if(strstr(str,chh)!=0) { strcpy(str1,str); StrReplace(str1,chh,""); StrReplace(str1," ",""); StrReplace(str1,"\n",""); if(cishu(str1," ")>=1) { p = strtok(str1, " "); while(p) { if(strcmp(p,chx)==0) { bz++; } //printf("....kkk%s\n", p); p = strtok(NULL, " "); } if(bz>=1) { zheng=1; zhengque++; printf("对了!\n"); printf("共计做对=%d个,错误=%d\n",zhengque,cuo); } else { cuo++; printf("错了!\n"); printf("正确答案:%s\n",str1); printf("共计做对=%d个,错误=%d\n",zhengque,cuo); zheng=2; } } else { if(strcmp(str1,chx)==0) { zheng=1; zhengque++; printf("对了!\n"); printf("共计做对=%d个,错误=%d\n",zhengque,cuo); } else { cuo++; printf("错了!\n"); printf("正确答案:%s\n",str1); printf("共计做对=%d个,错误=%d\n",zhengque,cuo); zheng=2; } break;} break; } } if(zheng==0) { printf("对照表中没有%s\n",chh); zheng=0; } rewind(fp); // fp回到开始位置 zongshu++; } fclose(fp); return 0; }