程式規格是
設計和實作一個程式,替代,使用替代密碼加密訊息。
在一個名為substitution 的目錄中的一個名為substitution.c 的檔案中實作您的程式。
您的程式必須接受一個命令列引數,即用于替換的鍵。鍵本身應該不區分大小寫,因此鍵中的任何字符是大寫還是小寫都不應影響程式的行為。
如果您的程式在沒有任何命令列引數或多個命令列引數的情況下執行,您的程式應該列印您選擇的錯誤訊息(使用 printf)并從 main 回傳值 1(這往往表示錯誤) 立即地。
如果密鑰無效(如不包含 26 個字符、包含任何非字母字符或每個字母不只包含一次),您的程式應該列印您選擇的錯誤訊息(使用 printf)并從 main 回傳立即值為 1。
您的程式必須輸出明文:(不帶換行符),然后提示用戶輸入明文字串(使用 get_string)。
您的程式必須輸出密文:(不帶換行符)后跟明文對應的密文,明文中的每個字母字符替換密文中的對應字符;非字母字符應原樣輸出。
您的程式必須保留大小寫:大寫字母必須保持大寫字母;小寫字母必須保持小寫字母。
輸出密文后,應列印換行符。然后你的程式應該通過從 main 回傳 0 來退出。
除了重復輸入之外,我能夠完成所有這些
我想這部分代碼有問題,但我不明白為什么。
當我輸入終端時,我不斷得到一個重復的值 != ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
我不明白為什么,因為代碼本身似乎沒問題。它檢查陣列內部的值是否重復,然后增加值計數器,但它不起作用。
當我在 vscode 除錯中進行測驗運行時,我得到了重復的最終值等于 25,這超出了我的理解。 在此處輸入影像描述
我下面的代碼是
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Check is to keep track if it fulfills all requirements
int check = 0;
// Check if the agrc values are equal to 2 or not, if not have to ask for it again
if (argc != 2)
{
printf("./substitution key\n");
return 1;
}
else
{
check ;
}
// Get the length of the string
int n = strlen(argv[1]);
// Check if the length of inputted characters is 26 or not
if (n != 26)
{
printf("Key must contain 26 characters\n");
return 1;
}
else
{
check ;
}
// Check if there are 26 letters
int letterCheck = 0;
// To get number of digits
// Loop through out the second input through terminal to see if the values are correct
for (int i = 0; i < n; i )
{
if (isalpha(argv[1][i]))
{
letterCheck ;
}
}
if (letterCheck != 26)
{
printf("Key must contain 26 letters\n");
return 1;
}
else
{
check ;
}
// Check for duplicates
int duplicate = 0;
for (int a = 0; a < n; a )
{
for (int b = 1; b < n; b )
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate ;
}
}
}
if (duplicate != 0)
{
printf("Key must not contain repeated characters\n");
return 1;
}
else
{
check ;
}
if (check == 4)
{
string plaintext = get_string("plaintext: ");
// Get the length of the string
int length = strlen(plaintext);
for (int i = 0; i < length; i )
{
if (isupper(plaintext[i]))
{
plaintext[i] = plaintext[i] - 65;
plaintext[i] = argv[1][(int)plaintext[i]];
plaintext[i] = toupper(plaintext[i]);
}
else
{
plaintext[i] = plaintext[i] - 97;
plaintext[i] = argv[1][(int)plaintext[i]];
plaintext[i] = tolower(plaintext[i]);
}
}
printf("ciphdertext: %s\n", plaintext);
return 0;
}
}```
'''
int duplicate = 0;
for (int a = 0; a < n; a )
{
for (int b = 1; b < n; b )
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate ;
}
}
}
if (duplicate != 0)
{
printf("Key must not contain repeated characters\n");
return 1;
}
else
{
check ;
}
'''
uj5u.com熱心網友回復:
您已將“b”值設定為“1”,因此每次回圈時都從 1 開始。因此,當您的外部回圈為 1 時,您的內部回圈也將從 1 開始,并且您的重復標志將增加。您的回圈應該類似于下面的回圈
for (int a = 0; a < n; a )
{
for (int b = a 1; b < n; b )
{
if (toupper(argv[1][a]) == toupper(argv[1][b]))
{
duplicate ;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/413820.html
標籤:
上一篇:如何合并陣列的第2個第i個元素
