2013-03-08 2 views
0

1 비트의 각도 만 저장하는 숙제에 대한 이진 연결 목록을 만듭니다. 가장 높은 학위를 얻을 수 있고 바이너리 목록의 어느 위치에 비트를 설정하고 특정 비트에서 어떤 비트가 반환되는지 알 수 있습니다. 그러나 어떤 이유로 복사 생성자와 할당 연산자 (=)를 만드는 데 가장 어려움을 겪고 있습니다. 여기에 내가 가진 코드가 있습니다 :복사 생성자 및 할당 연산자 문제

// copy constructor 
// creates a new linked list where the contents are a deep copy of the provided list 
Binary::Binary(const Binary &b) 
{ 
    Binary clone; 
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     clone.set_bit(1, current_other->degree); 
    } 
} 

// assignment operator 
// sets the current link list to be a deep copy of the provided list. 
// make sure to check if assigning to itself, and make sure to free old memory 
// before making the copy. 
Binary& Binary::operator=(const Binary &other) 
{ 
    Binary clone; 
    for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     clone.set_bit(1, current_other->degree); 
    } 
    return clone; 
} 

내 논리에 결함이 있습니까? 누군가 제발 도와주세요!

P. 나는 set_bit (b, d)와 다른 메소드를 너무 많이 테스트했으며, "Binary b3 (b2)"또는 "Binary b3 = b2"를 시도 할 때 프로그램이 멈추기 때문에 이것들이 엉망인 것을 알고있다. 가리킨 "할당 1. 1.exe 0x00DC4B18 처리되지 않은 예외 : 0xC0000005 : 액세스 위반 읽기 위치 0xCCCCCCD0"말한다.

편집 : 나뿐만 아니라 기본 생성자가 : 진() {firstTerm = nullptr;}

편집 편집 :

출력 :

TESTING DEFAULT CONSTRUCTOR 
    The binary number b1 is empty. 

    TESTING GET AND SET METHODS 
    The highest bit of binary number b1 is 5. 
    The bit of binary number b1 at degree 5 is 1. 
    The bit of binary number b1 at degree 2 is 0. 
    The bit of binary number b1 at degree 1 is 0. 

    TESTING PARAMETER CONSTRUCTOR 
    The bit of binary number b1 at degree 2 is 1. 
    The bit of binary number b1 at degree 0 is 1. 
    The bit of binary number b1 at degree 1 is 0. 

    TESTING COPY CONSTRUCTOR 
    B2 = 101 
    B3 = _ 

Unhandled exception at 0x00C04B18 in Assignment 1.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0. 

테스터 코드 :

#include <iostream> 
#include "binary.h" 

using namespace std; 

