網路教學(e-Learning)校園社群(e-Community)服務台(e-Service)系所班網(e-Class)登入
位置: 高鈺婷 > C++
C++ 十進位轉二進位程式碼
by 高鈺婷 2014-03-17 16:14:46, 回應(0), 人氣(8238)
#include <cstdlib>
#include <iostream>

using namespace std;
int tenToTwo(int n);
int TenToTwo2(int n);
int main(int argc, char *argv[])
{
    string intlist;
//4a0k0029高鈺婷
   int again = 0;
   
   do{
       int question;
       cout<<"請輸入要從十進位轉成二進位的數字: ";
       cin>>question;
       cout<<tenToTwo(question)<<endl;
       cout<<"--------"<<endl;
       TenToTwo2(question);
       cout<<"請問是否要繼續,要的話請輸入1,否則為0: ";
       cin >>again;
   }while(again == 1);
    
        
    exit(0);
    //system("PAUSE");
    return EXIT_SUCCESS;
}

int tenToTwo(int n)
{
    int total = 0,m = 1;
    while(n!=0)
    {
       total = total+(n%2)*m;
       n= n/2;
       m = m*10;
    }
    return total;
}
//------------------

int TenToTwo2(int n)
{
    if(n != 0)
    {
         TenToTwo2(n/2);
         cout << n%2;
    }
    
}