Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
C编程-任何缩短程序的方法_C - Fatal编程技术网

C编程-任何缩短程序的方法

C编程-任何缩短程序的方法,c,C,我的程序必须尝试所有可能的转换来解密密文 这是在函数中完成的。如果您有任何想法将此缩短,我们将不胜感激 char decryptCiphertext(char data[], double data2[]) { int i, convert, shiftingLetter; char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42], decrypted5[42],

我的程序必须尝试所有可能的转换来解密密文 这是在函数中完成的。如果您有任何想法将此缩短,我们将不胜感激

char decryptCiphertext(char data[], double data2[])
{
   int i, convert, shiftingLetter;
   char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42], 
   decrypted5[42], decrypted6[42], decrypted7[42], decrypted8[42], decrypted9[42], decrypted10[42], 
   decrypted11[42], decrypted12[42], decrypted13[42], decrypted14[42], decrypted15[42], decrypted16[42], 
   decrypted17[42], decrypted18[42], decrypted19[42], decrypted20[42], decrypted21[42], decrypted22[42], 
   decrypted23[42], decrypted24[42], decrypted25[42];

   //Try all possible shifts from 1-25 and store each in an array
   for (i = 0; data[i] != '\0'; i++) 
   {
      convert = data[i] - 'A';  //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
      convert = (convert + 1);    //add shift
      shiftingLetter = convert % 26;    //cycle around
      decrypted1[i] = 'A' + shiftingLetter;  //store new letter in array
   }
   decrypted1[i] = '\0';
   likelyPlaintext(decrypted1, data2);
   //printf(" 1. %s\n", decrypted1);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 2);
      shiftingLetter = convert % 26;
      decrypted2[i] = 'A' + shiftingLetter;
   }
   decrypted2[i] = '\0';
   //printf(" 2. %s\n", decrypted2);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 3);
      shiftingLetter = convert % 26;
      decrypted3[i] = 'A' + shiftingLetter;
   }
   decrypted3[i] = '\0';
   //printf(" 3. %s\n", decrypted3);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 4);
      shiftingLetter = convert % 26;
      decrypted4[i] = 'A' + shiftingLetter;
   }
   decrypted4[i] = '\0';
   //printf(" 4. %s\n", decrypted4);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 5);
      shiftingLetter = convert % 26;
      decrypted5[i] = 'A' + shiftingLetter;
   }
   decrypted5[i] = '\0';
   //printf(" 5. %s\n", decrypted5);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 6);
      shiftingLetter = convert % 26;
      decrypted6[i] = 'A' + shiftingLetter;
   }
   decrypted6[i] = '\0';
   //printf(" 6. %s\n", decrypted6);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 7);
      shiftingLetter = convert % 26;
      decrypted7[i] = 'A' + shiftingLetter;
   }
   decrypted7[i] = '\0';
   //printf(" 7. %s\n", decrypted7);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 8);
      shiftingLetter = convert % 26;
      decrypted8[i] = 'A' + shiftingLetter;
   }
   decrypted8[i] = '\0';
   //printf(" 8. %s\n", decrypted8);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 9);
      shiftingLetter = convert % 26;
      decrypted9[i] = 'A' + shiftingLetter;
   }
   decrypted9[i] = '\0';
   //printf(" 9. %s\n", decrypted9);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 10);
      shiftingLetter = convert % 26;
      decrypted10[i] = 'A' + shiftingLetter;
   }
   decrypted10[i] = '\0';
   //printf(" 10. %s\n", decrypted10);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 11);
      shiftingLetter = convert % 26;
      decrypted11[i] = 'A' + shiftingLetter;
   }
   decrypted11[i] = '\0';
   //printf(" 11. %s\n", decrypted11);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 12);
      shiftingLetter = convert % 26;
      decrypted12[i] = 'A' + shiftingLetter;
   }
   decrypted12[i] = '\0';
   //printf(" 12. %s\n", decrypted12);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 13);
      shiftingLetter = convert % 26;
      decrypted13[i] = 'A' + shiftingLetter;
   }
   decrypted13[i] = '\0';
   //printf(" 13. %s\n", decrypted13);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 14);
      shiftingLetter = convert % 26;
      decrypted14[i] = 'A' + shiftingLetter;
   }
   decrypted14[i] = '\0';
   //printf(" 14. %s\n", decrypted14);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 15);
      shiftingLetter = convert % 26;
      decrypted15[i] = 'A' + shiftingLetter;
   }
   decrypted15[i] = '\0';
   //printf(" 15. %s\n", decrypted15);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 16);
      shiftingLetter = convert % 26;
      decrypted16[i] = 'A' + shiftingLetter;
   }
   decrypted16[i] = '\0';
   //printf(" 16. %s\n", decrypted16);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 17);
      shiftingLetter = convert % 26;
      decrypted17[i] = 'A' + shiftingLetter;
   }
   decrypted17[i] = '\0';
   //printf(" 17. %s\n", decrypted17);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 18);
      shiftingLetter = convert % 26;
      decrypted18[i] = 'A' + shiftingLetter;
   }
   decrypted18[i] = '\0';
   //printf(" 18. %s\n", decrypted18);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 19);
      shiftingLetter = convert % 26;
      decrypted19[i] = 'A' + shiftingLetter;
   }
   decrypted19[i] = '\0';
   //printf(" 19. %s\n", decrypted19);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 20);
      shiftingLetter = convert % 26;
      decrypted20[i] = 'A' + shiftingLetter;
   }
   decrypted20[i] = '\0';
   //printf(" 20. %s\n", decrypted20);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 21);
      shiftingLetter = convert % 26;
      decrypted21[i] = 'A' + shiftingLetter;
   }
   decrypted21[i] = '\0';
   //printf(" 21. %s\n", decrypted21);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 22);
      shiftingLetter = convert % 26;
      decrypted22[i] = 'A' + shiftingLetter;
   }
   decrypted22[i] = '\0';
   //printf(" 22. %s\n", decrypted22);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 23);
      shiftingLetter = convert % 26;
      decrypted23[i] = 'A' + shiftingLetter;
   }
   decrypted23[i] = '\0';
   //printf(" 23. %s\n", decrypted23);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 24);
      shiftingLetter = convert % 26;
      decrypted24[i] = 'A' + shiftingLetter;
   }
   decrypted24[i] = '\0';
   //printf(" 24. %s\n", decrypted24);

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 25);
      shiftingLetter = convert % 26;
      decrypted25[i] = 'A' + shiftingLetter;
   }
   decrypted25[i] = '\0';
   //printf(" 25. %s\n", decrypted25);

} 
我的程序必须解密密文,尝试所有可能的移位
这是在函数中完成的。如果您有任何想法将其缩短,我们将不胜感激。

