老师是把余数10,11,12,13直接压进栈的,感觉直接压A、B、C、D方便很多。
老师的方法暂未看。
上一个程序不完善,因为有16进制。
https://www.cnblogs.com/curo0119/p/8304924.html
自己修改:应该正确:
#include <iostream>
#include <stdlib.h>
#include "MyStack.h"
#include <algorithm>
#include <string>
using namespace std;
//用栈进行进制转换
#define BIN 2
#define OCT 8
#define HEX 16
int main(void)
{
string str = "0123456789ABCDEF";
MyStack *pStack = new MyStack(30);
int N = 12;
while(N)
{
int tmp = N % HEX;
pStack->push(str[tmp]);
N /= HEX;
}
pStack->stackTraverse(false);
delete pStack;
pStack = NULL;
return 0;
}
栈不能通过下标访问,需要重载
可以通过pop方法打印弹出的值
当打印的数是十六进制,会出现显示问题,所以我们自己建立索引列表
针对十六进制的优化
char num[]="0123456789ABCDEF";
数字作为num下标即可表示出>=10时的正确数字。