[英]Encode-decode find a glitch pls
我有一個編碼器/解碼器代碼,它使用帶有空格的單詞轉儲作為結尾,並在其末尾帶有“ \\ n”作為輸入,並對文本中出現的前5個單詞進行編碼,反之亦然。 這似乎很吸引人,但是我的主管程序仍然有10次出現6次失敗。 我對輸入文件進行編碼,然后對編碼后的輸入文件進行解碼,並且效果很好。 仍然不明白這是怎么回事。 拜托,我需要你的眼睛!
簡單編碼器在文件中找到5個最常用的字符(至少3個字符長),並用短代碼替換。 代碼為2個字符長,看起來像這樣:!1!2 ...!5。 !1替換單詞出現次數最多的單詞,!5替換單詞出現次數最少的單詞。 如果兩個單詞出現相同,則代碼列表中的第一個單詞排在第一位(“越早越好”)。 其余的單詞必須保持不變。 在編碼文件的開頭,必須顯示代碼表。 該程序還必須具有解碼功能。 如果輸入以“!”開頭,則它必須調整代碼列表並解碼整個文件,以恢復原始狀態。 輸入:可以存在兩種輸入文件。 原始輸入最多包含2000個單詞,中間有空格,每個單詞最多22個字符。 在最后一個單詞之后,沒有空格,而是換行符(“ \\ n”)。 單詞由英文字母(全部小寫)組成。 非單詞中包含任何“!” 標志。 總有至少5個不同種類的單詞,至少3個字符。 警告! 如果輸入格式等於輸出格式,則當然需要解碼! 在解碼的情況下,當然,文件列表開頭的代碼表有效期為2000個單詞和最大22個字符。 輸出:第5行包含代碼列表。 第一個單詞是代碼,第二個單詞是它要替換的單詞。 替換字符之間的換行符之間的空白。 從第六行開始,需要解碼的編碼文本。 單詞之間的空格,末尾只有一個換行符。 警告! 輸出文件格式可以等於輸入文件格式! 在那種情況下,當然需要編碼。
要求:“ input.txt”用於讀取(只讀!),“ output.txt”(僅寫入!)用於編寫。 要成功運行,請返回0;否則,返回0。 在main()的末尾對於避免錯誤代碼是必需的。 可能的故障代碼:超出存儲時間限制; 浮點失敗,fe:除以零。 內存訪問失敗,索引建立數組,使用空指針。
/* Input.txt
o xxa o xxb xxb o xxc o xxd xxb xxe xxe
Output.txt
!1 xxb
!2 xxe
!3 xxa
!4 xxc
!5 xxd
o !3 o !1 !1 o !4 o !5 !1 !2 !2 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct words {
char *kod;
int occurrence;
} TABLE;
int main() {
FILE *data;
char wordmax[23];
TABLE *table = NULL;
int number = 0, i, m, n;
char c;
data = fopen("be.txt", "r" );
c = fgetc(data);
FILE *outfile;
outfile = fopen("ki.txt", "w");
int first = 1;
char codeReadIn[10][23];
if ( c == '!' ) {
data = fopen("be.txt", "r" );
for ( i = 0; i<10; i++){
fscanf(data, "%s", codeReadIn[i]);
}
while (fscanf(data, "%s", wordmax) != EOF ) {
if (first == 0){
fprintf(outfile, " ");
}
if (first == 1){
first = 0;
}
for (m=0; m<10; m = m + 2) {
if (strcmp(wordmax, codeReadIn[m]) == 0) {
fprintf(outfile, "%s", codeReadIn[m+1]);
break;
}
}
if (m==10) {
fprintf(outfile, "%s", wordmax);
}
}
fprintf(outfile, "\n");
} else {
data = fopen("be.txt", "r" );
while (fscanf(data, "%s", wordmax) != EOF ) {
for (i = 0; i < number; ++i){
if (strcmp(table[i].kod, wordmax) == 0){
break;
}
}
if (strlen(wordmax) <= 2){ // 2 char skip
continue;
}
if (i == number) {
++number;
table = (TABLE *)realloc(table, number * sizeof(TABLE));
table[i].kod = (char *)malloc((strlen(wordmax) + 1) * sizeof(char));
strcpy(table[i].kod, wordmax);
table[i].occurrence = 1;
}else{
++table[i].occurrence;
}
}
int maxOccurrences[5];
char* maxCodes[5];
int j, k ;
for(j = 0; j < 5; j++){ // search for the top5 among occurrences
maxOccurrences[j] = -1;
for (i = 0; i < number; ++i){ // going trough occurrences
// once put in top5, wont put it in again
int foundone = 0;
for (k = 0; k < j; k++){
if ( strcmp(maxCodes[k], table[i].kod) == 0){
foundone = 1;
}
}
if(foundone == 1){
continue;
} // search for max
if ( table[i].occurrence > maxOccurrences[j] ) { // if bigger then better
maxOccurrences[j] = table[i].occurrence;
maxCodes[j] = table[i].kod;
}
}
}
char* kod[5];
kod[0] = "!1";
kod[1] = "!2";
kod[2] = "!3";
kod[3] = "!4";
kod[4] = "!5";
for (i=0;i<5;i++) {
fprintf(outfile, "%s %s\n", kod[i], maxCodes[i]);
}
int m;
data = fopen("be.txt", "r" );
first = 1;
while (fscanf(data, "%s", wordmax) != EOF ) {
if(first == 0){
fprintf(outfile, " ");
}
if(first == 1){
first = 0;
}
for (m=0; m<j; m++) {
if (strcmp(wordmax, maxCodes[m]) == 0) {
fprintf(outfile, "%s", kod[m]);
break;
}
}
if (m==j) {
fprintf(outfile, "%s", wordmax);
}
}
fprintf(outfile, "\n");
for (i=0;i<number;++i){
free(table[i].kod);
}
free(table);
}
fclose(data);
fclose(outfile);
return 0;
}
您為什么忽略一兩個字母的單詞?
if (strlen(wordmax) <= 2){ // 2 char skip
continue;
}
並且使用strcasecmp()
代替strcmp()
會更好嗎,還是您實際上需要分別處理大小寫單詞?
編輯:這是一些測試用例:
輸入: one one two two three three four four five five
輸出:
!1 two
!2 three
!3 four
!4 five
!5 one
!5 !5 !1 !1 !2 !2 !3 !3 !4 !4
為什么“一個”在列表中排在最后? 這有問題嗎?
輸入: aaa bbb ccc ddd eee
輸出:
!1 bbb
!2 ccc
!3 ddd
!4 eee
!5 ^A
aaa !1 !2 !3 !4
那里發生了奇怪的事情
輸入: xxx yyy zzz
輸出:總線錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.