2014-04-24 85 views
0

我正在製作一個程序,它將congress.txt中的字符全部大寫,然後「將它們轉換爲兩個字符」,(A轉到C)(Z轉到B)。但是沒有任何內容正在打印,我主要關心的是如果我的數組正在被存儲並傳遞給不同的功能。爲什麼沒有打印? C編程

這是在congress.txt

國會不得制定法律尊重建立宗教或禁止自由行的;或者刪除言論自由或新聞自由;或和平集會的人民的權利,並請求政府糾正不滿。

#include<stdio.h> 

int processFile(int *store); 
int cipher(int *store, int *code); 
int outputCode(int *code); 

int main(void){ 
    int store[300], code[300], i; 

    processFile(store); 
    cipher(store, code); 
    outputCode(code); 
    getchar(); 
    return 0; 
} 

void processFile(int *store){ 
    int i, a = 0; 
    FILE *f = fopen("congress.txt", "r"); 

    for (i = 0; a != EOF;){ 
     fscanf(f, "%c", &a);  //store character in a 
     if (a <= 'Z' && a >= 'A'){ //store uppercase letters 
      store[i] = a; 
      i++; 
     } 
     if (a <= 'z' && a >= 'a'){ //store lowercase letters as uppercase 
      store[i] = a - 32; 
      i++; 
     } 
    } 
    i++; 
    store[i] = '\0'; 
} 

void cipher(int *store, int *code){ 
    int i; 

    for (i = 0; store[i] != 0; ++i){ 
     if (store[i] <= 'X' && store[i] >= 'A'){ //tests to see if the letter is between A and X 
      code[i] = (char)(store[i] + 2);   //shifts letter by two characters 
     } 
     if (store[i] >= 'Y' && store[i] <= 'Z'){ 
      code[i] = (char)(store[i] - 24);  //shifts Y and Z to A or B respectively 
     } 
    } 
} 

void outputCode(int *code){ 
    int i, a, b; 
    for (a = 0; code[a] != 0; ++a){ 
     if (!(a % 50)){        //makes a newline every 50 characters 
      printf("\n"); 
     } 
     for (b = 0; code[a] != 0 && b <= 5; ++b){ //prints chunks of 5 characters then makes a space 
      printf("%c", code[a]); 
     } 
     printf(" "); 
    } 

} 
+1

你應該明確提出終止''\ 0''你在'processFile'字符串的結尾... – Floris

+1

你或許應該改變你的'回報; '返回0;'或者因爲函數必須返回一個'int'。另外,你的其他函數也應該返回一個值。如果你不需要,將返回類型改爲'void'。我建議編譯您的代碼,並啓用所有警告以儘早檢測這些微小的錯誤。 – Rufflewind

+0

弗洛里斯會編輯我即將做到這一點? – ShaneBird

回答

1

有幾件事情你的代碼錯誤 - 其中許多你的編譯器會抱怨。

要開始 - 您沒有聲明int的函數的返回值。只要讓他們void,或返回的東西。

第二 - 你聲明int a;,但繼續使用它像一個char。聲明它如何使用它。

第三 - 文件結束測試是用feof(f)而不是a != EOF完成的。

四 - 當你輸出你的代碼,你需要增加a,否則,你得到相同的值的五倍:

​​

五 - 你的打印程序並不能保證它會停止 - 如果你有一個單一的'\0'後跟其他垃圾,你會打印更多的垃圾(除非它發生在5的倍數)。你需要用零填充你的密碼。

所以 - 工作代碼:

#include<stdio.h> 

int processFile(int *store); 
int cipher(int *store, int *code); 
int outputCode(int *code); 

int main(void){ 
    int store[300], code[300], i; 

    processFile(store); 
    cipher(store, code); 
    outputCode(code); 
    printf("\n=====\n\n"); 
    return 0; 
} 

int processFile(int *store){ 
    int i; 
    char a = 0; 
    FILE *f = fopen("congress.txt", "r"); 

    for (i = 0; !feof(f) && i<299;){ 
     fscanf(f, "%c", &a);  //store character in a 
     if (a <= 'Z' && a >= 'A'){ //store uppercase letters 
      store[i] = a; 
      i++; 
     } 
     if (a <= 'z' && a >= 'a'){ //store lowercase letters as uppercase 
      store[i] = a - 32; 
      i++; 
     } 
    } 
    store[i]='\0'; 
    return 0; 
} 

int cipher(int *store, int *code){ 
    int i; 

    for (i = 0; store[i] != 0; ++i){ 
     if (store[i] <= 'X' && store[i] >= 'A'){ //tests to see if the letter is between A and X 
      code[i] = (char)(store[i] + 2);   //shifts letter by two characters 
     } 
     if (store[i] >= 'Y' && store[i] <= 'Z'){ 
      code[i] = (char)(store[i] - 24);  //shifts Y and Z to A or B respectively 
     } 
    } 
    for(; i<300; i++) code[i]=0; // pad with zeros 
    return 0; 
} 

int outputCode(int *code){ 
    int i, a, b; 
    for (a = 0; code[a] != 0; ++a){ 
     if (!(a % 50)){        //makes a newline every 50 characters 
      printf("\n"); 
     } 

     for (b = 0; code[a] != 0 && b <= 5; ++b){ //prints chunks of 5 characters then makes a space 
      printf("%c", code[a++]); 
     } 
     printf(" "); 
    } 
return 0; 
} 
+0

你是弗洛里斯的男人! (或女人,我不知道);) – ShaneBird

+0

我也能夠注意到另一個小錯誤,雖然它是一個重要的錯誤。在outputCode中,第一個for循環在完成時不應該增加,因爲它會跳過a的值。 – ShaneBird

+1

@shanebird - 很高興你能工作,並注意到了額外的錯誤。這就是編程的工作原理 - 你修正了一件事,另一件顯示出來... – Floris