为什么没有打印? C编程

时间:2014-04-24 03:32:30

标签: c arrays printf

我制作一个程序,从congress.txt获取字符使它们全部为大写,然后"将它们移动两个字符",(A转到C)(Z转到B )。但是没有任何东西正在打印,我主要担心的是我的数组是否正在存储并正确传递给不同的函数。

这是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 :(得分:1)

您的代码有几个问题 - 编译器会抱怨其中很多。

要开始 - 您没有声明为int的函数的返回值。只需让它们void,或者返回一些东西。

第二 - 您声明int a;但继续像char一样使用它。声明你如何使用它。

第三 - 文件结尾的测试是使用feof(f)而不是a != EOF完成的。

第四 - 当您输出代码时,您需要增加a,否则您将获得相同的值五次:

VVVVVV JJJJJJ KKKKKK UUUUUU KKKKKK UUUUUU UUUUUU

第五 - 您的打印例程并不能保证它会停止 - 如果您有一个'\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;
}