C语言 拼音转汉字
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <windows.h> #define N 100 void del_char(char a[],char c)//删除字符串中的小写字母 { int i,j; for(i=0,j=0; *(a+i)!='\0'; i++) { if(*(a+i)>='a'&& *(a+i)<='z') continue; else { *(a+j)=*(a+i); j++; } } *(a+j)='\0'; } void del_char1(char a[],char c)//删除字符串中的除小写字母 和空格外的所有字符 { int i,j; for(i=0,j=0; *(a+i)!='\0'; i++) { if(*(a+i)>='a'&& *(a+i)<='z'|| *(a+i)==' ') { *(a+j)=*(a+i); j++; } else { continue; } } *(a+j)='\0'; } 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],str3[N+1]; char * chh; char *p; //判断文件是否打开失败 if ( (fp = fopen("duizhao1.txt", "rt")) == NULL ) {//确认正确打开了对照表,否则报错 puts("Fail to open file!"); exit(0); } while(1) { char str2[N+1]={0};//字符数组每循环一次赋一次初值 char str3[N+1]={0};//字符数组每循环一次赋一次初值 scanf("%s",&chx);//输入要检查的拼音 if(strcmp(chx,"1")==0)//结束标志 break; while( fgets(str, N, fp) != NULL ) //指针从头到尾依次变化 { bz=0; //printf("%s\n",str); if(strstr(str,chx)!=0)//检索对照表中每一行是否包含chx(输入的拼音) { strcpy(str1,str);//数组复制,str复制给str1 strcpy(str3,str); StrReplace(str1,"\n","");//将换行符替换 StrReplace(str1," ",""); //printf("%s\n",str1); if(cishu(str1," ")>=1) { del_char1(str1,' '); p = strtok(str1, " "); while(p) { if(strcmp(p,chx)==0) { bz++; } //printf("....kkk%s\n", p); p = strtok(NULL, " "); } if(bz>=1) { strcpy(str1,str); StrReplace(str1,"\n","");//将换行符替换 StrReplace(str1," ",""); del_char(str1,' '); strcat(str2, str1);//数组的连接,完成类似功能:str2=str2+str1 } } else { StrReplace(str3,"\n","");//将换行符替换 StrReplace(str3," ",""); del_char1(str3,' '); if(strcmp(str3,chx)==0) { strcpy(str3,str); StrReplace(str3,"\n","");//将换行符替换 StrReplace(str3," ",""); del_char(str3,' '); strcat(str2, str3); } } } } printf("%s\n",str2); //输出最后结果 rewind(fp); // fp回到开始位置 zongshu++; } fclose(fp);//关闭指针 return 0; }