int main (void) 
{ 
    // test default constructor 
    Binary b1; 
    cout << "TESTING DEFAULT CONSTRUCTOR" << endl; 
    if (b1.get_degree() == -1) 
     cout << "\tThe binary number b1 is empty." << endl; 
    else 
     cout << "\tThe binary number b1 is NOT empty. (INCORRECT)" << endl; 

    // test get_bit, set_bit, and get_degree 
    cout << "\nTESTING GET AND SET METHODS" << endl; 
    b1.set_bit(1, 2); 
    b1.set_bit(1, 5); 
    b1.set_bit(1, 0); 
    b1.set_bit(0, 2); 
    if (b1.get_degree() == 5) 
     cout << "\tThe highest bit of binary number b1 is 5." << endl; 
    else 
     cout << "\tThe highest bit of binary number b1 is NOT 5. (INCORRECT)" << endl; 
    if (b1.get_bit(5) == 1) 
     cout << "\tThe bit of binary number b1 at degree 5 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b1 at degree 5 is 0. (INCORRECT)" << endl; 
    if (b1.get_bit(2) == 0) 
     cout << "\tThe bit of binary number b1 at degree 2 is 0." << endl; 
    else 
     cout << "\tThe bit of binary number b1 at degree 2 is 1. (INCORRECT)" << endl; 
    if (b1.get_bit(1) == 0) 
     cout << "\tThe bit of binary number b1 at degree 1 is 0." << endl; 
    else 
     cout << "\tThe bit of binary number b1 at degree 1 is 1. (INCORRECT)" << endl; 

    // test parameter constructor 
    cout << "\nTESTING PARAMETER CONSTRUCTOR" << endl; 
    Binary b2(5); 

    if (b2.get_bit(2) == 1) 
     cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl; 
    if (b2.get_bit(0) == 1) 
     cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl; 
    if (b2.get_bit(1) == 0) 
     cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl; 

    // test copy constructor 
    cout << "\nTESTING COPY CONSTRUCTOR" << endl; 
    cout << "B2= " << b2 << endl; 
    b2.set_bit(1,1); 
    Binary b3(b2); 
    cout << "B3= " << b3 << endl; 
    b2.set_bit(1, 1); 
    cout << "B2= " << b2 << endl; 
    cout << "B3= " << b3 << endl; 
    if (b3.get_bit(2) == 1) 
     cout << "\tThe bit of binary number b3 at degree 2 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b3 at degree 2 is 0. (INCORRECT)" << endl; 
    if (b3.get_bit(0) == 1) 
     cout << "\tThe bit of binary number b3 at degree 0 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b3 at degree 0 is 0. (INCORRECT)" << endl; 
    if (b3.get_bit(1) == 0) 
     cout << "\tThe bit of binary number b3 at degree 1 is 0." << endl; 
    else 
     cout << "\tThe bit of binary number b3 at degree 1 is 1. (INCORRECT)" << endl; 

    // test assignment operator 
    cout << "\nTESTING ASSIGNMENT OPERATOR" << endl; 
    b2 = b3; 
    b3.set_bit(1, 1); 

    if (b2.get_bit(2) == 1) 
     cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl; 
    if (b2.get_bit(0) == 1) 
     cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl; 
    if (b2.get_bit(1) == 0) 
     cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl; 
    else 
     cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl; 

    // test convert 
    cout << "\nTESTING CONVERT METHOD" << endl; 
    if (b1.convert() == 33) 
     cout << "\tThe decimal value of binary number b1 is 33." << endl; 
    else 
     cout << "\tThe decimal value of binary number b1 is NOT 33. (INCORRECT)" << endl; 

    // test output operator 
    cout << "\nTESTING OUTPUT OPERATOR" << endl; 
    cout << "\tThe binary number b1 is " << b1 << endl; 
    cout << "\tThe number b1 should be 100001" << endl; 

    // test addition 
    cout << "\nTESTING ADDITION OPERATOR" << endl; 
    Binary b4 = b2 + b3; 

    if (b4.convert() == 12) 
     cout << "\t101 + 111 = 1100." << endl; 
    else 
     cout << "\t101 + 111 != 1100. (INCORRECT)" << endl; 

    // test subtraction 
    cout << "\nTESTING SUBTRACTION OPERATOR" << endl; 
    Binary b5(b1 - b2); 

    if (b5.convert() == 28) 
     cout << "\t100001 - 101 = 11100." << endl; 
    else 
     cout << "\t100001 - 101 != 11100. (INCORRECT)" << endl; 

    // test multiplication 
    cout << "\nTESTING MULTIPLICATION OPERATOR" << endl; 
    Binary b6 = b3 * b2; 

    if (b6.convert() == 35) 
     cout << "\t111 * 101 = 100011." << endl; 
    else 
     cout << "\t111 * 101 != 100011. (INCORRECT)" << endl; 

    system("pause"); 
} 

binary.h :

#ifndef _BINARY_H_ 
#define _BINARY_H_ 

#include <iostream> 

class Binary { 
private: 
    struct BinaryNode { 
     int degree; 
     BinaryNode* next; 
     BinaryNode(int d, BinaryNode* n): degree(d),next(n) {} 
    }; 
    BinaryNode *firstTerm; 

public: 
    // default constructor 
    Binary() {firstTerm = nullptr;} 

