c - 两个数组相加并检查是否溢出

标签 c arrays

我正在尝试编写一些代码,当我们有 2 个数组时,如果存在溢出,则返回 true,如果没有溢出,则返回 false。我编写了一些代码,但看起来不正确:

/ **
 * Description :
 * The function takes three integer: the first two are the operands of the sum
 * And the sum is left in the third.
 * Parameters :
 * N1 - first operand of the sum
 * N2 - second operand sum
 * Res - a result of the transaction sum
 * Return:
 * The function returns true on success or false in the event of " overlfow "
 * /

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

   #define MaxDigits 80

   typedef unsigned char byte;
   typedef byte BigInt [MaxDigits];


   bool  addBigInt( const BigInt n1, const BigInt n2, BigInt res ) {
    int c=0;
    for(int i = 0; i<MaxDigits; i++){
        res[i]=0;
    }
    for(int i = 0; i<MaxDigits; i++){

    res[i] = n1[i]+n2[i]+c;
    if( res[i]>=10){
        res[i]=res[i]-10;   
        c=1;
    }
    if (res[i]<10){
        c=0;
        }
    }
    if (c==1){
        return true;
             }

    else{
        return false;
             }

    }

最佳答案

编写有助于快速测试的测试代码总是好的:

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

#define MAX_DIGITS 80

typedef unsigned char byte;
typedef byte BigInt [MAX_DIGITS];

bool addBigInt(const BigInt n1, const BigInt n2, BigInt res );
void printBigInt(const BigInt n);

int main(int argc, char *argv[] ) {

    if (argc != 3) { fprintf(stderr, "error: require 2 arguments.\n"); exit(1); }
    int n1Len = strlen(argv[1]);
    if (n1Len == 0) { fprintf(stderr, "error: n1 is empty.\n" ); exit(1); }
    if (n1Len > MAX_DIGITS) { fprintf(stderr, "error: n1 length %d is greater than max %d.\n", n1Len, MAX_DIGITS ); exit(1); }
    int n2Len = strlen(argv[2]);
    if (n2Len == 0) { fprintf(stderr, "error: n2 is empty.\n" ); exit(1); }
    if (n2Len > MAX_DIGITS) { fprintf(stderr, "error: n2 length %d is greater than max %d.\n", n2Len, MAX_DIGITS ); exit(1); }

    BigInt n1;
    BigInt n2;
    for (int i = 0; i < MAX_DIGITS; ++i) {
        n1[i] = 0;
        n2[i] = 0;
    } // end for
    for (int i = 0; i < n1Len; ++i) {
        char c = argv[1][i];
        if (c < '0' || c > '9') { fprintf(stderr ,"error: n1 has invalid char '%c'.\n", c ); exit(1); }
        n1[n1Len-1-i] = c-'0';
    } // end for
    for (int i = 0; i < n2Len; ++i) {
        char c = argv[2][i];
        if (c < '0' || c > '9') { fprintf(stderr ,"error: n2 has invalid char '%c'.\n", c ); exit(1); }
        n2[n2Len-1-i] = c-'0';
    } // end for

    BigInt res;
    bool overflow = addBigInt(n1,n2,res);

    printBigInt(n1);
    printf(" + ");
    printBigInt(n2);
    printf(" = ");
    printBigInt(res);
    if (overflow) printf(" [overflow]");
    printf("\n");

    return 0;

} // end main()

bool addBigInt(const BigInt n1, const BigInt n2, BigInt res ) {

    for (int i = 0; i < MAX_DIGITS; ++i) res[i] = 0;

    int c = 0;
    for (int i = 0; i < MAX_DIGITS; ++i) {

        res[i] = n1[i]+n2[i]+c;

        if (res[i] >= 10) {
            res[i] = res[i]-10;
            c = 1;
        } else {
            c = 0;
        } // end if

    } // end for

    return c == 1;

} // end addBigInt()

void printBigInt(const BigInt n) {
    int lastDigitIndex;
    for (lastDigitIndex = MAX_DIGITS-1; lastDigitIndex > 0 && n[lastDigitIndex] == 0; --lastDigitIndex) ; // no-op
    for (int i = lastDigitIndex; i >= 0; --i) printf("%d",n[i]);
} // end printBigInt()

演示:

ls;
## addBigInt.c
gcc addBigInt.c -o addBigInt;
ls;
## addBigInt.c  addBigInt.exe
./addBigInt;
## error: require 2 arguments.
./addBigInt a b c;
## error: require 2 arguments.
./addBigInt a b;
## error: n1 has invalid char 'a'.
./addBigInt 0 b;
## error: n2 has invalid char 'b'.
./addBigInt 0 0;
## 0 + 0 = 0
./addBigInt 0 1;
## 0 + 1 = 1
./addBigInt 1 0;
## 1 + 0 = 1
./addBigInt 5 7;
## 5 + 7 = 12
./addBigInt 12 34;
## 12 + 34 = 46
./addBigInt 999 11;
## 999 + 11 = 1010
./addBigInt 999999999999999999999999999999999999999999999999999999999999999999999999999999999 0;
## error: n1 length 81 is greater than max 80.
./addBigInt 99999999999999999999999999999999999999999999999999999999999999999999999999999999 0;
## 99999999999999999999999999999999999999999999999999999999999999999999999999999999 + 0 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999
./addBigInt 99999999999999999999999999999999999999999999999999999999999999999999999999999999 1;
## 99999999999999999999999999999999999999999999999999999999999999999999999999999999 + 1 = 0 [overflow]

关于c - 两个数组相加并检查是否溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37156331/

相关文章:

c - 防止 valgrind 检查与我们的应用程序链接的共享库中的内存泄漏

c - MPI_Irecv 没有正确接收 MPI_Send 发送的数据

c - 我们如何根据 c 中的行设置文件指针新位置

c - Termcap tgetstr 获取方向键

c - 段错误 - 不同的 glib-c 版本可以做类似的事情吗?

c++ - 试图传递二维字符数组,但得到垃圾

c++ - vector 循环无法正常运行

ios - 如何在 Swift 中获取元组数组的最大 Double 值?

arrays - 为动态填充的对象数组生成 Mongoose 模式

python - 无法存储numpy.ndarray'对象没有属性 'save'