八进制、十进制、十六进制的转换代码

 #include 
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
  using namespace std;
 int main(int argc, char** argv) {
      
      //声明变量,并初始化
     int a=010,b=10,c=0X10; 
 
     //以十进制形式显示数据
    cout<<"DEC:";
    cout<<" a="<<a;
    cout<<" b="<<b;
    cout<<" c="<endl; 
     
     //以八进制形式显示数据
    cout<<"OCT:";
     cout<//指定八进制输出
     cout<<" a="<<a;
     cout<<" b="<<b;
     cout<<" c="<endl;  
     
    //以十六进制形式显示数据
     cout<<"HEX:";
     cout<//指定十六进制输出
     cout<<" a="<<a;
     cout<<" b="<<b;
    cout<<" c="<endl;  
 
     //八、十和十六进制数混合运算并输出
     cout<<"a+b+c=";
     cout<//恢复十进制输出
     cout<endl;

     //测试八、十和十六进制输入
     cout<<"DEC:a="; cin>>a;
     cout<<"OCT:b="; cin>>b;
    cout<<"HEX:a="; cin>>c;
     cout<<"DEC:"<//指定十进制输出
     cout<<"a="<endl;
    cout<<"b="<endl;
    cout<<"c="<endl;
    return 0;
 }

 

你可能感兴趣的