    // constructor 
    // takes a value representing a decimal number and creates 
    // the binary linked list representation of it. 
    Binary(int x); 

    // sets the term with degree d and bit b 
    // notice a node is created if bit is 1 AND a node 
    // for that degree doesn't exist, or the node is removed 
    // if the bit is 0 AND the node with that degree already exists 
    void set_bit(int b, int d); 

    // returns one if a term with degree d exists, zero otherwise 
    int get_bit(int d) const; 

    // returns the decimal integer representation of the binary number. 
    int convert() const ; 

    // returns the highest degree of any term in the binary number 
    // returns -1 if the the list is empty. 
    int get_degree() const; 

    // destructor 
    // make sure that all memory is returned (freed up) correctly 
    ~Binary(); 

    // copy constructor 
    // creates a new linked list where the contents are a deep copy of the provided list 
    Binary(const Binary &b); 

    // assignment operator 
    // sets the current link list to be a deep copy of the provided list. 
    // make sure to check if assigning to itself, and make sure to free old memory 
    // before making the copy. 
    Binary& operator=(const Binary &other); 

    // prints the binary number to the output stream o 
    // please use like:  10001101 
    // terms must be printed in descending order of degree 
    friend std::ostream& operator<<(std::ostream &o, const Binary &b); 

    // returns a new binary number representing the addition of 2 provided binary numbers. 
    // do NOT simply convert the numbers to decimal using convert(),add them, 
    // then convert back to binary. 
    friend Binary operator+(const Binary &b1, const Binary &b2); 

    // returns a new binary number representing the subtraction 
    // of 2 provided binary numbers. can assume b1 will always be 
    // larger than b2. 
    // do NOT simply convert the numbers to decimal using convert(),subtract them, 
    // then convert back to binary. 
    friend Binary operator-(const Binary &b1, const Binary &b2); 

    // returns a new binary number representing the multiplication 
    // of 2 provided binary numbers. 
    // do NOT simply convert the numbers to decimal using convert(),multiply them, 
    // then convert back to binary. 
    friend Binary operator*(const Binary &b1, const Binary &b2); 

}; 

std::ostream& operator<<(std::ostream &o, const Binary &b); 

Binary operator+(const Binary &b1, const Binary &b2); 
Binary operator-(const Binary &b1, const Binary &b2); 
Binary operator*(const Binary &b1, const Binary &b2); 

#endif 

binary.cpp :

#include "binary.h" 
using namespace std; 

// constructor 
// takes a value representing a decimal number and creates 
// the binary linked list representation of it. 
Binary::Binary(int x) 
{ 
    firstTerm = nullptr; 
    int deg = 0; 
    int n = x; 
    while (n != 0) 
    { 
     set_bit(n%2, deg); 
     n = n/2; 
     ++deg; 
    } 
} 

// sets the term with degree d and bit b 
// notice a node is created if bit is 1 AND a node 
// for that degree doesn't exist, or the node is removed 
// if the bit is 0 AND the node with that degree already exists 
void Binary::set_bit(int b, int d) 
{ 
    if (b == 1) 
    { 
     if (firstTerm == nullptr || d == 0) 
      { 
       firstTerm = new BinaryNode(d, firstTerm); 
      } 

     else 
      { 
       BinaryNode *current, *prev = firstTerm; 
       for(current = firstTerm; current != nullptr; current = current->next) 
       { 
        if (current->next == nullptr) 
        { 
         current->next = new BinaryNode(d, nullptr); 
         break; 
        } 
        else if (current->degree == d) 
        { 
         prev->next = new BinaryNode (d, current->next); 
         delete current; 
         break; 
        } 
        else if(current->degree > d) 
        { 
         prev->next = new BinaryNode (d, current); 
         break; 
        } 
        prev = current; 
       } 
      } 
    } 
    else 
    { 
     BinaryNode *current, *prev = firstTerm; 
     for(current = firstTerm; current != nullptr; current = current->next) 
     { 
      if (current->degree == d) 
      { 
       prev->next = current->next; 
       delete current; 
       break; 
      } 
      prev = current; 
     } 
    } 
} 

