#include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #define N 10004
typedef struct { char word[16]; int lens; }d; d dic[N];
int cmp(const void *p, const void *q) { return strcmp((char *)p, (char *)q); }
int main() { int i, j, n, count, k, clen; char ch[16], dic2[N][16], check[16],*p; i = 0;
freopen("in.txt", "r", stdin); while(strcmp(gets(dic[i].word), "#")) { dic[i].lens = strlen(dic[i].word); i++; } n = i; for(i=0; i<n; i++) strcpy(dic2[i], dic[i].word); //用bsearch的时候需要把数组排序,
qsort(dic2, n, sizeof(dic2[0]), cmp); //而后面处理各种情况的时候又要用到原来的顺序,所以把字典备份一个
while(strcmp(gets(check), "#")) { p = (char *)bsearch(&check, dic2, i, sizeof(dic2[0]), cmp); if(p) printf("%s is correct", check); //找到有相同的 else { clen = strlen(check); printf("%s:", check); for(i=0; i<n; i++) { count = 0; if(dic[i].lens == clen) //长度相同 { for(j=0; j<dic[i].lens; j++) //看看不同的字母有几个 { if(dic[i].word[j] != check[j]) count++; if(count > 1) break; } if(count == 1) //如果只有一个 printf(" %s", dic[i].word); } else if(dic[i].lens == clen+1) //被检查的长度小 { strcpy(ch, dic[i].word); for(j=0; j<dic[i].lens; j++) { for(k=j; k <dic[i].lens; k++) dic[i].word[k] = dic[i].word[k+1]; //从第一个到最后一个字母去掉试试
if(strcmp(dic[i].word, check) == 0) //如果跟被检查的单词相同了,就输出
{ printf(" %s", ch); strcpy(dic[i].word, ch); break; } strcpy(dic[i].word, ch); }
} else if(dic[i].lens == clen-1) //被检查的长度大 原理同上 { strcpy(ch, check); for(j=0; j<clen; j++) { for(k=j; k<clen; k++) check[k] = check[k+1]; if(strcmp(dic[i].word, check) == 0) { printf(" %s", dic[i].word); strcpy(check, ch); break; } strcpy(check, ch); } } } } printf("\n"); } getch(); return 0; }
|