#include#include
#include using namespace std; int main(int agrc, char *agrv[]) { int iInPut = 0; while (cin >> iInPut) { string sBinary;//轉換後的二進制存儲為字符串,調用了默認構造函數初試化為空串 int temp = abs(iInPut); if (temp == 0) { //cout.width(11);//以11位的寬度右對齊輸出 cout << " 0-->0\n"; continue; } while (temp) { if (temp & 0x01) { sBinary += '1'; } else { sBinary += '0'; } temp >>= 1;//對正數右移,高位補0 } reverse(sBinary.begin(), sBinary.end()); const char *cOutPut = sBinary.c_str(); cout.width(11); cout << iInPut << (iInPut > 0 ? "-->" : "-->-") << cOutPut << endl; } return 0; }