在C中添加两个整数(范围可以大于long long int)

时间:2015-05-17 07:42:15

标签: c

我编写了以下代码来添加两个大于long long int的整数但不知何故它似乎进入了一个无限循环。

我只是熟悉这种语言所以代码有点笨拙。如果有人指出我的错误,那就太好了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char a[127], c[127];
    char b[127], d[127];
    char result[127];
    int i, j, k, l1, l2, sum, carry;
    printf("\n\nenter 1st number\n\n");
    gets(a);
    printf("\n\nenter 2nd number\n\n");
    gets(b);
    l1 = strlen(a);
    l2 = strlen(b);
    //  for(i=0;i<=l1;i++)
    //      printf("%c",a[i]);
    j = l1;
    k = l2;
    /*for(i=l1;i>=0;i--)
    {
    c[j]=a[i];
    j--;
    }
    for(i=l2;i>=0;i--)
    {
    d[j]=b[i];
    j--;
    }*/
    printf("\n%d %d\n", j, k);
    if (l1>l2)
    {
        for (i = l1; i <= l2; i--)
        {
            c[i] = a[j];
            d[i] = b[k];
            j--;
            k--;
        }
        for (i = l2; i <= 0; i--)
        {
            c[i] = a[j];
            d[i] = 0;
            j--;
        }

    }
    //  printf("hello");
    j = l1;
    k = l2;
    if (l1<l2)
    {
        for (i = l2; i <= l1; i--)
        {
            c[i] = a[j];
            d[i] = b[k];
            j--;
            k--;
        }
        for (i = l1; i <= 0; i--)
        {
            c[i] = 0;
            d[i] = b[k];
            k--;
        }
    }
    j = l1;
    k = l2;
    if (l1 = l2)
    {
        for (i = l2; i <= 0; i--)
        {
            c[i] = a[j];
            d[i] = b[k];
            j--;
            k--;
        }
    }
    if (l1>l2)
    {
        for (i = l1; i <= 0; i--)
            printf("%c", c[i]);
    }

    if (l1>l2)
    {
        for (i = l1; i >= 0; i--)
        {
            if (i = l1)
                carry = 0;
            else
                /*if(i=l2)
                carry[i]=0;
                else
                carry[i]=sum[i]*/
                carry = sum / 10;
            sum = ((c[i] - '0') + (d[i] - '0')) + carry;
            //  if(i!=0)
            result[i] = sum % 10 + '0';
            //  else
            //      result[i]=sum[i];
        }
        //  for(i=l1-l2;i<=l1;i++)
        //      result[i]=c[i];
    }
    if (l1<l2)
    {
        for (i = l2; i >= 0; i--)
        {
            if (i = l2)
                carry = 0;
            else
                carry = sum / 10;
            sum = (c[i] - '0') + (d[i] - '0') + carry;
            //if(i!=0)
            result[i] = sum % 10 + '0';
            //  else
            //      result[i]=sum[i];
        }
        //      for(i=l2-l1;i<=l2;i++)
        //          result[i]=d[i];
    }
    if (l1 = l2)
    {
        for (i = l1; i >= 0; i--)
        {
            if (i = l1)
                carry = 0;
            else
                carry = sum / 10;
            sum = (c[i] - '0') + (d[i] - '0') + carry;
            if (i != 0)
                result[i] = sum % 10 + '0';
            else
                result[i] = sum + '0';
        }
    }
    printf("\n\nthe result is\n\n");
    if (l1 >= l2)
    {
        for (i = 0; i <= l1; i++)
            printf("%c", result[i]);
    }
    else
    {
        for (i = 0; i <= l2; i++)
            printf("%c", result[i]);
    }
    printf("\n\n");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

首先,你不应该使用@app.route('/', methods=['GET','POST']) def index(): result = "" if request.method == "GET": name = request.form['name'] if "name" in request.form else "" result = "We received a GET request and the value for <name> is :%s" % name elif request.method == "POST": result = "We received a POST request" else : result = "We don't know what type of request we have received" return result 因为你不能指定它应该读取多少个字符;它可能会很好地溢出你的缓冲区。相反,请使用gets

您的代码永远循环的原因是您的所有fgets(buf, sizeof(buf), stdin)都错了 - 您正在使用赋值运算符if而不是比较运算符===返回指定的值,即=始终返回0,这意味着if (var = 0)内的代码永远不会被执行。修复所有if语句后,您将发现下一个问题:

enter image description here