2012-03-05 76 views
1

我试图用~来翻转'Binary'类的位。这个'Binary'类存储一个'0'和'1'的字符数组,名称为bs。我只想翻转这个数组中的字符:如何重载〜(按位不)运算符?

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

但是,无论我使用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'来删除字符串文字。另外,在'operator ='中,你是'delete [] bs',然后将'b.bs'分配给'this-> bs',这使得两个类管理相同的资源,并且两次delete []'ed在析构函数中。 – 2012-03-05 03:07:11

回答

4

您正在尝试的价值与==而不是=分配。