2014-10-19 46 views
-3

你好堆栈溢出的人。使用c代码制作凯撒密码?

说明

我想以下几点。

我的程序需要输入一串字母作为输入,然后输出向前旋转13步。

例如

甲变为N

B成为ö

Ç变为P

d变为Q

对于这个程序我需要使用ascii表。因此,例如,下壳体= 97一旦我的节目完成它成为N = 110

我写了一个小的公式此

C =(C + 13-97)%26 + 97其中,c是我的信。正如你可以看到,如果C = 97,那么C将最终被110

因此,这里是我的程序

正如看见我使用了,如果statment来确定,如果我有一个大写或小写字母。

/* 97 is lower case a in ascii and 122 is lower case z*/ 

    # include <stdio.h> 

    int main() { 

    char c; 

    printf("please enter a character:"); 


    while (c!=-1) { 

    c=getchar(); 
    putchar (c); 

    if (c>=97 && c<=122) { 

    c=(c+13-97)% 26+97 ; 
    printf("%c \n " ,c); 

     } 
    else 

     c=(c+13-65) % 26 +65 ; 
     printf("%c \n " ,c); 


    } 
    return 0; 
     } 

我的问题是与输出,例如,如果我插上

而不是n我得到

an 
    n 

    1 
+2

使用'{}'为其他块。并替换'else if('A'<= c && c <='Z')'也可以'char c;' - >'int c;' – BLUEPIXY 2014-10-19 18:23:42

回答

0

您将遇到的另一个问题是需要在读取下一个字符之前刷新输入缓冲区中剩余的\ n。基本上,输入字符后用户按下Enter,c是从输入缓冲器中读取的,但'\n'仍然存在。因此,下一次输入循环时,\n已经存在于输入缓冲区中,并在该迭代中被视为c

为了防止这种情况发生,到flush输入缓冲器中的最简单的方法是定义一个扔掉的intint flush和之后的字符的每个读取,增加以下内容:

do { flush=getchar(); } while (flush != '\n'); 

这将解全部来自input buffer的其余字符准备下一次阅读。 (注:fflush不这样做对于输入流)与此结合的实现是:

#include <stdio.h> 

int main() { 

    int c = 0;  /* Always initialize variables */ 
    int flush = 0; 
    int new = 0; 

    printf ("\nPlease enter a character (ctrl+d to exit):\n\n"); 

    while (printf (" char: ") && (c = getchar()) != -1) { 

     do { flush=getchar(); } while (flush != '\n');  /* flush input buffer */ 

     if (c >= 'a' && c <= 'z') 
     { 
      new = (c + 13 - 97) % 26 + 97; 
      printf ("\t lower-case '%c' becomes: '%c'\n\n", c, new); 

     } 
     else if (c >= 'A' && c <= 'Z') 
     { 
      new = (c + 13 - 65) % 26 + 65; 
      printf ("\t upper-case '%c' becomes: '%c'\n\n", c, new); 
     } 
     else 
     { 
      printf ("\n invalid character, try again\n\n"); 
     } 
    } 

    printf ("\n\nexiting.\n\n"); 

    return 0; 
} 

输出:

Please enter a character (ctrl+d to exit): 

char: a 
      lower-case 'a' becomes: 'n' 

char: b 
      lower-case 'b' becomes: 'o' 

char: n 
      lower-case 'n' becomes: 'a' 

char: o 
      lower-case 'o' becomes: 'b' 

char: A 
      upper-case 'A' becomes: 'N' 

char: B 
      upper-case 'B' becomes: 'O' 

char: N 
      upper-case 'N' becomes: 'A' 

char: O 
      upper-case 'O' becomes: 'B' 

char: 

exiting. 

祝你好运与您的项目。如果遇到其他问题,请留言。


多个字符版本使用函数getline

关于你提到的有关线路输入问题。下面是一个使用getlinestdin读取多个字符的版本:

#include <stdio.h> 

