#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void encrypting(char cipher[25], int shift, int num)
{ int i;
for ( i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] + shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] + shift - 'a' + 26) % 26) + 'a');
}
}
}
void decrypting(char cipher[25], int shift, int num)
{ inti;
for ( i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] - shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] - shift - 'a' + 26) % 26) + 'a');
}
}
}
int main ()
{
char text[10];
static const char encrypt[] = "2";
static const char decrypt[] = "1";
int shift;
char cipher[25];
int result1;
int result2;
int num;
int i;
printf( "Enter operation: encrypt or decrypt/n" );
printf( "Press 1 to Encrypt or 2 to Decrypt" );
scanf("%c",&text) ;
printf( "Enter shift key" );
scanf("%d",&shift) ;
printf("Enter text to encrypt/decrypt") ;
fflush(stdin);
scanf( "%c",&cipher) ;
num = strlen (cipher);
result1 = strcmp (text, encrypt);
result2 = strcmp (text, decrypt);
if(result1 == 0)
{
decrypting(cipher, shift, num);
}
else{exit(0);}
if(result2 == 0)
{
encrypting(cipher, shift, num);
}
else{exit(0);}
printf("Result");
printf("%d",cipher);
}
这是我的代码,用于根据用户选择对字符串进行加密或解密的简单程序。
当我尝试编译时,代码产生了这个错误,任何人都可以解释这个问题。
C:\ Users \ Adam \ Documents \ Ceaser Cipher Dr X \ First try.c || in function&#39; main&#39;:|
C:\ Users \ Adam \ Documents \ Ceaser Cipher Dr X \ First try.c | 65 | error:函数参数太少&#39;解密&#39; |
C:\ Users \ Adam \ Documents \ Ceaser Cipher Dr X \ First try.c | 21 | note:声明在这里|
C:\ Users \ Adam \ Documents \ Ceaser Cipher Dr X \ First try.c | 71 | error:功能参数太少&#39;加密&#39; |
C:\ Users \ Adam \ Documents \ Ceaser Cipher Dr X \ First try.c | 6 | note:声明在这里|
|| ===构建失败:2个错误,0个警告(0分钟,0秒(秒))=== |
编辑:我已更正了我的代码,但现在用户输入了密码字符串后,程序将自动终止。
答案 0 :(得分:1)
删除函数头中的int i
,并声明为局部变量:
void encrypting(char cipher[25], int shift, int num)
{
int i;
for ( i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] + shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] + shift - 'a' + 26) % 26) + 'a');
}
}
}
void decrypting(char cipher[25], int shift, int num)
{
int i;
for ( i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] - shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] - shift - 'a' + 26) % 26) + 'a');
}
}
}
答案 1 :(得分:0)
您的加密和解密功能在传入3个参数时需要4个参数。
答案 2 :(得分:0)
你的两个函数(加密和解密)得到四个参数,但你在代码中传递三个参数
答案 3 :(得分:0)
你在函数中缺少一个参数。错误描述是不言自明的。
使用3个args而不是所需的4个
答案 4 :(得分:0)
由于解密函数有4个参数,所以你必须传递四个参数,你只是在解密函数中传递3个参数。