我正在尝试制作一些代码,当我们有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;
}
}
答案 0 :(得分:0)
编写便于快速测试的测试代码总是好的:
#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]