2012-03-05 5 views
1

~을 사용하여 'Binary'클래스의 비트를 뒤집어 쓰려고합니다. 이 'Binary'클래스는 '0'및 '1'의 문자 배열을 bs으로 저장합니다. 이 배열 안의 문자를 뒤집기 만하면됩니다.~ (bitwise not) 연산자를 오버로드하는 방법은 무엇입니까?

그러나 코드를 while이나 for에 관계없이 입력 할 수없는 것처럼 보입니다. 어쩌면 내가 잘못 썼을 수도 있습니다. 당신은 == 대신 =으로 값을 할당하려고

#include <iostream> 
#include <windows.h> 

using namespace std; 

TCHAR pressanykey(const TCHAR* prompt = NULL) 
{ 
    TCHAR ch; 
    DWORD mode; 
    DWORD count; 
    HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE); 

    // Prompt the user 
    if (prompt == NULL) 
     prompt = TEXT("Press any key to continue..."); 

    WriteConsole(
     GetStdHandle(STD_OUTPUT_HANDLE), 
     prompt, 
     lstrlen(prompt), 
     &count, 
     NULL 
    ); 

    // Switch to raw mode 
    GetConsoleMode(hstdin, &mode); 
    SetConsoleMode(hstdin, 0); 

    // Wait for the user's response 
    WaitForSingleObject(hstdin, INFINITE); 

    // Read the (single) key pressed 
    ReadConsole(hstdin, &ch, 1, &count, NULL); 

    // Restore the console to its previous state 
    SetConsoleMode(hstdin, mode); 

    // Return the key code 
    return ch; 
} 

class Binary { 
    char *bs; 
    int index; 

    public: 
     Binary(int x) { 
      bs = new char[20]; 
      index = 0; 
      DecimalToBinary(x); 
      bs[index] = '\0'; 
     } 

     Binary(char* str) { 
      bs = str; 
      index = 0; 
      while (bs[index] != '\0') { 
       index += 1; 
      } 
     } 

     Binary(const Binary& original) { 
      bs = original.bs; 
      index = original.index; 
     } 

     ~Binary() { 
      delete [] bs; 
      bs = NULL; 
     } 

     int ToDecimal() { 
      int result = 0; 
      for (int i = 0; i < index; i++) { 
       result *= 2; 
       if (bs[i] == '1') 
        result += 1; 
      } 
      return result; 
     } 

     Binary& operator~() 
     { 
      int i = 0; 
      while(i < index) { 
       if (bs[i] == '1') 
        bs[i] == '0'; 
       else bs[i] == '1'; 
       i++; 
      } 
      return *this; 
     } 

     Binary& operator= (const Binary &b) { 
      delete [] bs; 
      bs = NULL; 

      bs = b.bs; 
      index = b.index; 

      return *this; 
     } 

     void DecimalToBinary(int number) { 
      int remainder; 

      if (number == 1) { 
       bs[index] = '1'; 
       index += 1; 
       return; 
      } 
      if (number == 0) { 
       bs[index] = '0'; 
       index += 1; 
       return; 
      } 

      remainder = number%2; 
      DecimalToBinary(number >> 1); 
      if (remainder == 1) { 
       bs[index] = '1'; 
       index += 1; 
       return; 
      } 
      if (remainder == 0) { 
       bs[index] = '0'; 
       index += 1; 
       return; 
      } 
     } 

     friend istream& operator>>(istream& is, Binary& b); 
     friend ostream& operator<<(ostream& os, Binary& b); 
}; 

istream& operator>>(istream& is, Binary& b) 
{ 
    char *str = new char[20]; 

    is >> str; 

    b.bs = str; 

    b.index = 0; 
    while (b.bs[b.index] != '\0') { 
     b.index += 1; 
    } 

    return is; 
} 

ostream& operator<<(ostream& os, Binary& b) 
{ 
    os << b.bs; 
    return os; 
} 

int main() { 
    Binary a(15); 
    Binary b("10110"); 
    Binary c(b); 

    cout << "binary a is " << a << endl; 
    cout << "binary b is " << b << endl; 
    cout << "binary c is " << c << endl; 

    cout << endl << "Re-enter binary b: "; 
    cin >> b; 

    cout << "binary a is " << a << endl; 
    cout << "binary b is " << b << endl; 
    cout << "binary c is " << c << endl; 

    cout << "binary b in decimal form: " << b.ToDecimal() << endl; 
    cout << "bit flips b: "<< ~b << endl; 

    pressanykey(); 
    return 0; 
} 
+1

문자열 리터럴로 'Binary'를 만들면 UB 인 여러 곳에서'delete [] bs '를 호출하여 문자열 리터럴을'delete []'합니다. 또한'operator ='에서'delete [] bs'를하고'b.bs'를'this-> bs'에 대입하면 두 클래스가 같은 리소스를 관리하게되고'delete []'ed가 두 번됩니다 소멸자에서. –

답변

4

: 여기에 전체 코드입니다.

관련 문제