数据结构——进制转换

xiaoxiao2021-02-28  16

#include #define MAXSIZE 100 //顺序栈储存空间的初始分配量 using namespace std; typedef int selemtype; typedef struct { //顺序栈的存储结构 selemtype *base; //栈底指针 selemtype *top; //栈顶指针 int stacksize; }SqStack; void InitStack(SqStack &s) { //顺序栈的初始化 s.base = new selemtype[MAXSIZE]; if (!s.base)return; s.top = s.base; s.stacksize = MAXSIZE; } void Push(SqStack &s, selemtype e) { //顺序栈的入栈 if (s.top - s.base == s.stacksize)return; *s.top++ = e; } bool StackEmpty(SqStack &s) { //顺序栈的判空,采用bool型 if (s.top == s.base)return true; else return false; } void Pop(SqStack &s, selemtype &e) { //顺序栈的出栈 if (s.top == s.base)return; e = *--s.top; } void main() { //进制转换的方法,N为所要转换的数,x为所要转换的进制 int N; cout << "请输入所要转换的数" << endl; cin >> N; int e; int x; cout << "请输入所要转换的进制" << endl; cin >> x; SqStack s; InitStack(s); while (N) { Push(s, N%x); N = N / x; } while (!StackEmpty(s)) { Pop(s, e); cout << e; } }
转载请注明原文地址: https://www.6miu.com/read-2150369.html

最新回复(0)