C,如何正确使用退出代码?

发布于 2025-01-18 21:36:56 字数 2510 浏览 2 评论 0原文

当我尝试提交工作时,系统告诉我使用退出代码。当我使用返回0和重新检查时,系统告诉我使用返回1 ... https://i.sstatic.net/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;
}

我尽力做好我的作业和所有绿色...

When I try to submit my work, the system told me to use the exit code. When I use return 0 and recheck, the system told me to use return 1...
(https://i.sstatic.net/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

How can I fix it and what's wrong with my code?

#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;
}

I try to do well with my homework and all green...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2025-01-25 21:36:56

代码中有多个问题:

  • 您应该在错误时返回非零退出状态。

  • 如果给出的数字作为命令行参数的参数超过1位,则执行多个迭代(每个数字一个)。您应该将编码循环移出循环的第一个


  • 使用硬编码的ASCII值对上和下部的案例字母,使代码的便携程度较低且难以读取。您应该使用字符常数'a''z',等等。

  • 您应该使用字符常数'a ' 64;是不正确的,并且可能会为某些输入产生@而不是z。您应该使用r1 =(c [t] -65 + ok)%26 + 65;或更好的r1 =(c [t] - 'a'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;
}

There are multiple issues in your code:

  • you should return a non zero exit status upon error.

  • if the number given as a command line argument has more than 1 digit, you perform multiple iterations (one for each digit). You should move the encoding loop out of the first for loop.

  • using hard coded ASCII values for upper and lower case letters makes the code less portable and hard to read. You should use character constants 'A', 'Z', etc.

  • r1 = (c[t] - 64 + ok) % 26 + 64; is incorrect and may produce @ instead of Z for some inputs. You should use r1 = (c[t] - 65 + ok) % 26 + 65; or better r1 = (c[t] - 'A' + ok) % 26 + 'A';

  • same mistake for r1 = (c[t] - 96 + ok) % 26 + 96;

Here is a modified version:

#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;
}
宫墨修音 2025-01-25 21:36:56

您可以通过在适当的代码行中添加返回&lt; value&gt;,来使用退出代码。
使用&lt; value&gt;是与您的接口定义相匹配的,如果您的在线法官似乎是1。
在您的代码中,您至少在这里没有这样做:

else
    {
        printf("Sorry\n");
    }

这应该是

else
    {
        printf("Sorry\n");
        return 1;
    }

一种替代方案是更明确的 https://en.cppreference.com/w/c/program/exit 对于通往程序结束的路径并不那么明显的情况。

(这主要是Lundin提到的评论。我将其变成了一个明确的答案。)

但是,要完全满足法官,您需要处理您的输出。
有了问题中提供的信息,无法解决这些问题。

You use the exit code by adding a return <value>, at the appropriate code line.
With <value> being what matches your interface definition, in case of your online judge it seems to be a 1.
In your code you at least fail to do so here:

else
    {
        printf("Sorry\n");
    }

which should be

else
    {
        printf("Sorry\n");
        return 1;
    }

An alternative is the more explicit https://en.cppreference.com/w/c/program/exit for situations in which the path to the end of the program is not as obvious.

(This is mostly what the comment by Lundin mentions. I turned it into an explicit answer.)

However, to completely satisfy the judge you need to work on your output.
With the info given in the question, a solution for those problems is not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文