📄 2_16任意进制转换.cpp
字号:
/*********************************************************************
**** ****
**** 王平 2002计算机科学与技术专业 19# ****
**** ****
****数字转换:你可以给定一个位数不超过1000位的源数,程序按你给定的 ****
**** 进制给出相应的目的进制数 ****
**** 2004年10月23日 ****
*********************************************************************/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define Hexnum 16
#define MAXSIZE 1000
int baseS;
void multy(int& m){
m*=baseS;
// cout<<m<<'\t';
}
void print(int& m){
if(m==15) cout<<"F";
else if(m==14) cout<<"E";
else if(m==13) cout<<"D";
else if(m==12) cout<<"C";
else if(m==11) cout<<"B";
else if(m==10) cout<<"A";
else cout<<m;
}
int main(){
char ch;
int baseD,cur,remainder,i,num[MAXSIZE],length,k;
vector<int> numD;
for(i=0;i<MAXSIZE;i++) num[i]=0;
cout<<"Please inut the source number's base and destination's :"<<endl;
cin>>baseS>>baseD;
cout<<"Please inut the source number ended with &:"
<<"\nNotation: if the number you input is greater than 10"
<<"\nA~F(sensitive) for 10~15 is nessessaryly."<<endl;
length=0;
while(cin>>ch){
if(ch=='&') break;
else {
if(ch>='A'){num[++length]=static_cast<int>(ch-'7');}
else {num[++length]=static_cast<int>(ch-'0');}
}
}
cout<<"\nThe destnation number witch based on "<<baseS<<" is: "<<endl;
for_each(num+1,num+length+1,print);
// convert to hex!
reverse(num+1,num+length+1);
for(int j=length;j>1;j--){
for_each(num+j,num+length+1,multy);
for(int i=j-1;i<length+1;i++){
num[i]+=num[i+1]%Hexnum;
num[i+1]/=Hexnum;
}
}
reverse(num+1,num+length+1);
// convert to destnation!
j=k=1;
while(k<length+1){
remainder=0;
while(k<length+1){
cur=(remainder*16+num[k]);
num[k]=cur/baseD;
remainder=cur%baseD;
k++;
}
numD.insert(numD.end(),remainder);
k=j;
while(num[k]==0) k++;
j=k;
}
reverse(numD.begin(),numD.end());
cout<<"\nThe destnation number witch based on "<<baseD<<" is: "<<endl;
for_each(numD.begin(),numD.end(),print);
cout<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -