gpt4 book ai didi

c - 为什么没有打印任何内容? C语言编程

转载 作者:行者123 更新时间:2023-11-30 15:34:29 25 4
gpt4 key购买 nike

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

这是congress.txt中的内容:

Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the government for a redress of grievances.

#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(" ");
}

}

最佳答案

您的代码有几个问题 - 您的编译器会提示其中许多问题。

首先 - 对于声明为 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;
}

关于c - 为什么没有打印任何内容? C语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23259309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com