这是一个简单的密码。我不太明白为什么switch语句不能执行。 switch语句之前的所有内容均有效。如果您可以简要解释如何解决此问题,那将是很好的。我最近开始用C语言进行编码,如果这是一个愚蠢的问题,我感到抱歉!预先谢谢你!
#include <stdio.h>
#include <string.h>
int main()
{
char letter;
char word[100];
int i;
int j;
int x;
int shift;
int stringLength;
printf("Enter d to DECRYPT.\n");
printf("Enter e to ENCRYPT.\n");
scanf("%c", & letter);
if(letter == 'd' || letter == 'D') // d = 0, e = 1
{
x = 0;
}
else if(letter == 'e' || letter == 'E')
{
x = 1;
}
else
{
printf("The letter you entered was neither d or e!\n");
return 0;
}
printf("\nEnter the number of shifts:\n");
scanf("%d", &shift);
printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
scanf("%s", word);
//printf("\nThe word you entered was:\n%s\n", word);
stringLength = strlen(word);
//printf("\nThe size of the string is: %i\n", stringLength);
switch(x)
{
case 0: // decrypt
for(i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] - shift;
break;
/*
* while(i < stringLength)
* {
* word[i] = word[i] - shift;
* i++;
*
* printf("The Decrypted word is: %s\n", word);
*/
//break;
case 1: // encrypt
for(i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] + shift;
break;
/*
* while( i < stringLength)
* {
* word[i] = word[i] + shift;
* i++;
*
* printf("The Encrypted word is: %s\n", word);
*/
}
return 0;
}
答案 0 :(得分:1)
您的switch
正在被执行 ,但是每个case
的正文中注释掉的部分太多。
用fgets
代替scanf
作为短语,因为scanf
仅会得到 first 字。
旁注:我将使用#if 0
和#endif
而不是/*
和*/
来注释掉代码块。
请注意,尽管您可以 混合scanf
和fgets
,但我将其重新编码为仅使用fgets
和strtol
。
这是清理后的工作代码[请原谅免费的样式清理]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char word[100];
void
get(void)
{
fgets(word,sizeof(word),stdin);
char *cp = strchr(word,'\n');
if (cp != NULL)
*cp = 0;
}
int
main()
{
char letter;
int i;
int j;
int x;
int shift;
int stringLength;
printf("Enter d to DECRYPT.\n");
printf("Enter e to ENCRYPT.\n");
get();
letter = word[0];
if (letter == 'd' || letter == 'D') // d = 0, e = 1
{
x = 0;
}
else if (letter == 'e' || letter == 'E') {
x = 1;
}
else {
printf("The letter you entered was neither d or e!\n");
return 0;
}
printf("\nEnter the number of shifts:\n");
get();
shift = strtol(word,NULL,10);
printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
#if 0
scanf("%s", word);
#else
get();
#endif
printf("\nThe word you entered was:\n%s\n", word);
stringLength = strlen(word);
printf("\nThe size of the string is: %i\n", stringLength);
printf("x=%d\n", x);
switch (x) {
case 0: // decrypt
printf("decrypt case\n");
for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] - shift;
#if 0
while (i < stringLength) {
word[i] = word[i] - shift;
i++;
}
#endif
printf("The Decrypted word is: %s\n", word);
break;
case 1: // encrypt
printf("encrypt case\n");
for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] + shift;
#if 0
while (i < stringLength) {
word[i] = word[i] + shift;
i++;
}
#endif
printf("The Encrypted word is: %s\n", word);
break;
}
return 0;
}
答案 1 :(得分:0)
尝试在案件执行后立即打印单词。另外,请格式化代码。这是有效的格式化代码:
int main() {
char letter, word[100];
int i,j, x, shift, stringLength;
printf("Enter d to DECRYPT or e to ENCRYPT.\n");
scanf("%c", &letter);
if(letter == 'd' || letter == 'D') {
x = 0;
} else if(letter == 'e' || letter == 'E') {
x = 1;
}
else {
printf("The letter you entered was neither d or e!\n");
return 0;
}
printf("\nEnter the number of shifts:\n");
scanf("%d", &shift);
printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
scanf("%s", word);
stringLength = strlen(word);
switch(x) {
case 0:
for(i = 0; (i < 100 && word[i] != '\0'); i++) {
word[i] = word[i] - shift;
}
printf("The decrypted word is: %s\n", word);
break;
case 1: // encrypt
for(i = 0; (i < 100 && word[i] != '\0'); i++) {
word[i] = word[i] + shift;
}
printf("The encrypted word is: %s\n", word);
break;
default:
printf("Invalid case where the value of x is %d \n",x);
}
return 0;
}