2016-05-01 83 views
-1

因此,代碼是指一個十進制數轉換爲八進制,我已經超負荷運營商< <給我的輸出八進制轉換爲十進制的轉換(調試)

但是轉換不會發生,我也得到一個奇怪的輸出

這裏是我的代碼

#include<iostream> 
#include<math.h> 
using namespace std; 
class OCTAL 
{ 
    int octnum; 
    public: int dec_oct(int); 
      int oct_dec(int); 
      OCTAL(int x) 
      { 

       octnum=dec_oct(x); 
      } 
      int operator+(int k) 
      { 
       return (oct_dec(octnum)+k); 
      } 
      friend ostream& operator<<(ostream&,OCTAL); 
}; 
int OCTAL::dec_oct(int x) 
{ 

    int i=0,sum=0; 
    while(x!=0) 
    { 
     sum+=((x%8))+(pow(10,i)); 
     x=x/8; 
     i++; 
    } 
    return sum; 
} 

int OCTAL::oct_dec(int x) 
{ 

    int i=0,sum=0; 
    while(x!=0) 
    { 
     sum+=(x%10)+pow(8,i); 
     x=x/10; 
     i++; 
    } 
    return sum; 
} 

ostream& operator<<(ostream& ps,OCTAL obj) 
{ 
    cout<<obj.octnum<<endl; 
    return ps; 
} 

int main() 
{ 

    int x,k; 
    cout<<"Enter decimal N\n"; 
    cin>>x; 
    OCTAL n(x); 
    cout<<"Octal + object ="<<n<<endl; 
    cout<<"Enter base 10 No to be added\n"; 
    cin>>k; 
    int y=(n+k); 
    cout<<"Sum = "<<y<<endl; 
} 

    #include<math.h> 
    using namespace std; 
    class OCTAL 
    { 
     int octnum; 
     public: int dec_oct(int); 
       int oct_dec(int); 
       OCTAL(int x) 
       { 

        octnum=dec_oct(x); 
       } 
       int operator+(int k) 
       { 
        return (oct_dec(octnum)+k); 
       } 
       friend ostream& operator<<(ostream&,OCTAL); 
    }; 
    int OCTAL::dec_oct(int x) 
    { 

     int i=0,sum=0; 
     while(x!=0) 
     { 
      sum+=((x%8))+(pow(10,i)); 
      x=x/8; 
      i++; 
     } 

    } 

    int OCTAL::oct_dec(int x) 
    { 

     int i=0,sum=0; 
     while(x!=0) 
     { 
      sum+=(x%10)+pow(8,i); 
      x=x/10; 
      i++; 
     } 
    } 

    ostream& operator<<(ostream& ps,OCTAL obj) 
    { 
     cout<<obj.octnum<<endl; 
     return ps; 
    } 

    int main() 
    { 

     int x,k; 
     cout<<"Enter decimal No\n"; 
     cin>>x; 
     OCTAL n(x); 
     cout<<"Octal + object ="<<n<<endl; 
     cout<<"Enter base 10 No to be added\n"; 
     cin>>k; 
     int y=(n+k); 
     cout<<"Sum = "<<y<<endl; 
    } 

輸出 輸入十進制數轉換
八路+對象= 14

輸入基部10沒有被添加
薩姆= 29

+1

1你的函數沒有返回值,你應該收到一個編譯器的警告。第二你期望輸出什麼?整數是整數。八進制,十六進制,二進制和十進制是文本表示。您應該檢查[C++標準I/O操縱器](http://en.cppreference.com/w/cpp/io/manip/hex)如何在這些表示中輸出或輸入數字 –

回答

0

八進制和10進制整數值的外部表示。在內部,價值只是一個價值;它不是八進制的,它不是十進制的,也不是紅色的。將八進制輸入轉換爲整數值表示將輸入的文本轉換爲內部值;將值轉換爲八進制意味着生成代表內部值的文本字符串。同樣的事情爲十進制。因此,當您的任務是將八進制轉換爲十進制時,您需要從文本字符串開始,並以文本字符串結尾,通常會轉換爲整數值,如中間表示形式。所以八進制 - >整數 - >小數,實現爲文本 - >整數 - >文本。

0
sum+=(x%10)+pow(8,i); 

想象x是9.你得到9%10 = 9 + 8^0 = 9 + 1 = 10.

我猜這不是你想要的。你可能想要11--實際上你應該把值作爲一個字符串返回,儘管如此。

問題是,當你實際上不能處理超過8個數據時,你一次只能處理10個可能的值(0-9)。理想情況下,你將每次迭代時將x移位3位,這樣你可以始終代表你正在處理的價值。

x & 7會給你前3位的值,那麼你可以將x移動3 x>>3來準備接下來的3位來讀取。

這裏的東西,讓你開始:

int main() { 
    int x = 44; 
    while(x > 0) { 
     auto v = x&7; 
     cout<<v<<endl; 
     x=x>>3; 
    } 
} 

http://melpon.org/wandbox/permlink/50qDr48F9oyd5t4z