为什么我只能在凯撒大酒店获得40%的收益?

时间:2019-08-02 10:46:11

标签: cs50 caesar-cipher

我已经在线注册了CS50,并且在Caesar以及PSet2中的Vignere上进行了自己的工作。我在Vignere上获得了99%的分数,但在Caesar上获得了40%的分数,这真是奇怪,因为我是在Vigenere的Caesar工作的。它!

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

//user to input number key unprompted

int main(int argc, string argv[])
{
    //iterate to make sure only one argument is inputted

    if (argc != 2)
    {
        printf("Usage: ./caesar key \n");
    }
    else 
    {   //iterate to make sure the string only has numeric digits
        for (int j = 0; j <= (strlen(argv[1])); j++)
        {
            if (((argv[1][j] >= 'a') || (argv[1][j] >= 'A')) && ((argv[1][j] <= 'z') || (argv[1][j] <= 'Z')))
            {
                printf("Usage: ./caesar key \n");
                return 1;
            }
        }
        //converting digit string to an integer called key
        int key = atoi(argv[1]);
        if (key < 0) //making sure the key is positive
        {
            printf("Usage: ./caesar key \n");
            return 1;
        }

        //after validity has been checked
        else
        {
            //prompt user to input plaintext
            string plain = get_string("plaintext: ");
            int len_plain = strlen(plain);

            //convert plaintext to ciphertext using inputted key
            string cipher = plain;
            for (int x = 0; x < len_plain; x++)
            {
                if (plain[x] >= 'a' && plain[x] <= 'z')
                {
                    cipher[x] = ((plain[x] + key)%122);
                    if (cipher[x]<97)
                    {
                        cipher[x] = cipher[x] + 96;
                    }
                }
                else if (plain[x] >= 'A' && plain[x] <= 'Z')
                {
                    cipher[x] = ((plain[x] + key)%90);
                    if (cipher[x] < 65)
                    {
                        cipher[x] = cipher[x] + 64;
                    }
                }
                else 
                {
                    cipher[x] = plain[x];
                }   
            }
            printf("%s\n", cipher);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从规范中(添加了重点):

  

...验证密钥后,我们提示用户输入字符串(使用   “ plaintext:”作为提示),然后将其所有字符移动   1,打印出“密文:” ,然后是结果和换行符。

程序不会在结果中打印“密文:”。