ADD file via upload

main
fdzcxy212206260 2 months ago
parent e0ed0de4f4
commit 183a872823

@ -0,0 +1,73 @@
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
// 符号转化为数字
int get_num(char c) {
if (isdigit(c)) {
return c - '0';
}
if ('a' <= c && c <= 'z') {
return c - 'a' + 10;
}
if ('A' <= c && c <= 'Z') {
return c - 'A' + 10;
}
return 0;
}
// 数字转化为符号
char get_char(int num) {
return '0';
}
//将字符串根据进制转化为数字
int to_num(string& number, int base) {
int n = number.size();
long long num = 0;
for (int i = 0; i < n; i++) {
char ch = number[i];
num *= base;
num += get_num(ch);
}
return num;
}
//将数字根据进制转化为字符串
string tostring(int num, int base) {
if (num == 0) return "0";
string ans;
while (num > 0) {
ans += get_char(num % base);
num /= base;
}
reverse(ans.begin(), ans.end());
return ans;
}
string convertBase(string& num, int st_base, int en_base) {
return tostring(to_num(num, st_base), en_base);
}
int main() {
string number;
int st_base, en_base;
cout << "输入数字:";
cin >> number;
cout << "输入源进制2-16";
cin >> st_base;
cout << "输入目标进制2-16";
cin >> en_base;
string ans = convertBase(number, st_base, en_base);
cout << "转换后的数字是:" << ans << endl;
return 0;
}
Loading…
Cancel
Save