// returns one if a term with degree d exists, zero otherwise 
int Binary::get_bit(int d) const 
{ 
    for (BinaryNode *current = firstTerm; current != nullptr; current = current->next) 
    { 
     if (current == nullptr) 
      break; 
     if (current->degree == d) 
      return 1; 
    } 
    return 0; 
} 

// returns the decimal integer representation of the binary number. 
int Binary::convert() const 
{ 
    int sum = 0; 
    for (BinaryNode* current = firstTerm; current != nullptr; current = current->next) 
    { 
     sum = sum + (int)pow(2,current->degree); 
    } 
    return sum; 
} 

// returns the highest degree of any term in the binary number 
// returns -1 if the the list is empty. 
int Binary::get_degree() const 
{ 
    if (firstTerm == nullptr) 
     {return -1;} 
    else 
    { 
     BinaryNode *current; 
     for (current = firstTerm; current->next != nullptr; current = current->next); 

     return current->degree; 
    } 
} 

// destructor 
// make sure that all memory is returned (freed up) correctly 
Binary::~Binary() 
{ 
    BinaryNode* tmp; 
    for(BinaryNode* current = firstTerm; current != nullptr; current = tmp) 
    { 
      tmp = current->next; 
      delete current; 
    } 
} 

// copy constructor 
// creates a new linked list where the contents are a deep copy of the provided list 
Binary::Binary(const Binary &b) 
{ 
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     set_bit(1, current_other->degree); 
    } 
} 

// assignment operator 
// sets the current link list to be a deep copy of the provided list. 
// make sure to check if assigning to itself, and make sure to free old memory 
// before making the copy. 
Binary& Binary::operator=(const Binary &other) 
{ 
    Binary clone; 
    for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     clone.set_bit(1, current_other->degree); 
    } 
    return clone; 
} 

// prints the binary number to the output stream o 
// please use like:  10001101 
// terms must be printed in descending order of degree 
std::ostream& operator<<(std::ostream &o, const Binary &b) 
{ 
    for(int i = b.get_degree(); i >= 0; --i) 
    { 
     o << b.get_bit(i); 
    } 
    return o; 
} 

// returns a new binary number representing the addition of 2 provided binary numbers. 
// do NOT simply convert the numbers to decimal using convert(),add them, 
// then convert back to binary. 
Binary operator+(const Binary &b1, const Binary &b2) 
{ 
    int l = b1.get_degree(); 
    if (b1.get_degree() < b2.get_degree()) 
    { 
     l = b2.get_degree(); 
    } 
    int i, c = 0; 
    Binary sum; 
    for (i = 0; i <= l; ++i) 
    { 
     sum.set_bit(((b1.get_bit(i)^b2.get_bit(i))^c), i); //get sum (A XOR B XOR C) 
     c = ((b1.get_bit(i) & b2.get_bit(i)) | (b1.get_bit(i) &c)) | (b2.get_bit(i) & c); //get carry bit (AB + BC + CA) 
    } 
    sum.set_bit(c, i); 
    return sum; 
} 

// returns a new binary number representing the subtraction 
// of 2 provided binary numbers. can assume b1 will always be 
// larger than b2. 
// do NOT simply convert the numbers to decimal using convert(),subtract them, 
// then convert back to binary. 
Binary operator-(const Binary &b1, const Binary &b2) 
{ 
    Binary one = Binary(1); 
    Binary inv, two, result, fresult; 
    int i, l = b2.get_degree() + 1; 
    for(i = 0; i <= l; ++i) 
    { 
     if (b2.get_bit(i) == 1) 
      inv.set_bit(0,i); 
     else 
      inv.set_bit(1,i); 
    } 
    two = inv + one; 
    result = two + b1; 
    if (b1.get_degree() > l) 
    { 
     l = b1.get_degree(); 
    } 
    for (l; l >= 0; l--) 
    { 
     fresult.set_bit(result.get_bit(l), l); 
    } 
    return (fresult); 
} 

