代码实现:
#include <iostream>
#include <map>
using namespace std;
#define N 100
#define type int
type st[N];
int top = -1;
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == N - 1;
}
void push(int x) {
if (!isFull()) st[++ top] = x;
else cout << "full" << endl;
}
void pop() {
if (!isEmpty()) -- top;
else cout << "empty" << endl;
}
int peek() {
if (!isEmpty()) return st[top];
cout << "empty" << endl;
}
map<int, char> mp{ {10,'A'},{11,'B'},{12,'C'},{13,'D'},{14,'E'},{15,'F'} };
void conversion(int x, int base) {
while (x) {
push(x % base);
x /= base;
}
while (!isEmpty()) {
int res = peek();pop();
if (res >= 10) cout << mp[res] << " ";
else cout << res << " ";
}
cout << endl;
}
int main() {
int a = 1000;
conversion(a, 16);
return 0;
}