게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c++ 코드 문제점 질문하겠습니다..
게시물ID : programmer_20391짧은주소 복사하기
작성자 : 고아리
추천 : 0
조회수 : 781회
댓글수 : 8개
등록시간 : 2017/04/25 01:35:16
옵션
  • 본인삭제금지
정수의 집합을 위한 Set 클래스를 작성하시오.
A. 두 개의 private 멤버 변수
 int * m_pSet : 정수의 배열을 동적으로 할당하기 위한 포인터
 int m_nCount : 집합의 크기를 나타냄
B. 메소드
 Set 기본 생성자 : 비어있는 집합을 생성
 Set 복사 생성자 : 전달받은 객체를 이용하여 멤버 변수 초기화
 ~Set 소멸자 : pSet에 동적 할당된 메모리를 반환(delete 키워드 사용
 Add( int ) : 집합에 원소를 추가. 중복된 값이 있는지 검사하여 중복된 값이 있다면 false를 반환하고, 중복된 값이 없으면 true를 반환
 bool Remove( int ) : 집합의 원소를 제거. 원소가 존재하여 제거 되었다면, true를 반환하고, 해당 원소가 없으면 false를 반환
 int GetCount() : 집합 원소의 수를 반환
 void Print() : 집합의 원소들을 화면에 출력. ex) 1 2 4 8 
 bool IsMember( int ) : 파라미터로 전달한 멤버가 원소에 존재하면 true, 존재하지 않으면 false를 반환
 int GetAt( int ) : 파라미터로 전달받은 인덱스에 있는 원소를 반환하는 함수
C. 연산자
 + 연산자 : 두 집합의 합집합을 반환
 - 연산자 : 두 집합의 차집합을 반환
 * 연산자 : 두 집합의 교집합을 반환
 = 연산자 : 우측 집합 객체를 좌측 집합 객체로 대입
 friend 함수를 이용하여 +=, -=, *= 연산자를 구현
 add(), remove() 함수 사용시 pSet 멤버 변수에 할당되는 동적 메모리는 원소의 개수에 맞도록 조정되어야 함. 

이 문제에 대한 코드를 짜보았는데요, 코드가 잘 돌아가지 않습니다.. 어디가 문제인지 모르겠습니다ㅠㅠ



#include
using namespace std;

class Set
{
private:
    int* m_pSet;
    int m_nCount;

public:
    Set();
    
    Set(const Set& data);
    
    
    bool Add(int data);
    bool Remove(int data);
    int GetCount();
    void print();
    bool IsMember(int search);
    int GetAt(int index);
    
    Set operator +(Set B);
    Set operator -(Set B);
    Set operator *(Set B);
    Set operator =(Set another);
    
    friend Set operator += (Set one, Set another);
    friend Set operator -= (Set one, Set another);
    friend Set operator *= (Set one, Set another);
    
};

bool Set::Add(int data) {
    
    for(int c = 0; c
        if(*(m_pSet+c) == data) {
            return false;
        }
    }
    
    int *tmp = new int[m_nCount];
    for(int c = 0; c
        tmp[c] = *(m_pSet+c);
    }
    
    delete m_pSet;
    m_pSet = new int [m_nCount+1];
    
    for(int c = 0; c
        *(m_pSet+c) = tmp[c];
    }
    *(m_pSet+m_nCount) = data;
    
    m_nCount++;
    
    return true;
}

bool Set::Remove(int data) {
    int index;
    if(IsMember(data) == false)
        return false;
    
    for(int c = 0; c
        if(*(m_pSet+c) == data) {
            index = c;
            break;
        }
    }
   
    int *tmp = new int [m_nCount];
    for(int c = 0; c
        tmp[c] = *(m_pSet+c);
    }
    
    delete [] m_pSet;
    
    m_pSet = new int [m_nCount-1];
    
    for(int c = 0; c
        *(m_pSet+c) = tmp[c];
    }
    for(int c = index+1; c<(m_nCount-1); c++) {
        *(m_pSet+c) = tmp[c];
    }
    
    m_nCount--;
    return true;
}

int Set::GetCount() {
    return m_nCount;
}

void Set::print() {
    cout << "OUTPUT : ";
    for(int c = 0; c
        cout << *(m_pSet+c) << " ";
    }
    cout << endl;
}

bool Set::IsMember(int search) {
    
    for(int c = 0; c
        if(*(m_pSet+c) == search) {
            return true;
        }
    }
    
    return false;
}

int Set::GetAt(int index) {
    return *(m_pSet+index);
}

Set Set::operator+(Set B) {
    
    Set A = *(this);
    Set G(A*B);
    
    Set result = A;
    
    B -= G;
    
    for(int c = 0; c
        result.Add(B.m_pSet[c]);
    }
    
    return result;
}

Set Set::operator-(Set B) {
    
    Set A = *(this);
    
    Set G = A*B;
    
    for(int c = 0; c < G.m_nCount; c++) {
        if(A.Remove(G.m_pSet[c]))
            A.m_nCount--;
    }
    
    Set result;
    
    result.m_pSet = new int [A.m_nCount];
    
    for(int c = 0; c < A.m_nCount; c++) {
        result.Add(m_pSet[c]);
    }
    
    result.m_nCount = A.m_nCount;
    
    return Set(result);
}

Set Set::operator*(Set B) {
    
    int min;
    int* resultList = nullptr;
    

    if(this->m_nCount > B.m_nCount) {
        min = B.m_nCount;
    }

    else {
        min = this->m_nCount;
    }
    
    int *G = new int[min];
    int Glength = 0;
    
    for(int c = 0; cm_nCount; c++) {
        if(B.Remove(this->m_pSet[c])) {
            G[Glength] = this->m_pSet[c];
            Glength++;
        }
    }
    
    if(Glength) {
        resultList = new int [Glength];
        for(int c = 0; c
            resultList[c] = G[c];
    }
    
    Set result;
    
    result.m_nCount = Glength;
    result.m_pSet = resultList;
    
    return Set(result);
    
}

Set Set::operator=(Set another) {
    m_pSet = another.m_pSet;
    m_nCount = another.m_nCount;
    return *(this);
}

Set operator +=(Set one, Set another) {
    Set result = one + another;
    return Set(result);
}

Set operator-=(Set one, Set another) {
    
    Set result = one - another;
    return result;
    
}

Set operator*=(Set one, Set another) {
    
    Set result = one * another;
    return Set(result);
}

Set::Set() {
    m_pSet = nullptr;
    m_nCount = 0;
}

Set::Set(const Set &data) {
    m_pSet = data.m_pSet; // Again. 오류 예상 1
    m_nCount = data.m_nCount; // Again. 오류 예상 2
}

int main () {
    
    Set A;
    A.Add(1);
    A.Add(2);
    A.Add(4);
    A.Add(9);
    A.print();
    
    Set B;
    B.Add(3);
    B.Add(2);
    B.Add(7);
    B.Add(8);
    B.print();
    Set C;
    C = A+B;
    C.print();
   
    Set D;
    D = A-B;
    D.print();
   
    Set E;
    E = A*B;
    E.print();
    Set F(A) ;
    F += B;
    F.print();

    Set G(A) ;
    G -= B;
    G.print();

    Set H(A) ;
    H *= B;
    H.print();

    return 0;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호