首先,每个For循环都可以是您传递变量的函数

记住干。不要重复你自己

我不知道C,但下面的内容应该在一个函数中,您传入decrypted4和26并返回结果

   for (i = 0; data[i] != '\0'; i++)
   {
      convert = data[i] - 'A';
      convert = (convert + 4);
      shiftingLetter = convert % 26;
      decrypted4[i] = 'A' + shiftingLetter;
   }
这是:

   int i, j, convert, shiftingLetter;
   char decryptedArr[25][42];

   //Try all possible shifts from 1-25 and store each in an array
   for (j = 0; j < 25; j++)
   for (i = 0; data[i] != '\0'; i++) 
   {
      convert = data[i] - 'A';  //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc
      convert = (convert + i + 1);    //add shift
      shiftingLetter = convert % 26;    //cycle around
      decryptedArr[j][i] = 'A' + shiftingLetter;  //store new letter in array
   }
   decryptedArr[j][i] = '\0';
inti,j,convert,shiftingLetter;
char decryptedArr[25][42];
//尝试从1到25的所有可能的移位,并将每个移位存储在一个数组中
对于(j=0;j<25;j++)
对于(i=0;数据[i]!='\0';i++)
{
convert=data[i]-“A”;//将字母转换为等于0-26的形式,例如:A=0、B=1、C=2等
convert=(convert+i+1);//添加移位
shiftingLetter=convert%26;//循环
decryptedArr[j][i]=“A”+移位字母;//在数组中存储新字母
}
decryptedArr[j][i]='\0';

这属于codereview。首先将所有这些向量打包成一个双数组(例如,解密的[][]),或者最好打包成一个元素正确对齐的数组。将所有这些循环包装成一个循环(很容易做到)。我需要将所有移位字符存储在一个数组中。所以如果它被移位1,我需要它在一个数组中,如果它被移位2,它也需要在另一个数组中,这样我可以计算出最高的频率来找到最可能的明文。对不起,我对c语言编程有点陌生。我从未学过2D数组。我尝试了你的代码,但出现了错误。请再次检查,我忘了修改某些内容,现在应该可以了。我收到一条错误消息,在为此行的type int分配给type char[42]时,显示不兼容的类型decryptedArr[j]=“A”+shiftingLetter;我已经修好了,请现在检查一下