凯撒密码加密,我有这样的代码。该程序使用用户的书面文字。但我想这是从一个文本文件,并运行读取。
#include<stdio.h>
#include <conio.h>
#include<string.h>
void main()
{
int key,i;
char [30];
clrscr();
printf("\n enter plain text : ");
gets(data);
printf("\ enter key value : ");
scanf("%d",&key);
{
for (i=o;i<strlen(data);i++) {
if (data[i]==' ') {}
else
{
if (data[i]>='x')
{
data[i]=data[i]-26;
}
data[i]=data[i]+key;
}
}
}
printf("your cipher text is : %s",data);
getch();
}
你只需复制并粘贴某处此代码?
正如其他人已经指出的那样,有一些相当大的问题,你的代码,所以我会尝试和触摸他们并没有在整个提到的那些。要开始虽然,你应该声明main
作为int
。返回值告诉你,如果程序正确退出,它不只是约定。
这里是你的原代码,格式化。我删除conio.h
因为它被废弃多年,而且你不要在这里需要它。所有它所做的是清除屏幕给你,你可以用System("CLS");
做,虽然here是一个已经共享多次在这里,这样解释了为什么你应该避免使用它一个伟大的文章。
#include<stdio.h>
#include<string.h>
int main()
{
int key,i;
// original: char [30];
// I'm guess you copied and pasted wrong?
char plainText[30];
printf("\n enter plain text : ");
gets(data);
printf("\nenter key value : ");
scanf("%d",&key);
// You had brackets here, essentially creating an unnecessary
// block of code.
for(i=o;i<strlen(data);i++)
{
// Where did the "data" variable come from? You haven't declared it
// and you're trying to get its length in this for loop
if (data[i]==' ')
{
// ?
}
else
{
if (data[i]>='x')
{
// Instead of this approach I would go with a modulus operation
// If you're not familiar with modular arithmetic I really recommend you look
// it up. It's absolutely essential in cryptography. It's central to both
// symmetric and asymmetric key cryptography.
data[i]=data[i]-26;
}
// This is the heart of the Caesar cipher right here. This is where you're actually
// shifting the characters, so it's a literal Caesar cipher
data[i]=data[i]+key;
}
}
// Again, you haven't declared "data" so you can't call it. I'm guessing you didn't
// try to compile this program because it is teeming with errors GCC, Clang, and VC++
// would have caught immediately
printf("your cipher text is : %s",data);
getch();
// Remember to make your "main" function return an <code>int</code>. This value is
// very important, especially when your program gets called by another program because
// it's how your program communicates that it ran successfully, or something went
// wrong. In that case you could set a specific error code to know exactly
// what went wrong and were
// Example:
int x = 1;
if (x == 1)
exit(4);
// This program would now exit with a status of 4, which you would then know was due
// to this function. This was a contrived example, but hopefully you get the gist
return 0;
}