因此,代碼是指一個十進制數轉換爲八進制,我已經超負荷運營商< <給我的輸出八進制轉換爲十進制的轉換(調試)
但是轉換不會發生,我也得到一個奇怪的輸出
這裏是我的代碼
#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你的函數沒有返回值,你應該收到一個編譯器的警告。第二你期望輸出什麼?整數是整數。八進制,十六進制,二進制和十進制是文本表示。您應該檢查[C++標準I/O操縱器](http://en.cppreference.com/w/cpp/io/manip/hex)如何在這些表示中輸出或輸入數字 –