这篇代码是我早就打出来的,当时放在百度网盘,本地放在硬盘上,后来硬盘坏了,最近清网盘的时候才想起来,所以赶紧改了改就发出来了
这也是我比较早的代码了,所以记得并不清楚,改也只改了一点,当时不爱打注释,具体思路也忘得一干二净,所以直接把代码贴出来了(好像屁话有点多)
Code:
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int a=0;
char d[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
int leave(char x[], int y) {
int h = 0, k = strlen(x), max=-1, q[1919];
if(y>16) {
a = -1;
}
for (int j = k; j > 0; j--) {
if ('0' <= x[j - 1] && x[j - 1] <= '9') {
h += (x[j - 1] - 48) * pow(y, k - j);
} else if ('A' <= x[j - 1] && x[j - 1] <= 'F') {
h += (x[j - 1] - 55) * pow(y, k - j);
}
for(int i=0; i<k; i++) {
if(x[i]=='A'||x[i]=='B'||x[i]=='C'||x[i]=='D'||x[i]=='E'||x[i]=='F') {
q[i]=x[i]-55;
} else {
q[i]=x[i]-48;
}
if(q[i]>max) {
max=q[i];
}
}
}
if(max>y) {
a = -1;
return a;
}
return h;
}
void back(int a, int b) {
if (a == -1) {
cout << "DATA ERROR!(错误!)";
return;
}
int r;
r = a % b;
a /= b;
if (a) {
back(a, b);
}
cout << d[r];
return;
}
int main() {
char l[100000]= {0, 0, 0};
int s, p;
cout << "进制转换器v2.0 Made By 2024 August 8th ." << endl;
cout << "请输入待转换进制的数据和待转换的进制(仅支持2~36进制的正整数!!!(先输入待转换进制的数据,再输入待转换的进制!!))" << endl;
cin >> l >> s;
while(s<2||s>36) {
cout<<endl<<"输入的待转换的进制不在2~36之间!";
Sleep(1000);
cout<<endl<<"重新输入待转换的进制: ";
cin>>s;
}
if(s<2||s>36) cout<<endl;
cout << "请输入目标进制" << endl;
cin >> p;
while(p<2||p>36) {
cout<<endl<<"输入的待转换的进制不在2~36之间!";
Sleep(1000);
cout<<endl<<"重新输入目标进制: ";
cin>>p;
}
if(p<2||p>36) cout<<endl;
cout << "将 " << s << " 进制的 " << l << " 转换成 " << p << " 进制的结果为 ";
back(leave(l, s), p);
return 0;
}
标签:转换,进制,int,max,s36,C++,include,p36,cout
From: https://blog.csdn.net/j5486545648564/article/details/141033694