在链表计算器中添加值

发布于 2024-10-31 18:51:27 字数 1003 浏览 1 评论 0原文

我正在用 C 语言开发一个单链表计算器(是的,这是家庭作业)。我有添加函数“工作”,但由于某种原因我只能添加两个长度相同的值。我真的不知道如何添加 12 + 128 之类的东西。目前我的代码只接受 120 + 128。我做错了什么,如何修复此代码?

struct digit* add(struct digit *x, struct digit *y)
{
    int carry = 0;
    struct digit *xHead;
    struct digit *yHead;
    struct digit *totalHead;
    struct digit *current_Digit;

    xHead = x;
    yHead = y;
    totalHead = NULL;


    while(x != NULL && y != NULL)
    {
        current_Digit = (struct digit *)malloc(sizeof(struct digit));
        current_Digit->value = x->value + y->value + carry;

        //calculates the carry
        carry = 0;
        if(current_Digit->value > 9)
        {
            carry = 1;
            current_Digit->value = current_Digit->value % 10;
        }
        else
        {
            carry = 0;
        }
        current_Digit->next = totalHead;
        totalHead = current_Digit;

        x = x->next;
        y = y->next;
    }
    return totalHead;
}

I am working on a single linked-list calculator in C (yes, it's homework). I have the add functions "working" but for some reason I can only add two values that are the same length. I can't really figure out how to add something like 12 + 128. Currently my code only accepts 120 + 128. What did I do wrong, how can I fix this code?

struct digit* add(struct digit *x, struct digit *y)
{
    int carry = 0;
    struct digit *xHead;
    struct digit *yHead;
    struct digit *totalHead;
    struct digit *current_Digit;

    xHead = x;
    yHead = y;
    totalHead = NULL;


    while(x != NULL && y != NULL)
    {
        current_Digit = (struct digit *)malloc(sizeof(struct digit));
        current_Digit->value = x->value + y->value + carry;

        //calculates the carry
        carry = 0;
        if(current_Digit->value > 9)
        {
            carry = 1;
            current_Digit->value = current_Digit->value % 10;
        }
        else
        {
            carry = 0;
        }
        current_Digit->next = totalHead;
        totalHead = current_Digit;

        x = x->next;
        y = y->next;
    }
    return totalHead;
}

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

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

发布评论

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

评论(3

邮友 2024-11-07 18:51:27

您的函数不应同时转到 x->nexty->next,而是应执行以下操作:(

while (x != NULL || y != NULL) {
    // malloc

    current_Digit->value = (x ? x->value : 0)
                         + (y ? y->value : 0)
                         + carry;

    // compute

    if (x) x = x->next;
    if (y) y = y->next;
}

它看起来也好像您正在构建结果向后列出...)

Instead of going to x->next and y->next simultaneously, your function should do the following:

while (x != NULL || y != NULL) {
    // malloc

    current_Digit->value = (x ? x->value : 0)
                         + (y ? y->value : 0)
                         + carry;

    // compute

    if (x) x = x->next;
    if (y) y = y->next;
}

(It also looks as if you're constructing your result list backwards...)

笑红尘 2024-11-07 18:51:27

您当前正在递增两个参数的数字,而不查看是否已到达其中一个参数的末尾。您需要进行一项特殊测试,如果只有一个链表位于其末尾,则不要增加它并假设其数字值为零。

因此 12 + 128 应该动态地变为 [0]12 + 128。您必须添加逻辑来识别本例中的 x 值已到达其数字末尾,但 y 尚未到达。因此,继续使用 y 并为 x 数字变出零。

You're currently incrementing the digits of both arguments without looking at whether you've hit the end of one of them. You need to have a special test that if only one linked list is at its end, then don't increment it and just assume its digit value is zero.

So 12 + 128 should be dynamically made as [0]12 + 128. You must add logic to recognize that the x value in this case has reached the end of its digits but the y has not. So keep going with the y and conjure zero for the x digit.

吃兔兔 2024-11-07 18:51:27

您应该确保 xy 具有相同的位数,否则最终将设置为 NULL。在添加之前,找到最短的并添加零,直到匹配另一个的长度

You should ensure x and y have the same number of digits, otherwise you'll end up with either set to NULL. Before adding, find the shortest and add zeros to it until you match the other's length

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