主题:请高手指点!栈应用实现进制转换
//栈应用实现进制转换
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 2 //存储空间初始分配量
#define STACK_ADD_SIZE 10 //存储空间分配增量
#define NULL 0
typedef union Selem //定义共用体
{
int i;
char ch;
float f;
}selem;
#define selem int
class Sqstack //定义类
{
private:
selem* top;
selem* base;
int stacksize;
public:
//返回栈顶元素
selem getTop()
{
return *(top -1);
}
//返回栈底元素
selem getBase()
{
return *base;
}
//返回栈长
int getStacksize()
{
return stacksize;
}
Sqstack(void); //构造方法
~Sqstack(void); //折构方法
//构造一个空栈
friend void initStack(Sqstack* s);
//插入新栈e为新栈顶元素--压入栈
friend bool push(Sqstack*s,selem e);
//出栈,存在e中
friend bool pop(Sqstack* s,selem *e);
//判断栈是否为空
friend bool isEmpty(Sqstack* s);
};
void initStack(Sqstack* s)
{
s->base = (selem*)malloc(STACK_INIT_SIZE * sizeof(selem));
if(s->base == NULL)exit(0);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
cout <<"栈底地址"<<s->base<<endl;
}
bool push(Sqstack* s,selem e)
{
if(s->top - s->base >= s->stacksize) //如果空间已满,追加空间
{
s->base = (selem*)realloc(s->base,STACK_ADD_SIZE * sizeof(selem));
if(s->base == NULL)exit(0);
// s->top = s->base + s->stacksize;
s->stacksize += STACK_ADD_SIZE;
}
*s->top++ = e;
return true;
}
bool pop(Sqstack* s,selem *e)
{
if(s->top == s->base)return false; //如果是空栈
*e = *--s->top;
cout <<"栈底地址"<<s->base<<endl;
return true;
}
bool isEmpty(Sqstack* s)
{
cout <<s->top <<endl;
cout <<s->base <<endl;
if(s->top == s->base)return true;
return false;
}
Sqstack::Sqstack()
{
cout << "\n";
}
Sqstack::~Sqstack()
{
cout <<"\n";
}
void conversion()
{
Sqstack s;
selem se,num;
initStack(&s);
//cin >> num;
num = 1348;
while(num)
{
push(&s,num % 8);
num /= 8;
}
while(!isEmpty(&s))
{
if(pop(&s,&se))
printf("%d",se);
}
}
void main()
{
conversion();
}
运行后,发现 while(!isEmpty(&s))
{
if(pop(&s,&se))
printf("%d",se);
}
循环不能在,栈为空的情况下停止,但是用Debug调试出来的结果,是循环在栈为空的情况下可以停止,但真正运行的时候,就不能停止了,请大家指点下,谢谢!
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 2 //存储空间初始分配量
#define STACK_ADD_SIZE 10 //存储空间分配增量
#define NULL 0
typedef union Selem //定义共用体
{
int i;
char ch;
float f;
}selem;
#define selem int
class Sqstack //定义类
{
private:
selem* top;
selem* base;
int stacksize;
public:
//返回栈顶元素
selem getTop()
{
return *(top -1);
}
//返回栈底元素
selem getBase()
{
return *base;
}
//返回栈长
int getStacksize()
{
return stacksize;
}
Sqstack(void); //构造方法
~Sqstack(void); //折构方法
//构造一个空栈
friend void initStack(Sqstack* s);
//插入新栈e为新栈顶元素--压入栈
friend bool push(Sqstack*s,selem e);
//出栈,存在e中
friend bool pop(Sqstack* s,selem *e);
//判断栈是否为空
friend bool isEmpty(Sqstack* s);
};
void initStack(Sqstack* s)
{
s->base = (selem*)malloc(STACK_INIT_SIZE * sizeof(selem));
if(s->base == NULL)exit(0);
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
cout <<"栈底地址"<<s->base<<endl;
}
bool push(Sqstack* s,selem e)
{
if(s->top - s->base >= s->stacksize) //如果空间已满,追加空间
{
s->base = (selem*)realloc(s->base,STACK_ADD_SIZE * sizeof(selem));
if(s->base == NULL)exit(0);
// s->top = s->base + s->stacksize;
s->stacksize += STACK_ADD_SIZE;
}
*s->top++ = e;
return true;
}
bool pop(Sqstack* s,selem *e)
{
if(s->top == s->base)return false; //如果是空栈
*e = *--s->top;
cout <<"栈底地址"<<s->base<<endl;
return true;
}
bool isEmpty(Sqstack* s)
{
cout <<s->top <<endl;
cout <<s->base <<endl;
if(s->top == s->base)return true;
return false;
}
Sqstack::Sqstack()
{
cout << "\n";
}
Sqstack::~Sqstack()
{
cout <<"\n";
}
void conversion()
{
Sqstack s;
selem se,num;
initStack(&s);
//cin >> num;
num = 1348;
while(num)
{
push(&s,num % 8);
num /= 8;
}
while(!isEmpty(&s))
{
if(pop(&s,&se))
printf("%d",se);
}
}
void main()
{
conversion();
}
运行后,发现 while(!isEmpty(&s))
{
if(pop(&s,&se))
printf("%d",se);
}
循环不能在,栈为空的情况下停止,但是用Debug调试出来的结果,是循环在栈为空的情况下可以停止,但真正运行的时候,就不能停止了,请大家指点下,谢谢!