进制转换修订版!!!(点个赞吧!!!)

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
int RtoTen(int R,string s){
	int len=s.size();
	int ans=0;
	for(int i=0;i<len;i++){
		if(s[i]>='0'&&s[i]<='9'){
			ans+=(s[i]-'0')*pow(R,len-i-1);
		}else{
			ans+=(s[i]-'A'+10)*pow(R,len-i-1);
		}
	}
	return ans;
}
string TentoR(int R,int n){
	string ans="";
	while(n){
		int yu=n%R;
		if(yu<10){
			ans=char(yu+'0')+ans;
		}else{
			ans=char(yu-10+'A')+ans;
		}
		n/=R;
	}
	return ans;
}
int main()
{
	int n,m;
	string str;
	cout<<"请输入待转换的进制(一个数字,如二进制就写2):";
	cin>>n;
	cout<<endl;
	cout<<"请输入带转换的数(一个数字):";
	cin>>str;
	cout<<endl;
	cout<<"请输入需要转换成数的进制(一个数字如二进制就写2):";
	cin>>m;
	cout<<endl;
	cout<<"转换中请稍后…";
	Sleep(500);
	cout<<"…";
	Sleep(500);
	cout<<"…";
	Sleep(500);
	cout<<"…";
	Sleep(500);
	cout<<"…";
	Sleep(500);
	cout<<"…";
	Sleep(500);
	cout<<"…";
	cout<<endl;
	cout<<"转换成功,转换出的数是: "; 
	cout<<TentoR(m,RtoTen(n,str));
	return 0;
}
2 个赞