使用字符数组添加长号时出错

时间:2016-05-27 05:42:42

标签: c

下面写的代码工作得很好,但只发出一个我无法找到原因的错误。当我们添加99+1999+1等数字时,它只打印零而不打印一个。如果您指出代码中的错误并更正它,我宁愿感谢您的帮助。

#include<stdio.h>
int main()
{
    int num1[255], num2[255], sum[255];
    char s1[255], s2[255];
    int l1, l2,h;

    printf("Enter Number1:");
    scanf("%s", &s1);
    printf("Enter Number2:");
    scanf("%s", &s2);


    for (l1 = 0; s1[l1] != '\0'; l1++)
        num1[l1] = s1[l1] - 48;

    for (l2 = 0; s2[l2] != '\0'; l2++)
        num2[l2] = s2[l2] - 48;

    int carry = 0;
    int k = 0;
    int i = l1 - 1;
    int j = l2 - 1;

    for (; i >= 0 && j >= 0; i--, j--, k++)
    {
        sum[k] = (num1[i] + num2[j] + carry) % 10;
        carry = (num1[i] + num2[j] + carry) / 10;
    }

    if(l1==0 && carry==1)
    {
        sum[k++]=1;
    }
    else if (l1 > l2)
    {
        while (i >= 0)
        {
            sum[k++] = (num1[i--] + carry)%10 ;
            carry = (num1[i]+carry)/10;
        }
        if(i==0&&carry==1)
        {
            sum[k++]=carry;
        }
    }
    else if(l1<l2)
    {
        while (j >= 0)
        {
            sum[k++] = (num2[j--] + carry)%10 ;
            carry = (num2[j]+carry)/10;
        }
        if(j==0&&carry==1)
        {
            sum[k++]=carry;
        }
    }

    printf("Result:");

    for (k--; k>= 0; k--)
        printf("%d", sum[k]);

    return 0;
}

3 个答案:

答案 0 :(得分:2)

您需要编写进位计数器,然后读取它。您只需要对进位进行微调,然后就可以了。

#include <stdio.h>

int main() {
    int num1[255], num2[255], sum[255];
    char s1[255], s2[255];
    int l1, l2;

    printf("Enter Number1:");
    scanf("%s", s1);
    printf("Enter Number2:");
    scanf("%s", s2);    

    for (l1 = 0; s1[l1] != '\0'; l1++)
        num1[l1] = s1[l1] - '0';

    for (l2 = 0; s2[l2] != '\0'; l2++)
        num2[l2] = s2[l2] - '0';

    int carry = 0;
    int k = 0;
    int i = l1 - 1;
    int j = l2 - 1;
    for (; i >= 0 && j >= 0; i--, j--, k++) {
        sum[k] = (num1[i] + num2[j] + carry) % 10;
        carry = (num1[i] + num2[j] + carry) / 10;
    }
    if (l1 > l2) {


        while (i >= 0) {
            sum[k++] = (num1[i] + carry) % 10;
            carry = (num1[i--] + carry) / 10;
        }

        if (sum[k - 1] != carry)
            sum[k++] = carry;

    } else if (l1 < l2) {
        while (j >= 0) {
            sum[k++] = (num2[j] + carry) % 10;
            carry = (num2[j--] + carry) / 10;
        }
        if (sum[k - 1] != carry)
            sum[k++] = carry;
    } else {
        if (carry > 0)
            sum[k++] = carry;
    }


    printf("Result:");
    for (k--; k >= 0; k--)
        printf("%d", sum[k]);

    return 0;
}

<强>测试

Enter Number1:99
Enter Number2:1
Result:100

测试2

Enter Number1:9999999999999999999999999999999999
Enter Number2:1
Result:10000000000000000000000000000000000

答案 1 :(得分:2)

  1. 在while循环(i> = 0)中,您在求和步骤中递减索引i。您正在下面的进位步骤中再次使用此索引。你应该在下一个步骤中对它进行去除。

  2. 之后的if条件,if(i==0&&carry==1) i在循环结束时始终为-1。因此i==0的条件可以删除。

  3. 固定代码如下。

    #include<stdio.h>
    int main() {
      int num1[255], num2[255], sum[255];
      char s1[255], s2[255];
      int l1, l2,h;
    
      printf("Enter Number1:");
      scanf("%s", s1);
      printf("Enter Number2:");
      scanf("%s", s2);
    
    
      for (l1 = 0; s1[l1] != '\0'; l1++)
        num1[l1] = s1[l1] - 48;
    
      for (l2 = 0; s2[l2] != '\0'; l2++)
        num2[l2] = s2[l2] - 48;
    
      int carry = 0;
      int k = 0;
      int i = l1 - 1;
      int j = l2 - 1;
      for (; i >= 0 && j >= 0; i--, j--, k++) {
        sum[k] = (num1[i] + num2[j] + carry) % 10;
        carry = (num1[i] + num2[j] + carry) / 10;
      }
      if(l1==0&&carry==1)
      {
        sum[k++]=1;
      }
      else if (l1 > l2) {
    
        while (i >= 0) {
          sum[k++] = (num1[i] + carry)%10 ;
          carry = (num1[i--]+carry)/10;
        }
        if(carry==1)
        {
          sum[k++]=carry;
        }
      } else if(l1<l2) {
        while (j >= 0) {
          sum[k++] = (num2[j] + carry)%10 ;
          carry = (num2[j--]+carry)/10;
        }
        if(carry==1)
        {
          sum[k++]=carry;
    
        }
      }
    
      printf("Result:");
      for (k--; k>= 0; k--)
        printf("%d", sum[k]);
    
      return 0;
    }
    

    <强>测试1

    Enter Number1:99
    Enter Number2:2
    Result:101
    

    <强>的Test2

    Enter Number1:22
    Enter Number2:99998
    Result:100020
    

答案 2 :(得分:0)

试试这段代码。我认为这应该对你有帮助。

#include<stdio.h>
int main()
{
int num1[255], num2[255], sum[255];
char s1[255], s2[255];
int l1, l2,h;

printf("Enter Number1:");
scanf("%s", &s1);
printf("Enter Number2:");
scanf("%s", &s2);

for (l1 = 0; s1[l1] != '\0'; l1++)
    num1[l1] = s1[l1] - '0';

for (l2 = 0; s2[l2] != '\0'; l2++)
    num2[l2] = s2[l2] - '0';

int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;

for (; i >= 0 && j >= 0; i--, j--, k++)
{
    sum[k] = (num1[i] + num2[j] + carry) % 10;
    carry = (num1[i] + num2[j] + carry) / 10;
}

//if num2 has more digits than num1

if(i==-1 && j>=0)
{  
      while(j>=0)
      {
          sum[k++]=(num2[j]+carry)%10;
          carry=(num2[j]+carry)/10;
          j--;
      }
}

//if num1 has more digits than num2

if(i>=0 && j==-1)
{  
      while(i>=0)
      {
          sum[k++]=(num1[i]+carry)%10;
          carry=(num1[i]+carry)/10;
          i--;
      }
}
 // both has reached to their last digits position and suppose the carry is still left
 if(i==-1 && j==-1)
 { 
    if(carry>0)
      sum[k++]=carry;
 }

printf("Result:");

for (k=k-1; k>= 0; k--)
    printf("%d", sum[k]);

return 0;
}