int main() { 

    int new = 0; 
    ssize_t nread = 0; /* number of chars read by getline      */ 
    char *line = NULL; /* string holding chars - getline allocates when NULL */ 
    size_t n = 0;  /* limit number of bytes to (ignored when 0)   */ 
    char *p = NULL;  /* point to use to iterate over each char in line  */ 
    int index = 0;  /* simple index for formatted output.     */ 


    printf ("\nPlease enter characters to translate (ctrl+d to exit):\n"); 

    while (printf ("\n input: ") && (nread = getline (&line, &n, stdin) != -1)) { 

     index = 0;      /* reset index     */ 
     p = line;      /* assign pointer to line  */ 
     printf ("\n");     /* just because it looks nice */ 

     while (*p != '\n')    /* getline consumes the '\n' */ 
     { 

      if (*p >= 'a' && *p <= 'z') 
      { 
       new = (*p + 13 - 97) % 26 + 97; 
       printf ("\t char[%2d] : %c => %c\n", index, *p, new); 

      } 
      else if (*p >= 'A' && *p <= 'Z') 
      { 
       new = (*p + 13 - 65) % 26 + 65; 
       printf ("\t char[%2d] : %c => %c\n", index, *p, new); 
      } 
      else 
      { 
       printf ("\n char[%2d] : %c => invalid character\n", index, *p); 
      } 

      p++; 
      index++; 
     } 
    } 

    printf ("\n\nexiting.\n\n"); 

    return 0; 
} 

输出:

Please enter characters to translate (ctrl+d to exit): 

    input: aAbBcCmMnNoO 

      char[ 0] : a => n 
      char[ 1] : A => N 
      char[ 2] : b => o 
      char[ 3] : B => O 
      char[ 4] : c => p 
      char[ 5] : C => P 
      char[ 6] : m => z 
      char[ 7] : M => Z 
      char[ 8] : n => a 
      char[ 9] : N => A 
      char[10] : o => b 
      char[11] : O => B 

    input: 

exiting. 
+0

我看到是的,我注意到,新角色不断搞乱输入感谢指出一个解决方案。 – humblecomputeruser 2014-10-19 19:27:42

+0

经验告诉我 - 这是下一个问题:) – 2014-10-19 19:40:53

+0

其实我还有一个问题,如果你不介意,它是如果我想输入多个字母说一个“abc”并将其变成“nop “我必须能够使用get char而不是使用字符串来做到这一点。 – humblecomputeruser 2014-10-19 20:04:50

0

while回路应

while ((c=getchar()) != EOF) 

您的代码在c是我之前检查c的值nitialized。你应该得到这个警告。

您不应该在代码中使用硬编码的ASCII值。小写字母a是'a'。小写字母z是'z'。因此,举例来说,你可以写

if (c >= 'a' && c <= 'z') 

注意stdin缓冲区字符,直到你按下键return。当您按下return键时,getchar会为您输入您输入的字符,然后是换行符'\n'字符。您的代码只能处理小写和大写字符。您需要修改代码以正确处理非字母字符,例如空格,标点符号,换行符。

+0

可能值得一提的是'EOF'被定义为' -1',以确保在将测试子句从'-1'切换到'EOF'时不会产生混淆。正如你指出的那样,问题是检查未初始化的'c',否则测试条件是OK的。 – 2014-10-19 18:25:34

+0

好的,我看到了回复的感谢。我认为它现在应该工作。 – humblecomputeruser 2014-10-19 18:29:36

2

你的代码是在它的一些小的改动精绝。

 /* 97 is lower case a in ascii and 122 is lower case z*/ 

    # include <stdio.h> 

    int main() 
    { 

    char c; 

    printf("please enter a character:"); 


    while (c!=-1) 
    { 

     c=getchar(); 
     //putchar (c);  // Avoid Printing entered character 

     if (c>=97 && c<=122) 
     { 
      c=((c+13-97)% 26)+97 ; 
      printf("%c \n " ,c); 
     } 
    else 
    {        // Braces missing 
      c=((c+13-65) % 26) +65 ; 
      printf("%c \n " ,c); 
    } 
    return 0; 
    } 
    } 

输出:

please enter a character : a 
n