读书人

两个大数相加有关问题

发布时间: 2012-04-25 19:32:32 作者: rapoo

两个大数相加问题!
代码如下所示。不知哪里出了什么逻辑上的问题,在处理进位上不正常。
求各位朋友帮忙指出。在此先行谢谢!

#include <iostream>

using namespace std;
#define ARRAY_SIZE 50

//Enter a big number, and store it as a string into an array ch,
//the size is the numbers of char.
void inputNumbers(char ch[], int& size);

//Reverse the elements of the array ch.
void reverseArray(char ch[], int size);

//Adding two big numbers, and the result will be stored in the array ch3,
//and return the size of the array ch3.
void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3);

//show the adding result.
void displayResult(char ch[], int size);

int main()
{
char ch1[ARRAY_SIZE], ch2[ARRAY_SIZE], result[ARRAY_SIZE];
int size1 = 0, size2 = 0, resultSize = 0;

cout << "Enter the first big number, ending with an enter:" << endl;
inputNumbers(ch1, size1);
cout << "Enter the second big number, ending with an enter:" << endl;
inputNumbers(ch2, size2);

reverseArray(ch1, size1);
reverseArray(ch2, size2);

computeAdding(ch1, size1, ch2, size2, result, resultSize);
displayResult(result, resultSize);

system("pause");
return 0;
}

//Function inputNumbers
void inputNumbers(char ch[], int& size)
{
char next;

cin.get(next);
while (next != '\n' && size < ARRAY_SIZE)
{
ch[size++] = next;
cin.get(next);
}
}//inputNumbers

//Function reverseArray
void reverseArray(char ch[], int size)
{
int i = 0, j = size-1;
char temp;

while (i <= j)
{
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
i ++;
j --;
}
}//end reverseArray function

//function computeAdding
void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3)
{
int i = 0, j = 0;
char result;

while (i < size1 && j < size2)
{
result = ch1[i] + ch2[j] - 48;
if (result >= '10')
{
if (size1 == i && j < size2) //If size1 < size2
ch2[j] = ch2[j] + 1;
else
ch1[i] = ch1[i] + 1;
ch3[size3++] = result - '10' + '0';
}
else
ch3[size3++] = result;
i ++;
j++;
}
if (size1 < size2)//If the ch1 has more bits, execute this clause
{
for (i = size1; i < size2; i++)
{
ch3[size3++] = ch2[i] ;
}
}
else if (size1 > size2)
{
for (i = size2; i < size1; i++)
{
ch3[size3++] = ch2[i] ;
}

}
}//End reverseArray

//function displayResult
void displayResult(char ch[], int size)
{
reverseArray(ch, size);//make the number to be normal

cout << "The adding result is:" ;
for (int i = 0; i < size; i++)
cout << ch[i] ;
cout << endl;
}










[解决办法]
楼主,你大数相加那函数写的,有点乱,我重新给你写了个,你参考下:

C/C++ code
void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3){    int i,cf,tmp;        for( i=0,cf=0; i<size1&&i<size2; i++)    {        tmp = (ch1[i]-'0') + (ch2[i]-'0') + cf;        ch3[i]= tmp%10+'0';        cf = tmp/10;    }    while( i<size1 ) {        tmp = (ch1[i]-'0')+cf;        ch3[i] = tmp%10+'0';        cf = tmp/10;        i++;    }    while( i<size2 ){        tmp = (ch2[i]-'0') +cf;        ch3[i] = tmp%10 +'0';        cf = tmp/10;        i++;    }    if( cf ) { ch3[i]= cf+'0'; i++;}    ch3[i]='\0';    size3 = i;}//End reverseArray 


[解决办法]

C/C++ code
//建议LZ不要用char类型进行相加运算,采用intvoid computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3){    int i = 0, j = 0;    char result;    int result;    int carry;    while (i < size1 && j < size2)    {        result = ch1[i] + ch2[j] - 48;  //这个地方出错了,如果是8+4得到的是12,这个时候已经不是数字字符        //这个地方需要判断进位        //最好使用int型进行操作,字符型不好判断进位        cout<<"reuslt="<<result<<endl;        if (result >= '10') //这个地方也不对,没有10这个字符的        {            if (size1 == i && j < size2) //If size1 < size2                ch2[j] = ch2[j] + 1;            else                ch1[i] = ch1[i] + 1;            ch3[size3++] = result - '10' + '0';        }        else            ch3[size3++] = result;        i++;        j++;    }    if (size1 < size2)//If the ch1 has more bits, execute this clause    {        for (i = size1; i < size2; i++)        {            ch3[size3++] = ch2[i] ;        }    }    else if (size1 > size2)    {        for (i = size2; i < size1; i++)        {            ch3[size3++] = ch2[i] ;        }            }}//End reverseArray
[解决办法]
C/C++ code
//按照整数进行改写了一下void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3){    int i = 0, j = 0;    int result = 0;    int carry = 0;//初始进位为0    while(i < size1 && j < size2)    {        //计算两个数对应位的和,带进位的计算        result = (ch1[i]^0x30) + (ch2[j]^0x30)+carry;          //取其最低位        ch3[size3++] = (result%10)^0x30;        //进位        carry = result/10;        i++;        j++;    }    //第一个数位数大于第二个位数    while(i<size1)    {        int ret = (ch1[i]^0x30) +carry;          ch3[size3++] = (ret%10)^0x30;        carry = ret/10;        i++;    }    //第二个数的位数大于第一个位数    while(j<size2)    {        int ret = (ch2[j]^0x30) +carry;          ch3[size3++] = (ret%10)^0x30;        carry = ret/10;        j++;    }    //所以计算完成看是否有进位    if(i==size1 &&j==size2 && carry!=0)        ch3[size3++] = carry^0x30;    ch3[size3] = '\0';    }//End reverseArray
[解决办法]
C/C++ code
#include <stdio.h>#include <string.h>#define MAXLEN 1000char a1[MAXLEN];char a2[MAXLEN];static int v1[MAXLEN];static int v2[MAXLEN];static int v3[MAXLEN];int i,j,n,L,z;void main(void) {    scanf("%d",&n);    for (j=0;j<n;j++) {        scanf("%s%s",a1,a2);        L=strlen(a1);        for (i=0;i<L;i++) v1[i]=a1[L-1-i]-'0';        L=strlen(a2);        for (i=0;i<L;i++) v2[i]=a2[L-1-i]-'0';        for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];        for (i=0;i<MAXLEN;i++) {            if (v3[i]>=10) {                v3[i+1]+=v3[i]/10;                v3[i]=v3[i]%10;            }        }        printf("Case %d:\n", j+1);        printf("%s + %s = ", a1, a2);        z=0;        for (i=MAXLEN-1;i>=0;i--) {            if (z==0) {                if (v3[i]!=0) {                    printf("%d",v3[i]);                    z=1;                }            } else {                printf("%d",v3[i]);            }        }        if (z==0) printf("0");        printf("\n");    }}//Sample Input//3//0 0//1 2//112233445566778899 998877665544332211////Sample Output//Case 1://0 + 0 = 0//Case 2://1 + 2 = 3//Case 3://112233445566778899 + 998877665544332211 = 1111111111111111110 

读书人网 >C++

热点推荐