★大数加法(C语言实现)★
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// generate carry temp_str[max - 1]++; a -= 10; } temp_str[max] = a + '0'; // integer to character }
}else{ // num2 longer than num1 or equal to
strcpy(temp_str+2, num2+1);
strcpy(result+1, temp_str); result[0] = '-'; } temp_str = temp_str2; // restore
}else{ // mark = 0 // final is a positive number(abs(num1) > abs(num2))
max += 2; // plus sign-bit and carry-bit(maybe) temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); // NOTE: must be '0', not 0
// // 大数加法
// try_fli // 08/23/2010 //
大数加法
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h>
// disable warnings #pragma warning(disable:4996) // deprecated function #pragma warning(disable:4267) // possible loss of data (size_t to int)
temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num2);
// num2 minus num1 while(min-- > 0) {
max++; // plus negative sign temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num2);
// num2 minus num1 while(min-- > 0) {
// num1 minus num2 while(min-- > 0) {
a = temp_str[--max] - num2[--bits2]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*(++temp_str) == '0') ; if(*temp_str == '\0') result = "0"; else{
a = temp_str[--max] - num2[bits2--]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*temp_str == '0') temp_str++; if(*temp_str == '\0') result = "0"; else strcpy(result, temp_str); temp_str = temp_str2; // restore }
if(temp_str) free(temp_str); return ; }
// num1 is a positive number and num2 is a negative number else if(num1[0] != '-' && num2[0] == '-') {
// get bits(exclude sign) bits1 = strlen(num1);
bits2 = strlen(num2) - 1; max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
// get final sign if(bits1 > bits2) mark = 0; else if(bits1 < bits2) mark = 1; else{
if(temp_str) free(temp_str); return ; }
// both are negative else if(num1[0] == '-' && num2[0] == '-') {
// both are negative bits1 = strlen(num1) - 1; bits2 = strlen(num2) - 1; max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
} temp_str[max] = a + '0'; } }
// get final sign if(bits1 > bits2) mark = 1; else if(bits1 < bits2) mark = 0; else{
temp = strcmp(num1+1, num2); if(temp > 0) mark = 1; else if(temp < 0) mark = 0; else { result = "0"; return; } }
// num1 longer than num2 if(bits1 > biwk.baidu.coms2) {
strcpy(temp_str+2, num1+1);
// num1 plus num2 while(min-- > 0) {
a = (temp_str[--max] - '0') + (num2[bits2--] - '0'); if(a >= 10) {
// num2 plus num1 while(min-- > 0) {
a = (temp_str[--max] - '0') + (num1[bits1--] - '0'); if(a >= 10)
{ // generate carry temp_str[max - 1]++; a -= 10;
temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num1);
// num1 minus num2 while(min-- > 0) {
// final is a negative number(abs(num1) > abs(num2)) if(mark == 1) {
max++; // plus the negative sign temp_str = (char *) malloc(max+1); // plus the space of character '\0' assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num1);
// high-precision integer additions void add(const char *num1, const char *num2, char *result) {
// special cases if(!result) return; if(!num1) { strcpy(result, num2); return; } if(!num2) { strcpy(result, num1); return; }
// num1 is a negative number and num2 is a positive number if(num1[0] == '-' && num2[0] != '-') {
// get the bits of integers bits1 = strlen(num1) - 1; bits2 = strlen(num2); max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
strcpy(result+1, temp_str); result[0] = '-'; // don't forget the negative sign } temp_str = temp_str2; // restore
}else{ // mark = 0 // final is a positive number(abs(num1) < abs(num2))
a = temp_str[--max] - num1[bits1--]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*temp_str == '0') temp_str++; if(*temp_str == '\0') result = "0"; else strcpy(result, temp_str); temp_str = temp_str2; // restore }
char mark; // negative sign (0-positive, 1-negative) int bits1, bits2; // bits of integer(exclude sign) char *temp_str = NULL; // pointer to result string keep in heap char *temp_str2 = NULL; // pointer to result string keep in heap int temp, max, min, a; // temporary variable
temp = strcmp(num1, num2+1); if(temp > 0) mark = 0; else if(temp < 0) mark = 1; else { result = "0"; return; } }
// final is a negative number(abs(num1) < abs(num2)) if(mark == 1) {
a = temp_str[--max] - num1[--bits1]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*(++temp_str) == '0') ; if(*temp_str == '\0') result = "0"; else{
}else{ // num2 longer than num1 or equal to
strcpy(temp_str+2, num2+1);
strcpy(result+1, temp_str); result[0] = '-'; } temp_str = temp_str2; // restore
}else{ // mark = 0 // final is a positive number(abs(num1) > abs(num2))
max += 2; // plus sign-bit and carry-bit(maybe) temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); // NOTE: must be '0', not 0
// // 大数加法
// try_fli // 08/23/2010 //
大数加法
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h>
// disable warnings #pragma warning(disable:4996) // deprecated function #pragma warning(disable:4267) // possible loss of data (size_t to int)
temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num2);
// num2 minus num1 while(min-- > 0) {
max++; // plus negative sign temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num2);
// num2 minus num1 while(min-- > 0) {
// num1 minus num2 while(min-- > 0) {
a = temp_str[--max] - num2[--bits2]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*(++temp_str) == '0') ; if(*temp_str == '\0') result = "0"; else{
a = temp_str[--max] - num2[bits2--]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*temp_str == '0') temp_str++; if(*temp_str == '\0') result = "0"; else strcpy(result, temp_str); temp_str = temp_str2; // restore }
if(temp_str) free(temp_str); return ; }
// num1 is a positive number and num2 is a negative number else if(num1[0] != '-' && num2[0] == '-') {
// get bits(exclude sign) bits1 = strlen(num1);
bits2 = strlen(num2) - 1; max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
// get final sign if(bits1 > bits2) mark = 0; else if(bits1 < bits2) mark = 1; else{
if(temp_str) free(temp_str); return ; }
// both are negative else if(num1[0] == '-' && num2[0] == '-') {
// both are negative bits1 = strlen(num1) - 1; bits2 = strlen(num2) - 1; max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
} temp_str[max] = a + '0'; } }
// get final sign if(bits1 > bits2) mark = 1; else if(bits1 < bits2) mark = 0; else{
temp = strcmp(num1+1, num2); if(temp > 0) mark = 1; else if(temp < 0) mark = 0; else { result = "0"; return; } }
// num1 longer than num2 if(bits1 > biwk.baidu.coms2) {
strcpy(temp_str+2, num1+1);
// num1 plus num2 while(min-- > 0) {
a = (temp_str[--max] - '0') + (num2[bits2--] - '0'); if(a >= 10) {
// num2 plus num1 while(min-- > 0) {
a = (temp_str[--max] - '0') + (num1[bits1--] - '0'); if(a >= 10)
{ // generate carry temp_str[max - 1]++; a -= 10;
temp_str = (char *) malloc(max+1); assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num1);
// num1 minus num2 while(min-- > 0) {
// final is a negative number(abs(num1) > abs(num2)) if(mark == 1) {
max++; // plus the negative sign temp_str = (char *) malloc(max+1); // plus the space of character '\0' assert(temp_str != NULL); memset(temp_str, '0', max+1); strcpy(temp_str, num1);
// high-precision integer additions void add(const char *num1, const char *num2, char *result) {
// special cases if(!result) return; if(!num1) { strcpy(result, num2); return; } if(!num2) { strcpy(result, num1); return; }
// num1 is a negative number and num2 is a positive number if(num1[0] == '-' && num2[0] != '-') {
// get the bits of integers bits1 = strlen(num1) - 1; bits2 = strlen(num2); max = bits1 > bits2 ? bits1 : bits2; min = bits1 < bits2 ? bits1 : bits2;
strcpy(result+1, temp_str); result[0] = '-'; // don't forget the negative sign } temp_str = temp_str2; // restore
}else{ // mark = 0 // final is a positive number(abs(num1) < abs(num2))
a = temp_str[--max] - num1[bits1--]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*temp_str == '0') temp_str++; if(*temp_str == '\0') result = "0"; else strcpy(result, temp_str); temp_str = temp_str2; // restore }
char mark; // negative sign (0-positive, 1-negative) int bits1, bits2; // bits of integer(exclude sign) char *temp_str = NULL; // pointer to result string keep in heap char *temp_str2 = NULL; // pointer to result string keep in heap int temp, max, min, a; // temporary variable
temp = strcmp(num1, num2+1); if(temp > 0) mark = 0; else if(temp < 0) mark = 1; else { result = "0"; return; } }
// final is a negative number(abs(num1) < abs(num2)) if(mark == 1) {
a = temp_str[--max] - num1[--bits1]; if(a < 0) {
temp_str[max - 1]--; a += 10; } temp_str[max] = a + '0'; }
// remove redundancy zero temp_str2 = temp_str; // store pointer to heap memory in order to free while(*(++temp_str) == '0') ; if(*temp_str == '\0') result = "0"; else{