#include <iostream>
#include <stack>
#include <string>
void decimalToBinary(int n) {
std::stack<int> binaryStack;
while (n > 0) {
binaryStack.push(n % 2);
n = n / 2;
}
while (!binaryStack.empty()) {
std::cout << binaryStack.top();
binaryStack.pop();
}
std::cout << std::endl;
}
void decimalToOctal(int n) {
std::stack<int> octalStack;
while (n != 0) {
octalStack.push(n % 8);
n = n / 8;
}
while (!octalStack.empty()) {
std::cout << octalStack.top();
octalStack.pop();
}
std::cout << std::endl;
}
void decimalToHexadecimal(int n) {
std::stack<int> hexStack;
while (n != 0) {
int remainder = n % 16;
if (remainder < 10) {
hexStack.push(remainder);
} else {
char hexChar = 'A' + remainder - 10;
hexStack.push(hexChar);
}
n = n / 16;
}
while (!hexStack.empty()) {
std::cout << hexStack.top();
hexStack.pop();
}
std::cout << std::endl;
}
int main() {
int number;
std::cout << "请输入一个十进制数: ";
std::cin >> number;
std::cout << "对应的二进制数是: ";
decimalToBinary(number);
std::cout << "对应的八进制数是: ";
decimalToOctal(number);
std::cout << "对应的十六进制数是: ";
decimalToHexadecimal(number);
return 0;
}
10-26
4544
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交