Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 添加两个数组并检查是否存在溢出_C_Arrays - Fatal编程技术网

C 添加两个数组并检查是否存在溢出

C 添加两个数组并检查是否存在溢出,c,arrays,C,Arrays,我试图编写一些代码,当我们有两个数组时,如果有溢出,它将返回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 th

我试图编写一些代码,当我们有两个数组时,如果有溢出,它将返回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;
             }

    }
/**
*说明:
*该函数取三个整数:前两个是和的操作数
*总数留在第三位。
*参数:
*N1-和的第一个操作数
*N2-第二个操作数和
*Res-交易金额的结果
*返回:
*函数在成功时返回true,在“overlfow”时返回false
* /
#包括
#包括
#包括
#定义最大数字80
typedef无符号字符字节;
typedef byte BigInt[MaxDigits];
bool addBigInt(常量BigInt n1、常量BigInt n2、BigInt res){
int c=0;

对于(int i=0;i而言,编写有助于快速测试的测试代码总是好的:

#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()

请参阅并提供一个。你说“这不对”是什么意思?说明当前输出是什么,以及您期望的是什么。@2501我更改了代码,但它仍然执行相同的操作。您没有首先解释它的“功能”。请回答前两条注释并编辑问题,这样它将包含一个实际问题。
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]