一.这一部曲是紧接第二部没有介绍完的内容,主要是函数接口实体的实现,代码比较多,如果没有看前两部曲的先去看看,再来看这里,不然不好理解,话不说多上代码,
1 #define _CRT_SECURE_NO_WARNINGS 2 #include "dict.h" 3 4 5 6 7 8 /**************函数实现************************/ 9 /*统计文件中单词数量,返回单词数量*/ 10 int statWordCount(char *filaename) 11 { 12 FILE *F = fopen(filaename, "r"); 13 if (F == NULL) 14 { 15 printf("文件打开失败"); 16 return -1; 17 } 18 else 19 { 20 int num = 0; 21 while (!feof(F)) 22 { 23 char buf[275]; 24 fgets(buf, 275, F); 25 num++; 26 } 27 fclose(F); 28 return num / 2;//两行对应一个单词 29 30 } 31 } 32 /*加载文本词库文件*/ 33 void loadTextFile(char *filename) 34 { 35 LEN = statWordCount(filename);//计算单词数量 36 mallocForSArra(LEN);//为单词结构体数组分配内存 37 FILE *F = fopen(filename, "r"); 38 if (F == NULL) 39 { 40 printf("加载文件失败"); 41 return; 42 } 43 createInitArray(F);//创建并初始化结构体数组 44 printf("OK "); 45 46 47 48 } 49 /*加载二进制词库文件*/ 50 void loadBinFile(char *filename) 51 { 52 FILE *F = fopen(filename, "rb"); 53 if (F == NULL) 54 { 55 printf("打开文件失败"); 56 return; 57 } 58 59 createInitBinArr(F); 60 61 62 63 64 } 65 /*为长度为len的结构体数组分配内存*/ 66 void mallocForSArra(int len) 67 { 68 DICT = (SWORD *)malloc(sizeof(SWORD)*len); 69 } 70 /*对结构体dict数组进行排序,使用插入排序*/ 71 void sortStructArr(SWORD * dict) 72 { 73 SWORD SW; 74 int j; 75 int i; 76 for (i = 1; i < LEN; i++) 77 { 78 SW = dict[i]; 79 for (j = i; j>0 && wordcmp(dict[j-1],SW) > 0; j--) 80 { 81 dict[j] = dict[j - 1]; 82 } 83 dict[j] = SW; 84 } 85 printf("ok"); 86 87 } 88 /*输入模块,返回输入字符串*/ 89 char *getInput(char *p) 90 { 91 92 scanf("%s",p); 93 //printf("%s", p); 94 return p; 95 } 96 /*输出模块,输出一个结构体*/ 97 void setOutput(SWORD *word) 98 { 99 if (word == NULL) 100 { 101 printf("没有找到您输入的单词 "); 102 return; 103 } 104 printf(" %s", word->key); 105 int len = word->count_trans; 106 printf(" 解释: "); 107 for (int i = 0; i < len; i++) 108 { 109 printf(" %s", word->trans[i]); 110 } 111 112 } 113 /*查询模块,使用二分法,据关键词key在词库里查询,找到返回这个结构体位置,否则返回NULL*/ 114 SWORD *searchWord(char *key) 115 { 116 //printf("%s ",key); 117 //int low = 0; 118 //int high = LEN; 119 //while (low < high) 120 //{ 121 // int mid = (low + high) / 2; 122 123 // if (strcmp(key,DICT[mid].key) == 0) 124 // { 125 // printf("%s", DICT[mid].key); 126 // return DICT + mid; 127 128 // } 129 // else if (strcmp(key, DICT[mid].key) > 0) 130 // { 131 // low = mid + 1; 132 // } 133 // else 134 // { 135 // high = mid - 1; 136 // printf("%s*********", DICT[mid].key); 137 // 138 // } 139 //} 140 //int len = strlen(key); 141 //int dictlen = strlen(DICT[0].key); 142 //printf("%d %d", len, dictlen); 143 //printf("%s*********", DICT[0].key); 144 for (int i = 0; i < LEN; i++) 145 { 146 if (strcmp(key, DICT[i].key) == 0) 147 { 148 return DICT + i; 149 } 150 151 152 153 } 154 155 return NULL; 156 157 } 158 /*从输入获取要添加的单词*/ 159 void getWord(SWORD *word) 160 { 161 162 163 164 printf("请输入要添加的单词(输入#over结束添加):"); 165 scanf("%s", word->key); 166 167 printf("%s", word->key); 168 169 printf("请输入解释数量:"); 170 scanf("%d", &(word->count_trans)); 171 for (int i = 0; i < word->count_trans; i++) 172 { 173 printf("请输入第%d条解释:",i+1); 174 scanf("%s", (word->trans)[i]); 175 } 176 printf("hello"); 177 178 179 } 180 181 /*添加单词word到结构体数组,弃用*/ 182 void addWordToArr(SWORD *word) 183 { 184 //给词库添加单词 185 //int len = (*word).count_trans; 186 187 //puts((*word).key); 188 //for (int i = 0; i < len; i++) 189 //{ 190 // puts((*word).trans[i]); 191 //} 192 193 194 //printf("%d ", LEN); 195 //printf("%s ", DICT[LEN - 1].key); 196 //printf("%d ", DICT[LEN - 1].count_trans); 197 //for (size_t i = 0; i < DICT[LEN - 1].count_trans; i++) 198 //{ 199 // printf("%s ", DICT[LEN - 1].trans[i]); 200 //} 201 LEN = LEN + 1; 202 DICT = (SWORD *)realloc(DICT, LEN); 203 DICT[10000 - 1] = *word; 204 205 206 //puts((*word).key); 207 //for (int i = 0; i < len; i++) 208 //{ 209 // puts((*word).trans[i]); 210 //} 211 //printf("%d ", (*word).count_trans); 212 213 //int keylen = strlen((*word).key)+1; 214 //分配单词空间并填充 215 //DICT[LEN - 1].key = (char *)malloc(sizeof(char) * keylen); 216 //strcpy(DICT[LEN - 1].key, word->key); 217 //DICT[LEN - 1].key[keylen - 1] = ' '; 218 ////解释长度 219 //int transnum = word->count_trans; 220 //DICT[LEN - 1].count_trans = transnum; 221 222 //DICT[LEN - 1].trans = (char **)malloc(sizeof(char *)*transnum); 223 ////解释空间分配与填充 224 //for (int i = 0; i < transnum; i++) 225 //{ 226 // int trlen = strlen(word->trans[i])+1; 227 // DICT[LEN - 1].trans[i] = (char *)malloc(sizeof(char)* trlen); 228 // strcpy(DICT[LEN - 1].trans[i], word->trans[i]); 229 // DICT[LEN - 1].trans[i][trlen - 1] = ' '; 230 //} 231 232 } 233 /*保存一个单词到文件*/ 234 void saveWord(char *filename,SWORD *sw) 235 { 236 FILE *F = NULL; 237 if (!_access(filename, 0))//如果文件存在 238 { 239 F = fopen(filename, "a"); 240 if (F == NULL) 241 { 242 printf("保存词库到文件失败"); 243 return; 244 } 245 246 char str[50]; 247 sprintf(str, "#%s", sw->key); 248 fputs(str, F); 249 250 char trans[200] = ""; 251 for (int i = 0; i < sw->count_trans; i++) 252 { 253 char strs[50]; 254 if (i == 0) 255 { 256 sprintf(strs, "Trans:%s", sw->trans[i]); 257 } 258 else 259 { 260 sprintf(strs, "@%s", sw->trans[i]); 261 } 262 strcat(trans, str); 263 264 } 265 fputs(trans, F);//将解释写入文件 266 267 268 fclose(F); 269 270 } 271 else 272 { 273 F = fopen(filename, "w"); 274 if (F == NULL) 275 { 276 printf("保存词库到文件失败"); 277 return; 278 } 279 280 char str[50]; 281 sprintf(str, "#%s", sw->key); 282 fputs(str, F); 283 284 char trans[200] = ""; 285 for (int i = 0; i < sw->count_trans; i++) 286 { 287 char strs[50]; 288 if (i == 0) 289 { 290 sprintf(strs, "Trans:%s", sw->trans[i]); 291 } 292 else 293 { 294 sprintf(strs, "@%s", sw->trans[i]); 295 } 296 strcat(trans, str); 297 298 } 299 fputs(trans, F);//将解释写入文件 300 301 302 fclose(F); 303 } 304 305 306 } 307 /*生成二进制的词库文件,30位字符块大小*/ 308 void createBinFile(SWORD *word) 309 { 310 FILE *F = fopen(dictbitname, "wb"); 311 if (F == NULL) 312 { 313 printf("打开文件失败"); 314 return; 315 } 316 printf("打开文件成功"); 317 fwrite(&LEN, sizeof(int), 1, F);//前四个字节存储单词个数 318 printf("写入长度成功"); 319 for (; word <= DICT+LEN-1; word += 1) 320 { 321 fwrite(word->key, sizeof(char),50, F); 322 //printf("写入单词成功"); 323 fwrite(&(word->count_trans), sizeof(int),1, F); 324 325 //printf("写入解释数量%d成功", word->count_trans); 326 for (int i = 0; i < word->count_trans; i++) 327 { 328 329 330 fwrite(word->trans+i, sizeof(char), 50, F); 331 //printf(" 写入解释成功"); 332 333 334 335 } 336 } 337 fclose(F); 338 339 } 340 341 /*计算一个字符串里某个ch字符的个数*/ 342 int calcNumber(char *str,char ch) 343 { 344 int num = 0; 345 for (char *p =str; *p != '