C++调用顺序栈的基本操作实现进制转换

    科技2025-07-11  23

    C++ 调用顺序栈的基本操作实现进制转换

    提前定义好常量,根据需要定义栈的长短。

    const int MaxSize = 20;

    定义栈的结构:因为涉及到进制转换,这里的data数组是int类型

    typedef struct {//栈的结构 int data[MaxSize];//存放数据 int top;//top下标 }SqStack;

    初始化栈

    void InitStact(SqStack * &s) {//初始化栈 s = (SqStack*)malloc(sizeof(SqStack)); s->top = -1; }

    销毁栈(非必须)

    void DestroyStack(SqStack * &s) {//销毁 free(s); }

    判断栈是否为空

    bool StackEmpty(SqStack * &s) {//判断栈是否为空 return(s->top == -1); }

    进栈

    bool Push(SqStack * &s, int e) {//进栈 if (s->top == MaxSize-1) return false; s->top++; s->data[s->top] = e; return true; }

    出栈

    bool Pop(SqStack * s, int& e) {//出栈 if (s->top == -1) return false; e = s->data[s->top]; s->top--; return true; }

    取栈顶元素 (不必要)

    bool GetTop(SqStack * s, int& e) {//取栈顶元素 if(s->top == -1) return false; e = s->data[s->top]; return true; }

    进制转换函数

    void change(int number, int aimScale, SqStack* s)//number为要被转换的数 aimScale为目标进制 s为栈 { int quotient;//商 int remainder;//余数 quotient = number / aimScale; remainder = number % aimScale; Push(s, remainder);//进栈 while (quotient != 0) { remainder = quotient % aimScale;//这行和下一行的顺序不能互换,如果互换quotient的值会被提前改变 quotient = quotient / aimScale; Push(s, remainder);//进栈 } }

    打印

    void printNumber(SqStack* s) {//打印栈的数值 int number; cout << "转换后的数字为:" << endl; while (!StackEmpty(s)) {//当栈不为空的时候循环 Pop(s, number);//出栈 cout << number;//不要加endl,防止多次输出换行 } }

    总代码

    #include<iostream> using namespace std; const int MaxSize = 20; typedef struct {//栈的结构 int data[MaxSize];//存放数据 int top;//top下标 }SqStack; void InitStact(SqStack * &s) {//初始化栈 s = (SqStack*)malloc(sizeof(SqStack)); s->top = -1; } void DestroyStack(SqStack * &s) {//销毁 free(s); } bool StackEmpty(SqStack * &s) {//判断栈是否为空 return(s->top == -1); } bool Push(SqStack * &s, int e) {//进栈 if (s->top == MaxSize-1) return false; s->top++; s->data[s->top] = e; return true; } bool Pop(SqStack * s, int& e) {//出栈 if (s->top == -1) return false; e = s->data[s->top]; s->top--; return true; } bool GetTop(SqStack * s, int& e) {//取栈顶元素 if(s->top == -1) return false; e = s->data[s->top]; return true; } void change(int number, int aimScale, SqStack* s)//number为要被转换的数 aimScale为目标进制 s为栈 { int quotient;//商 int remainder;//余数 quotient = number / aimScale; remainder = number % aimScale; Push(s, remainder);//进栈 while (quotient != 0) { remainder = quotient % aimScale;//这行和下一行的顺序不能互换,如果互换quotient的值会被提前改变 quotient = quotient / aimScale; Push(s, remainder);//进栈 } } void printNumber(SqStack* s) {//打印栈的数值 int number; cout << "转换后的数字为:" << endl; while (!StackEmpty(s)) {//当栈不为空的时候循环 Pop(s, number);//出栈 cout << number;//不要加endl,防止多次输出换行 } } int main() { int number,aim;//number为数字,aim为目标进制 SqStack* sq;//创建SqStack指针 InitStact(sq);//初始化SqStack cout << "请输入要转换进制的十进制整数" << endl; cin >> number; cout << "请输入要转换到的进制数" << endl; cin >> aim; change(number, aim, sq); printNumber(sq); DestroyStack(sq); return 0; }

    遗留问题: 1.对于大于十进制的字母表示和进制范围未做出限制处理,不正确的进制会导致各种问题。 2.没有规定输入的数的进制,默认是十进制。

    Processed: 0.009, SQL: 8