问题遇到的现象和发生背景
C的数据结构,栈转化10进制总是报空,代码不知道哪里出错啦,麻烦各位办一下忙
用代码块功能插入代码,请勿粘贴截图
#include<iostream>
using namespace std;
//顺序栈结构体类型定义
typedef int SElemType;
const int STACK_INIT_SIZE = 100;
typedef struct {
SElemType* base;
SElemType* top;
int stacksize;
}sqstack;
//初始化顺序栈
void init_stack(sqstack& s) {
s.base = new SElemType[STACK_INIT_SIZE];
if (!s.base)
exit(-1);
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
}
//判断栈满
int stack_full(sqstack s) {
if (s.top - s.base >= s.stacksize)
return 1;
else
return 0;
}
//判断栈空
int stack_empty(sqstack s) {
if (s.base = s.top)
return 1;
else
return 0;
}
//入栈
void push_satck_sq(sqstack& s, SElemType x) {
if (stack_full(s))
cout << "The stack is full !" << endl;
else *s.top++ = x;
}
//出栈
int pop_stack_sq(sqstack& s, SElemType& x) {
if (stack_empty(s))
{
cout << "The satck is empty !" << endl;
return 0;
}
else
x = *(--s.top);
return x;
}
//获得栈顶元素
SElemType gettop_sq(sqstack s) {
if (stack_empty(s))
cout << "The stack is empty !";
else
return *(s.top - 1);
}
//输出栈中元素
void printstack_sq(sqstack s) {
sqstack p = s;
if (stack_empty(p))
cout << "The stack is empty !" << endl;
else
{
while (!stack_empty(p))
{
cout << gettop_sq(p) << " ";
p.top--;
}
}
}
void main(){
sqstack s;
init_stack(s);
int a, b;
cout << "请输入你要转换的十进制数" << endl;
cin >> a;
if (a != 0)
{
cout << "请输入你要转换的进制(最多支持16进制)" << endl;
cin >> b;
while (b > 16 || b < 0)
{
cout << "请输入正确的进制数(请输入你要转换的进制(最多支持16进制))" << endl;
cin >> b;
}
while (a != 0)
{
push_satck_sq(s, a % b);
a = a / b;
}
printstack_sq(s);
}
else cout << "输出完成" << endl;
}
运行结果及报错内容
我想要达到的结果
运行正常