C语言编程题目,各位大神帮帮我吧。

文件移位加密与解密(1人)
要求实现
1)文件加密;
2)文件解密。
备注:将某一已知文件的内容(仅限于英文字母)以字符形式读出,根据密钥(用户从键盘输入)将对应字符进行移位操作即可,解密时移动相反。
例如:加密:设原文为abcdef,密钥为5,则有abcdef每个字母按字母表向后移动5们(注:z后接a)可得到密文(乱码)fghijkl;对该文件解密:文件内容为fghijk1,密钥为5,则有fghijk1每个字母向前移动5位(注a后接z),可得到原文abcdef。

/*
备注:将某一已知文件的内容(仅限于英文字母)以字符形式读出,
根据密钥(用户从键盘输入)将对应字符进行移位操作即可,解密时移动相反。 
例如:加密:设原文为abcdef,密钥为5,则有abcdef每个字母按字母表向后移动5们
(注:z后接a)可得到密文(乱码)fghijkl;
对该文件解密:文件内容为fghijk1,密钥为5,
则有fghijk1每个字母向前移动5位(注a后接z),可得到原文abcdef。
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1024    //字符串最大长度
char readtext[MAX];//读取的未加密字符
char tmp[MAX];//加密后字符
//encrypt-加密  decrypt-解密  genKey-输入数字密匙
int genKey()
{
int a=0;
puts("enter key<int>:");
scanf("%d",&a);
return a;
}
void file()
{
FILE *fp=fopen("test.txt","r");//自行选择文本文件
    if( fp ==NULL)
printf("file not exsit");
int i=0;
char ch;
while( !feof(fp))
{
ch=fgetc(fp);
readtext[i] =ch;
i++;
}
fclose(fp);
}
void decrypt(char *text, int key)
{
puts("to decrypt:");
puts(text);
char tmp[MAX]="";
strcat(tmp,text);
for(int i=0; i< strlen(text)+1; i++)
if( tmp[i]>='a' && tmp[i]<='z' )
{
tmp[i] -= key ;
if( tmp[i] <'a' )
  tmp[i] = 'z'+1 + tmp[i]-'a';
}
puts("decrypted:");
puts(tmp);

}
void emcrypt(char *text, int key)
{
puts("to emcrypt:");
puts(text);
//char tmp[MAX]="";
strcat(tmp,text);
for(int i=0; i<strlen(text)+1; i++)
if( tmp[i]>='a' && tmp[i]<='z' )
{
tmp[i] += key ;
if( tmp[i] >'z' )
  tmp[i] = 'a'-1 + tmp[i]-'z';
}
puts("emcrypted:");
puts(tmp);
}
 int main()
 {
 int a =genKey();
 file();
 emcrypt(readtext,a);
 decrypt(tmp,a);
 return 0;
 }

追问

运行不了,编译出错

追答

//注意此行代码
//FILE *fp=fopen("test.txt","r");//自行选择文本文件
//在.cpp文件相同目录下新建文件test.txt
//------------------test.txt 文件实例-------------------//

The Impact of Electronic Products on Family Reunion。The picture depicts an authentic scene of family get-together. Everyone sits around the dinner table for the family reunion; however, oddly, both children and young adults immerse themselves in electronic products like iPhones or iPads, leaving their old parents looking at each other speechlessly.
//------------------test.txt 文件实例-------------------//

追问

第35行出错,不知道为什么。第25行的是text吗?后面都是用的text,但你写成test

追答

把你的有错误代码发个我

追问

for(int i=0;i<strlen(text)+1; i++)
for(int i=0;i<strlen(text)+1; i++)
分别是36和53行的

追答

先尝试压缩包源码:http://pan.baidu.com/s/1bnppVxP
//两处可更改为:
int size =strlen(text)+1;
for(int i=0; i<size ; i++)

追问

这是什么原因呢?

可以了

谢谢

有没有简单一点的?有的话我再问一次,再采纳你。

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答