C++ 长整数加法运算问题

[复制链接]
查看11 | 回复1 | 2009-7-11 10:48:30 | 显示全部楼层 |阅读模式
为什么我的这个算法显示不出最后一位 就是比如23500+77777=01277 最前面一个1 (在原变量里是最后一个 我是把它倒过来了)
#include
#include
using namespace std;
void sumArrStrNum(char* arrStringNum_1, char* arrStringNum_2, char* arrStringNum_3);
void revArray(char* arrStringNum_3);
const int MAX_SIZE = 30, DECIMAL_BASE = 10;
char arrStringNum_1[MAX_SIZE], arrStringNum_2[MAX_SIZE], arrStringNum_3[MAX_SIZE];
void main() {
cout > arrStringNum_1;
cout > arrStringNum_2;
sumArrStrNum(arrStringNum_1, arrStringNum_2, arrStringNum_3);
system("pause");
}
void sumArrStrNum(char* tempArray_1, char* tempArray_2, char* tempArray_3) {
int tempSum = 0, opndParam = 0, strLength_1, strLength_2, strLength_3 = 0;
strLength_1 = strlen(tempArray_1) - 1;
strLength_2 = strlen(tempArray_2) - 1;
for ( ; strLength_1 >= 0 || strLength_2 >= 0; strLength_1 --, strLength_2 --, strLength_3 ++) {
if (strLength_1 >= 0 && strLength_2 >= 0) {
tempSum = (tempArray_1[strLength_1] - '0') + (tempArray_2[strLength_2] - '0') + opndParam;
} else if (strLength_1 >= 0 && strLength_2 = 0) {
tempSum = (tempArray_2[strLength_2] - '0') + opndParam;
}
if (tempSum >= DECIMAL_BASE) {
tempArray_3[strLength_3] = tempSum - DECIMAL_BASE + '0';
opndParam = 1;
} else {
tempArray_3[strLength_3] = tempSum + '0';
opndParam = 0;
}
}
//tempArray_3[strLength_3] = '\1' + '0';
revArray(tempArray_3);
cout << endl << tempArray_3 << endl;
}
void revArray(char* tempArray) {
int param, tempNum, strLength;

strLength = strlen(tempArray);
for (param = 0; param < strLength / 2; param ++) {
tempNum = tempArray[param];
tempArray[param] = tempArray[strLength - param - 1];
tempArray[strLength - param - 1] = tempNum;
}
}

回复

使用道具 举报

千问 | 2009-7-11 10:48:30 | 显示全部楼层
你注释掉的哪行改为if(opndParam == 1)tempArray_3[strLength_3] ='1';...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行