0

这段代码通过加法运算添加两个整数,包括进位过程,我在这里遇到错误,但我不知道这里到底是什么!该程序的目的是使c ++采用“大于int大小”的大数字并进行加法过程..我真的需要帮助!谢谢 !

#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

class BigDecimalInt
{
    protected:
    int num1;
    public:
    string s1;

    string getstring()
    {
        return s1;
    }
    void setstring(string s2)
    {
        s1=s2;
    }
    BigDecimalInt (string decStr){s1=decStr;}
    BigDecimalInt (int decInt); // Initialize from integer
    BigDecimalInt operator+ ();
    BigDecimalInt operator- (BigDecimalInt anotherDec);
};

BigDecimalInt operator+ (BigDecimalInt firstDec,BigDecimalInt secDec)
{
    int size,diff,carry=0,x,y,z,counter=0;
    BigDecimalInt temp("");
    string c1,c2,result,smaller,bigger;
    c1=firstDec.getstring();
    c2=secDec.getstring();
    if(c1.length()<c2.length())
        {
            smaller = c1;
            bigger = c2;
        }
    else
       {
           smaller = c2;
           bigger = c1;
       }
    diff = bigger.length()-smaller.length();
    size = smaller.length();
    for(int i=size-1;i>=0;i--)
        {
            y = int(bigger[i+diff]);
            y-=48;
            x = int(smaller[i]);
                x-=48;
            z = x+y+carry;
            if(z<=9)
            {
                result+=char(z+48);
                carry = 0;
            }
            else
            {
                result+=char(z+38);
                carry = 1;
            }
        }
    while((carry==1)&&(counter<bigger.length()))
    {
        counter = size;
        x = int(bigger[counter])-48+carry;
        if(x<=9)
            {
                result+=char(x+48);
                carry = 0;
            }
        else
        {
            result+=char(x+38);
            carry = 1;
        }
        counter++;
    }
    if(counter==bigger.length())
        {
            if(carry==1)
            result+=char(49);
        }
    else
        for(int o=counter;o>=0;o++)
            result+=bigger[o];

    temp.s1=result;

    return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
     string k1,k2;

    cout<<"Enter first num : \n";
    cin>>k1;
    BigDecimalInt c1(k1);
    cout<<"Enter second num : \n";
    cin>>k2;
    BigDecimalInt c2(k2);

    BigDecimalInt c3("");

    c3=c1+c2;
    cout<<c3.s1;
    system("pause");
    return 0;
}
4

2 回答 2

1

如果counter == bigger.length()你下标biggerbigger.length()而最后一个有效索引是bigger.length() - 1.

于 2012-10-20T10:51:09.647 回答
1

这个 for 循环与众不同

for(int o=counter;o>=0;o++)

所有其他人都在倒数零,但这似乎正在数数。

于 2012-10-20T10:55:53.027 回答