请帮修改程序
本帖最后由 风过无痕1989 于 2020-8-18 23:42 编辑下面的程序,测试过,没有问题,只是输入太过麻烦,不实用,请帮用 fopen 语句逐字符录入数组之中,文件存放在 C:\ important.txt
#include <stdio.h>
#define Len 100 // 定义文件长度
int main()
{
int i;
char cipher_text; // 密文数组
char original_text; // 原文数组
for (i = 0;i < Len;i++)
{
cipher_text = getchar(); // 将密文存入密文数组
getchar();
}
for (i = 0;i < Len;i++) // 逐个元素进行解密
{
if (cipher_text >= 65 && cipher_text <= 90)
{
original_text = 90 - cipher_text + 65;
}
else if (cipher_text >= 97 && cipher_text <= 122)
{
original_text = 122 - cipher_text + 97;
}
else
original_text = cipher_text;
}
for (i = 0;i < Len;i++) // 输出加密文件的原文
{
printf("%c",original_text);
}
printf("\n");
}
本帖最后由 chxchxkkk 于 2020-8-19 00:58 编辑
进行了略微的修改,加入了读取文件
#include <stdio.h>
#include <stdlib.h>
#define Len 100 // 定义文件长度
int main()
{
int i, num;
char cipher_text; // 密文数组
char original_text; // 原文数组
FILE *fp = fopen("C:\\important.txt", "r");
if (!fp)
{
printf("文件读取失败!\n");
exit(1);
}
fgets(cipher_text, Len, fp);
printf("%s\n", cipher_text);
num = strlen(cipher_text);
for (i = 0;i < num;i++) // 逐个元素进行解密
{
if (cipher_text >= 65 && cipher_text <= 90)
{
original_text = 90 - cipher_text + 65;
}
else if (cipher_text >= 97 && cipher_text <= 122)
{
original_text = 122 - cipher_text + 97;
}
else
original_text = cipher_text;
}
printf("%s\n",original_text);
fclose(fp);
getchar();
return 0;
} 你看这个行不行?#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
char convert(char c) {
if (c >= 65 && c <= 90) {
return 90 - c + 65;
}
else if (c >= 97 && c <= 122) {
return122 - c + 97;
}
else {
return c;
}
}
int main() {
FILE* input = fopen("i don't know the path", "r"), * output = fopen("C:\\important.txt", "w");
char temp;
if (input && output) {
while ((temp = fgetc(input)) != EOF) {
fputc(convert(temp), output);
fgetc(input);
}
return 0;
}
return 1;
} 永恒的蓝色梦想 发表于 2020-8-19 11:06
你看这个行不行?
2楼的已经达到了我的要求,只是有点小问题,我在修改调试,花了一点时间。我先回复他的,再来试你的程序,若你的不需要修改,最佳就是你的,若你的也是需要修改,我只好选他了,请理解 chxchxkkk 发表于 2020-8-19 00:57
进行了略微的修改,加入了读取文件
你的程序我试了,少用了 #include <string.h> 这个声明,导致编译报错,其次,我已经定义了一个文件长度,你在没有去掉的情况下,另用了一个 num,这就导致了显示出来,中间有乱码。下面是我修改后,正常的程序:
#if(1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i,len;
char cipher_text; // 密文数组
char original_text; // 原文数组
FILE *fp = fopen("C:\\important.txt", "r");
if (!fp)
{
printf("文件读取失败!\n");
exit(1);
}
len = strlen(cipher_text);
fgets(cipher_text, Len, fp);
printf(" 加密的文件如下:\n");
printf(" %s\n", cipher_text);
for (i = 0;i < len;i++) // 逐个元素进行解密
{
if (cipher_text >= 65 && cipher_text <= 90)
{
original_text = 90 - cipher_text + 65;
}
else if (cipher_text >= 97 && cipher_text <= 122)
{
original_text = 122 - cipher_text + 97;
}
else
original_text = cipher_text;
}
printf(" 解密的文件如下:\n");
printf(" %s\n",original_text);
fclose(fp);
getchar(); // 让系统处于等待状态,去掉了 Press any key . . .
return 0;
}
#endif
风过无痕1989 发表于 2020-8-19 11:37
2楼的已经达到了我的要求,只是有点小问题,我在修改调试,花了一点时间。我先回复他的,再来试你的程序 ...
{:10_277:}其实这个程序根本不需要开数组 永恒的蓝色梦想 发表于 2020-8-19 12:17
其实这个程序根本不需要开数组
你那个程序我试了,一闪就直接 Press any key to continue ...
我打开C看,我保存的加密文件 important.txt,被清空了,再将加密文件找回来。原本,我那个文件是解密文件,反过来,将原文件加密,应该也是没有问题的,可不知为何,将原用这个程序来加密,总有几个码是错的 风过无痕1989 发表于 2020-8-19 12:53
你那个程序我试了,一闪就直接 Press any key to continue ...
我打开C看,我保存的加密文件 importan ...
你给个测试文件啊{:10_277:} 永恒的蓝色梦想 发表于 2020-8-19 12:17
其实这个程序根本不需要开数组
能不开数组,当然更好! 只是不开数组,字符串操作容易出错,我等你帮写个用字符串操作的(字符串操作,我看了几遍视频,又看了几遍书,仍然是七窍通了六窍。你写成了,我正好学习学习,最佳答案就是你的) 风过无痕1989 发表于 2020-8-19 12:53
你那个程序我试了,一闪就直接 Press any key to continue ...
我打开C看,我保存的加密文件 importan ...
你这要求说的根本不明确啊{:10_277:}
请帮用 fopen 语句逐字符录入数组之中,文件存放在 C:\ important.txt我还可以理解成把解密结果输出到 C:\ important.txt 永恒的蓝色梦想 发表于 2020-8-19 12:57
你给个测试文件啊
Lm Lxglyvi 1, 1949. Xszrinzm Nzl Avwlmt hlovnmoh wvxozivw gl gsv dliow: gsv Kvlkov'h Ivkfyorx lu Xsrmz dzh ulfmwvw zmw gsv Xsrmvhv kvlkov szev hgllw fk hrmxv gsvm!
On October 1, 1949, Chairman Mao Zedong solemnly declared to the world: the People's Republic of China was founded and the Chinese people have stood up since then! 本帖最后由 永恒的蓝色梦想 于 2020-8-19 14:50 编辑
风过无痕1989 发表于 2020-8-19 13:04
Lm Lxglyvi 1, 1949. Xszrinzm Nzl Avwlmt hlovnmoh wvxozivw gl gsv dliow: gsv Kvlkov'h Ivkfyorx lu X ...
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
char convert(char c) {
if (c >= 65 && c <= 90) {
return 90 - c + 65;
}
else if (c >= 97 && c <= 122) {
return122 - c + 97;
}
else {
return c;
}
}
int main() {
FILE* file = fopen("C:\\important.txt", "r");
char temp;
if (file) {
for (;;) {
temp = fgetc(file);
if (EOF == temp) {
return 0;
}
putchar(convert(temp));
}
return 0;
}
return 1;
} 永恒的蓝色梦想 发表于 2020-8-19 13:11
我试过了,没有问题。谢谢!
再多请教一个问题:怎么用类似 FILE* file = fopen("C:\\translated.txt", "w"); 的语句直接写到硬盘? 风过无痕1989 发表于 2020-8-19 14:41
我试过了,没有问题。谢谢!
再多请教一个问题:怎么用类似 FILE* file = fopen("C:\\translated.txt", ...
额,什么叫“直接写到硬盘”? 风过无痕1989 发表于 2020-8-19 14:41
我试过了,没有问题。谢谢!
再多请教一个问题:怎么用类似 FILE* file = fopen("C:\\translated.txt", ...
第一个参数改成文件路径就可以了 永恒的蓝色梦想 发表于 2020-8-19 14:54
第一个参数改成文件路径就可以了
122 FILE* file = fopen("C:\\translated.txt", "w");
123 putchar(convert(temp));
加上122句就报错了:error C2275: 'FILE' : illegal use of this type as an expression 风过无痕1989 发表于 2020-8-19 14:58
122 FILE* file = fopen("C:\\translated.txt", "w");
123 putchar(convert(temp));
...
去网上查了一下,老的编译器声明必须在函数体最前面。
换编译器吧。
页:
[1]