#include <iostream>
using namespace std;
const int maxN = 100 + 10;
int d[maxN];
void radix(int x, int t){
cout << "输出的" << t << "进制数为:";
if (!x) {
cout << x << endl;
return;
}
int tot = 0;
while (x) {
d[tot++] = x % t, x /= t;
//输入的十进制数求余赋值给数组,再将十进制数除以要转换的进制数,利用循环求余的
}
while (tot > 0){
tot--;
if (d[tot] > 9) {
cout << char(d[tot] + 'A' - 10);
//逆序输出
}
else {
cout << d[tot];
}
}
cout << endl;
}
int main(){
cout << "请输入一个十进制数:";
int x;cin >> x;
radix(x, 2); //转换为二进制
radix(x, 8); //转换为8进制
radix(x, 16); //转换为16进制
//cout << "请输入一个十进制数:";
//int x;cin >> x;
//cout << "输入要转换成几进制:";
//int y; cin >> y;
//radix(x, y); //转换为2,8,16进制
return 0;
}
第三期 C/C++数据结构 栈的基本操作
于 2022-03-17 22:29:12 首次发布