2014-02-25 49 views
0

我一直在盯着这个问题几个星期,我什么都没有!它不起作用,我非常了解,但我不知道为什么或错在哪里。我知道开发者吐出关于我突出显示的行的“错误:预期表达”,但实际上这只是冰山的一角。如果任何人都知道如何解决这个小问题,我将不胜感激!!!!!凯撒密码在C

好了,所以我改变了我< n和> =喜欢你惊人的助手建议,它会贯穿创造,但还是有导致这些难看的错误毛刺:

:(encrypts "a" as "b" using 1 as key 
    \ expected output, not an exit code of 0 
:(encrypts "barfoo" as "yxocll" using 23 as key 
    \ expected output, not an exit code of 0 
:(encrypts "BARFOO" as "EDUIRR" using 3 as key 
    \ expected output, but not "E\nA\nU\nI\nR\nR\n" 
:(encrypts "BaRFoo" as "FeVJss" using 4 as key 

有什么建议?

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <cs50.h> 
#include <ctype.h> 

int main(int argc, char *argv[]) 
{ 
    //Get the key 
    if (argc != 2 || atoi(argv[1]) < 0) 
     { 
     printf("Usage: ./caesar k"); 
     return 1; 
     } 

    int key = atoi(argv[1]); 
    string plaintext = GetString(); 

    for (int i = 0, n = strlen(plaintext); i < n; i++) 
    { 
     if (plaintext[i] > 'A' && plaintext[i] <= 'Z') 
      { 
      plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A'; 
      } 
    } 


    for (int i = 0, n = strlen(plaintext); i < n; i++) 
    {   
     if (plaintext[i] >= 'A' && plaintext[i] >= 'Z') 
     { 
     plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A'; 
     } 
     else if (plaintext[i] >= 'a' && plaintext[i] < 'z') 
     { 
     plaintext[i] = (plaintext[i] - 'a' + key) % 26 + 'a'; 
     } 
     else 
     { 
      printf("%c\n", plaintext[i]); 
     } 
    } 

    return 0; 
} 

回答

0

=>不是有效的操作符。使用> =

也,我< n在你的for循环,而不是n <我。

而你的第二个for循环并不是真的需要。只是放(明文);

+0

啊非常感谢你!我非常感谢帮助! – user3349389

+0

接受答案? :) – Hafthor

0

更改您的以下行

if (plaintext[i] => 'A' && plaintext[i] >= 'Z') 

if (plaintext[i] >= 'A' && plaintext[i] >= 'Z')

你应该使用> =,而不是=>

+0

非常感谢您的帮助!我根本不明白这个问题! – user3349389