#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 프로그램이 예기치 않게 종료됩니다.
체계적으로 코드를 들여 쓰고 적어도 코드에 삽입되어있는 경우에는 들여 쓰기 문자로 탭을 피하십시오. –