2013-02-03 6 views
1

BigInt 클래스 용 코드를 구현하고 있는데 객체를 표시하는 데 문제가 있습니다. 내가 뭘 잘못하고 있지만 내 결과가 거꾸로 들었는지 잘 모르겠습니다. 도와주세요!BigInt 표시 함수

BigInt.h 

#include <iostream> 
using namespace std; 
using std::cout; 

#ifndef BIGINT_H 
#define BIGINT_H 

class BigInt 
{ 
//input and output operators 
    friend istream & operator >> (istream &, BigInt &); 
    friend ostream & operator << (ostream &, const BigInt &); 

public: 
    BigInt(); //default constructor 
    BigInt(int); //initializes array with user-specified numbers 

    BigInt operator + (BigInt &); 

    void display(); //prints array 

private: 
    static const int CAPACITY = 40; 
    int Digits[CAPACITY]; //stores all digits 
}; 
#endif 

BigInt.cpp

#include <iostream> 
#include "BigInt.h" 
using std::cout; 

BigInt::BigInt() 
{ 
    for (int i = 0; i < CAPACITY; i++) 
     Digits[i] = 0; 
} 

BigInt::BigInt(int InitNum) 
{ 
    //Inputs the individual numbers given to BigInt into the Digits array's elements 
    for(int i = 0; i < CAPACITY; i++) 
    { 
     if(InitNum > 0) 
     { 
     Digits[i] = InitNum%10; 
     InitNum = InitNum/10; 
     } 
     else 
     Digits[i]=0; 
    } 
} 

//------------------------------------------------------------------ 
BigInt BigInt::operator +(BigInt & a) 
{ 
    for(int i = CAPACITY - 1; i >= CAPACITY; i--) 
     Digits[i]+=a.Digits[i]; 
} 

//----------------------------------------------------------------- 
ostream & operator << (ostream & cout, const BigInt& a) 
{ 
    for(int i=0; i< a.CAPACITY ; i++) 
     cout << a.Digits[i]; 
    return cout; 
} 

istream & operator >> (istream & cin, BigInt& a) 
{ 
    for(int i = 0; i < a.CAPACITY; ++i) 
     cin >> a.Digits[i]; 

    return cin; 
} 

//--------------------------------------------------------------- 
void BigInt::display() 
{ 
    for(int i = 0; i< CAPACITY; i++) 
     cout << Digits[i]; 

    cout << "\n"; 
} 

하여 Main.cpp

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
#include "BigInt.h" 

int main() 
{ 
    BigInt object1(45756369); 
    BigInt object2(47435892); 

    object1.display(); 
    object2.display(); 

    BigInt object3 = object1 + object2; 

    cout << object3; 

    return 0; 
} 

감사합니다! 또한 연산자 + 함수는 괜찮습니까?

답변

0

생성자에서 숫자를 역순으로 입력합니다. Num % 10은 마지막 숫자를 줄 것입니다 ... 생성자에서 채우면 배열을 뒤집어야합니다.