當我嘗試提交我的作品時,系統告訴我使用退出代碼。當我使用 return 0 并重新檢查時,系統告訴我使用 return 1... ( https://i.stack.imgur.com/dnVLV.png )
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b"
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: is..."
:( handle lack of argv[1]
expected exit code 1, not 0
:( handles non-numeric key
timed out while waiting for program to exit
:( handles too many arguments
expected exit code 1, not 0
我該如何解決它以及我的代碼有什么問題?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int ok;
char r1;
if (argc == 2)
{
for (int i = 0, s = strlen(argv[1]); i < s; i )
{
if (!isdigit(argv[1][i]))
{
printf("Sorry\n");
return 0;
}
else
{
ok = atoi(argv[1]);
string c = get_string("Enter:");
printf("ciphertext: ");
for (int t = 0, a = strlen(c); t < a; t )
{
if (c[t] < 91 && c[t] > 64)
{
r1 = (c[t] - 64 ok) % 26 64;
printf("%c", r1);
}
else if (c[t] < 123 && c[t] > 96)
{
r1 = (c[t] - 96 ok) % 26 96;
printf("%c", r1);
}
else
{
printf("%c", c[t]);
}
}
return 0;
}
}
}
else
{
printf("Sorry\n");
}
printf("\n");
return 0;
}
我努力做好我的家庭作業和所有的綠色...
uj5u.com熱心網友回復:
您的代碼中有多個問題:
您應該在出錯時回傳非零退出狀態。
如果作為命令列引數給出的數字超過 1 位,則執行多次迭代(每個數字一個)。您應該將編碼回圈移出第一個
for回圈。對大小寫字母使用硬編碼的 ASCII 值會降低代碼的可移植性和難以閱讀性。您應該使用字符常量
'A','Z'等。r1 = (c[t] - 64 ok) % 26 64;是不正確的,可能會產生@而不是Z某些輸入。你應該使用r1 = (c[t] - 65 ok) % 26 65;或更好r1 = (c[t] - 'A' ok) % 26 'A';同樣的錯誤
r1 = (c[t] - 96 ok) % 26 96;
這是修改后的版本:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "missing argument\n");
return 1;
}
char *arg = argv[1];
char *p;
int shift = (int)strtol(arg, &p, 10);
if (!(arg[0] >= '0' && arg[0] <= '9') || p == arg || *p != '\0') {
fprintf(stderr, "invalid shift argument: %s\n", arg);
return 1;
}
char *s = get_string("Enter string: ");
printf("ciphertext: ");
for (int t = 0; s[t] != '\0'; t ) {
unsigned char c = s[t];
/* assuming ASCII: upper and lowercase letters are contiguous */
if (c >= 'A' && c <= 'Z') {
c = (c - 'A' shift) % 26 'A';
} else
if (c >= 'a' && c <= 'z') {
c = (c - 'a' shift) % 26 'a';
}
putchar(c);
}
putchar('\n');
return 0;
}
uj5u.com熱心網友回復:
return <value>,您可以通過在適當的代碼行添加 a 來使用退出代碼。
由于<value>與您的介面定義相匹配,如果您的在線判斷它似乎是 1。
在您的代碼中,您至少在這里沒有這樣做:
else
{
printf("Sorry\n");
}
應該是
else
{
printf("Sorry\n");
return 1;
}
另一種方法是更明確的https://en.cppreference.com/w/c/program/exit,用于程式結束的路徑不那么明顯的情況。
(這主要是 Lundin 的評論提到的。我把它變成了一個明確的答案。)
但是,要完全滿足法官的要求,您需要處理您的輸出。
使用問題中給出的資訊,這些問題的解決方案是不可能的。
uj5u.com熱心網友回復:
您可以使用該exit (3)功能。
#include <stdlib.h>
void exit (int status);
相關的代碼片段可能是
if (argc != 2) {
fprintf(stderr, "missing argument\n");
//return 1;
exit (EXIT_FAILURE);
}
和
putchar('\n');
// return 0;
exit (EXIT_SUCCESS);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/456467.html
上一篇:c語言中指標的說明
