Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
维格纳';s密码CS50 if循环_C_Cs50 - Fatal编程技术网

维格纳';s密码CS50 if循环

维格纳';s密码CS50 if循环,c,cs50,C,Cs50,作为pset2 CS50的一部分,用于vigerne的密码。我的问题是,我应该如何安排“如果”循环?我觉得我做得不对 请指出我代码中的错误/不好的地方。(我的压痕看起来不好) 到目前为止,我得到的加密与他们在cs50上提供的加密相匹配。但我希望改善这一点。提前谢谢 #include <stdio.h> #include <cs50.h> #include <stdlib.h> #include <string.h> #include <cty

作为pset2 CS50的一部分,用于vigerne的密码。我的问题是,我应该如何安排“如果”循环?我觉得我做得不对

请指出我代码中的错误/不好的地方。(我的压痕看起来不好)

到目前为止,我得到的加密与他们在cs50上提供的加密相匹配。但我希望改善这一点。提前谢谢

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

int main(int argc ,string argv[])
{ 
    // Make sure 1 argument is given
    if(argc !=2)
    {
    printf("Please enter only 1 alphabetical word as key\n");
    return 1; //returns an error if no 2nd arg is provided.
    }

    // Converting 2nd argument into an integer

    string k=argv[1];

    for(int i=0,n=strlen(k);i<n;i++)
    {   
        if(!isalpha(k[i]))
        {
        char c= k[i];
        printf("%c is not an alphabet. Please enter only alphabets.\n",c);
        return 2; //returns an error if key has non-alphabets
        }
    }

    // Prompt for plaintext to be encrypted    
    printf("Please enter the plain text you wish to encrypt\n");
    string p=GetString();
    printf("This is your text: %s\n",p);



    // Converting upper/lower case alphabets to 0-25 indexed
   for(int i=0,j=0,n=strlen(p),m=strlen(k);i < n; i++,j++)
   {
        if(p[i]==' ')
        {
        j--;
        }
                    if(isupper(k[j]))
                    {  
                    k[j] = (int)(k[j]-'A'); 
                    }

                    if(islower(k[j]))
                    {  
                    k[j] = (int)(k[j]-'a');   
                    }


                  //  printf("%d\n",k[i]); // value of k[i] is still stored as int here for ea char
                         if(isalpha(p[i]))
                         {  
                            if(isupper(p[i]))
                            {
                            int newpindex =(p[i]-'A');
                            int x = (newpindex + k[(j+m)%(m)])%26;
                            int cipher = x + 'A';
                            printf("%c",cipher);
                            }

                            else if(islower(p[i]))
                            {
                            int newpindex =(p[i]-'a');
                            int x = (newpindex + k[(j+m)%(m)])%26;
                            int cipher = x + 'a';
                            printf("%c",cipher);
                            }
                         } 

                         else 
                         printf("%c",p[i]);
  }

              printf(" \n");           
} 
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符串argv[])
{ 
//确保给出1个参数
如果(argc!=2)
{
printf(“请仅输入1个字母单词作为关键字”);
return 1;//如果没有提供第二个参数,则返回一个错误。
}
//将第二个参数转换为整数
字符串k=argv[1];

对于(int i=0,n=strlen(k);i代码中的一个可能错误是识别既不是字母也不是空格的字符

看看你的代码,循环的每一次迭代中,j都会递增,当遇到空格时,它会递减。但是,如果非字母字符是感叹号或其他符号呢

考虑是否有更好的方法在循环中处理j,以确保在这种情况下不会丢失在键中的位置。这也可以消除检查空格(或通常的符号)的需要,从而使代码更加简洁

编辑:刚刚看到了一些其他的东西,isupper和islower包含isalpha(isalpha详细信息的手册页面,它相当于isupper | | |更低)。因此,您可以选择删除isalpha条件,只检查isupper/islower

    if(p[i]==' ')
    {
    j--;
    } 
考虑到字符串k中的第一个字符是一个空格,这个if语句将不起作用,因为这将使j=-1,所以在下一个if语句中使用它作为p[-1]将不起作用。 一个更好的选择是删除这个if语句,这样在p[i]是空格的情况下,它将按原样显示,不做任何修改

为了使代码看起来更干净,可以从只有一条指令的if语句中删除花括号

例如:

      if(isupper(k[j]))  
            k[j] = (int)(k[j]-'A'); 
什么是“完全正确”?