2012-02-06 5 views
1

bigint 프로젝트를 수행하고 있는데 왜 테스트 연산자에서 추가 연산자가 제대로 작동하지 않는지를 잘 모릅니다.Bigint + operator

아마 불필요하기 때문에 .h 파일을 제외 시켰습니다.

이 테스트 케이스 bigint.cpp

#include "bigint.h" 
#include<iostream> 
#include<fstream> 
#include<cstdlib> 
#include<cassert> 



bigint::bigint() 
{       //Default constructor that sets digit to ZERO 
    for (int i = 0; i < MAX; i++) 
    { 
    digits[i] = 0; 
    } 
} 


bigint::bigint(int n) 
{ 

for(int i = 0; i < MAX; ++i)  //Sets the digit to ZERO 
    digits[i] = 0; 

    for (int i = 0; n != 0 ; ++i) 
{ 
    digits[i] = (n % 10);  // 
      n = n/10; 
} 


} 


bigint::bigint(const char new_digits[]) 
{ 
int null = 0; 
int temp = 0; 

for(int i = 0; i < MAX; ++i) 
{ 
    digits[i] = 0; 
} 

while(new_digits[null] != '\0') 
    ++null; 
    --null; 
temp = null; 

for(int j = 0; j < MAX && temp >= 0; ++j) 
{ 
    digits[j] = new_digits[temp] - '0'; 
    temp -= 1; 
} 
} 


bool bigint::operator==(const bigint& equal) const 
{ 
int i = 0; 

while(i < MAX) 
{ 
    if(digits[i] != equal.digits[i]) 
    { 
     return false; 
    } 

    ++i; 
} 
return true; 
} 


std::ostream& operator<<(std::ostream& output, const bigint& source) 
{ 

int sub1 = MAX - 1; //subtracts 1 from the maximum size 

while(source.digits[sub1] == 0) 
{ 
    --sub1;       //EMPTY 
} 

while(sub1 > -1) 
{ 
    output << source.digits[sub1]; 
    --sub1; 
} 

std::cout << std:: endl; 

return output; 
} 

std::istream& operator>>(std::istream& in, bigint& source) 
{ 
char getdata[MAX]; 
char user_input; 
int i = 0; 



    in.get(user_input); 
    while(!in.eof() && user_input != ';') 
{ 
    in.get(user_input); 
    source.digits[i] = user_input; 
    ++i; 
} 

    source = bigint(getdata); 

    return in; 
} 

char bigint::operator[](const int i) 
{ 
return digits[i]; 
} 

bigint bigint::operator+(const bigint rhs) 
{ 
    bigint result; 
    int i = 0; 

    for(; i < MAX; ++i) 
    { 

     if((digits[i] + rhs.digits[i]) > 9) 
     { 
      digits[i+1] = digits[i+1] + 1 ; 


     } 

       result.digits[i] = (digits[i] + rhs.digits[i]); 
       result.digits[i] = result.digits[i] % 10; 
    } 

    return result; 




} 

하여 Main.cpp (시험 케이스)

int main() 
{ 
      // Setup fixture 
    bigint left("1"); 
    bigint right("9"); 
    bigint result; 

    // Test 
    result = (left + right); 

    Verify 
    assert(left == "1"); 
    assert(right == "9"); 
    assert(result == "10"); 

} 

프로그램은 어설에서 중단 (결과 == "10");

하지만 어설 션 (결과 == 10)을 제외하고 동일한 테스트 케이스가있는 경우; 프로그램이 실행됩니다.

아무도 말할 수 있습니까?

+0

코드 서식을 지정하는 방법은 무엇입니까? – ObscureRobot

+0

많은 것을 한 번에 테스트하고 있습니다. 비교 연산자, 추가 및 'bigint (char [])'생성자에 대해 별도의 테스트를 수행하십시오. –

답변

3

먼저, 할당 연산자 bigint::operator=(const bigint&)을 구현해야합니다.

지금, operator+, 당신은이 코드에서, 왼쪽 개체의 내용을 변경하고 있습니다 : 좋지 않아

if((digits[i] + rhs.digits[i]) > 9) 
{ 
    digits[i+1] = digits[i+1] + 1 ; 
} 

합니다. 당신은 아마 통과해야하는 위치

bigint x("5"); 
bigint y("6"); 

x+y; 
x+y; 

당신은, 당신은 bigint::operator 인수 에 의해 전달하는 x가 17

다음 인으로 끝낼 것입니다 : 예를 들어,이 코드를 실행 참조 번호 (&).

마지막으로, 여기 들여 적극적으로 악성 :

while(new_digits[null] != '\0') 
    ++null; 
    --null; 

여기에 루프 본문에서 무엇입니까? 맞습니다. 세 번째 줄이 아닙니다. 그런 식으로 코드를 들여 쓰지 마십시오. 새끼 고양이가 울부 짖습니다. 최소한 새끼 고양이 프로그래밍.

NB : 여기서 동적 메모리 할당 코드가 표시되지 않습니다. 이는 digits이 정적 크기의 배열 일 가능성이 있음을 의미합니다. 크기를 초과하면 휴식을 취할 것이라는 점을 알고 있어야합니다.

+0

프로그래밍 용 고양이를 믿지 않는 사람 : http://www.elistmania.com/images/articles/117/Original/LOLCODE.jpg –