// returns a new binary number representing the multiplication 
// of 2 provided binary numbers. 
// do NOT simply convert the numbers to decimal using convert(),multiply them, 
// then convert back to binary. 
Binary operator*(const Binary &b1, const Binary &b2) 
{ 
    Binary prod = b1; 
    for (int i = 1; i < b2.convert(); ++i) 
    { 
     prod = prod + b1; 
    } 
    return prod; 
} 
+0

나는이 연산자를 믿는다. 새로운 이진 코드를 만들고 복제하는 대신이 포인터를 사용한다. –

+0

기본 생성자를 정의 했습니까? (인수를 취하지 않는 생성자) – Roberto

+0

당신을 돕기 위해 완전한 클래스가 필요합니다. – Roberto

답변

1

나는 그것을 알아 냈습니다. 모두 도와 주셔서 감사합니다. 나는 내 문제를 알아 냈다. 복사 생성자에서 누군가 지적했듯이 다른 생성자에서와 마찬가지로 아직 첫 번째 저작을 선언하지 않았습니다. 내가 필요한 경우, 먼저 현재 목록을 파괴한다, 또는 그 자체에 복사되지 않은 것을 확인되지 않은, 할당 연산자에서

Binary::Binary(const Binary &b) 
{ 
    firstTerm = nullptr; //construct firstTerm 
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    //set a node pointer = to b's firstTerm then go through b's list, setting each bit and degree to the new list 
    { 
     set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree 
    } 
} 

: 마지막 코드로 끝났다. 또한, 나는 잘못된 위치에 &을 가졌습니다. 최종 코드는 다음과 같이 끝났다 :

Binary &Binary::operator=(const Binary &other) 
{ 
    if(this != &other) //make sure it isn't copying to itself 
    { 
     if (this->get_degree() != -1) //if the Binary list isn't empty, destruct it 
     { 
      this->~Binary(); 
     } 
     firstTerm = nullptr; //construct firstTerm 
     for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
     //set a node pointer = to other's firstTerm then go through other's list, setting each bit and degree to the new list 
     { 
      set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree 
     } 

     return *this; 
    } 
} 
3
Binary::Binary(const Binary &b) 
{ 
    Binary clone; 
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     clone.set_bit(1, current_other->degree); 
    } 
} 

당신은 객체를 생성은 clone라고 그 비트를 설정하고 그것을 멀리 던져. 그건 맞지 않아. 아마도 당신은 의미 : 당신이 생성자를 정의로

Binary::Binary(const Binary &b) 
{ 
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    { 
     set_bit(1, current_other->degree); 
    } 
} 
+0

나는 그것을 시도했지만 어느 쪽도 작동하지 않는다 ... –

+0

"작동하지 않는다"는 유용한 문제 설명이 아니다. 그것은 컴파일합니까? 실행됩니까? 어떤 결과를 기대 했습니까? 너는 무엇을 얻었 느냐? (당신은 또한'operator ='와 비슷한 문제를 가지고 있습니다 - 호출 된 객체를 수정하지 않습니다. 이것은 보통 그러한 연산자가하는 일입니다.) –

+0

미안 해요, 일하기 위해 서둘러 왔어요. 그것은 내가 다른 방향으로 가지고있을 때와 같은 오류를 준다. 프로그램이 컴파일되지만 복사 된 바이너리 목록을 가지고 무엇을하려고 할 때 원본 게시물에 게시 한 "액세스 위반 읽기"오류가 발생합니다. 예제에서는 main()에 다음과 같이 표시했습니다. 이진 b3 (b2); cout << b3 << endl; 이 시점에서 출력에서는 중단되지만 모든 것은 미리 작동합니다. 위의 코드 전체를 게시합니다. –

0

A는 곧, 컴파일러는 당신을위한 기본 생성자를 생성하지 않습니다, 이것은 당신이 Binary::Binary() 생성자를 정의하지 않은 경우 Binary clone;을 할 수 없음을 의미합니다.

+0

기본 생성자도 있습니다 : Binary() {firstTerm = nullptr;} –

관련 문제