简体   繁体   English

可以在C中添加非常大的数字,但不能少的数字

[英]Can add very large numbers but not small numbers in C

I have a C program that can add large numbers and it works fine but I also need to do add smaller numbers but I get a odd output and am not sure where the error is coming from. 我有一个可以添加大数的C程序,它可以正常工作,但是我也需要添加小数,但是我得到的输出是奇数,并且不确定错误来自何处。

#include<stdio.h>
#include<string.h>

int main() {
int num1[255], num2[255], sum[255], m = 0, z, x = 0;
char s1[255], s2[255], in[255];
int l1, l2;

printf("enter the input as a+b=\n");
scanf("%s", in);
for (z = 0; in[z] != '+'; z++) {
    s1[z] = in[z];
    m++;
}

for (z = m + 1; z < strlen(in) - 1; z++) {
    s2[x] = in[z];
    x++;
}

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

    if (carry != 0) {
        sum[k] = carry;
        k = k + 1;
    }
} else {

    if (carry > 0)
        sum[k++] = carry;
}

printf("%s+%s=\n", s1, s2);

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

If I enter a values such as: 如果输入以下值:

enter the input as a+b= 输入输入为a + b =

9999999999999999999999999999+1= 9999999999999999999999999999 + 1 =

I get this output which is what I wanted it to be 我得到的输出就是我想要的

9999999999999999999999999999+1= 9999999999999999999999999999 + 1 =

10000000000000000000000000000 10000000000000000000000000000

When I try something smaller like: 当我尝试较小的东西时:

123+456= 123 + 456 =

The output is different 输出不同

123ó+456= 123ó+ 456 =

16-5-6 16-5-6

I don't know where these numbers are coming from and this keeps occurring with any number less than 9999999999999999999999999999+1= It should work for any number not just a single set. 我不知道这些数字从何而来,并且任何小于99999999999999999999999999999999 + 1的数字都会不断出现,它应该适用于任何数字,而不仅仅是一个数字。

for (l1 = 0; s1[l1] != '\0'; l1++)
//           ^^^^^^^^^^^^^^

Here's a problem: You're checking for '\\0' in s1 , but you never set any element of s1 to '\\0' , so this will access uninitialized memory and potentially go out of bounds of the array. 这是一个问题:您正在s1检查'\\0' ,但从未将s1任何元素设置为'\\0' ,因此这将访问未初始化的内存并可能超出数组的范围。

You have the same problem with s2 . 您对s2有同样的问题。

There may be other problems, but this is where I stopped reading. 可能还有其他问题,但这是我停止阅读的地方。


General comments: 普通的留言:

  • Never use scanf . 切勿使用scanf
  • If you do, always check its return value to make sure it was successful. 如果这样做,请始终检查其返回值以确保成功。
  • Never use %s with scanf . 切勿将%sscanf It's a buffer overflow waiting to happen. 这是等待发生的缓冲区溢出。
  • Your code makes very stringent assumption about what the input looks like. 您的代码对输入的外观进行了非常严格的假设。 If the user doesn't type exactly what your code is expecting, fun things will happen (eg the first for (z = 0; in[z] != '+'; z++) loop will go out of bounds if there is no + in the string). 如果用户未正确输入您的代码所期望的内容,则会发生有趣的事情(例如,第一个for (z = 0; in[z] != '+'; z++)循环将超出范围。字符串中的+ )。 You should probably verify your assumptions and not blindly assume everything is OK. 您可能应该验证您的假设,而不是盲目地假设一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM