请问大家这个数据结构栈的代码十进制转八进制为什么转弯八进制后后面还带着这么多垃圾数据?

#include <stdio.h>#include <stdlib.h>#include<stdbool.h>#define MAXSIZE 50typedef int elemtype;typedef struct{ elemtype data[MAXSIZE]; int top;}sqstack;//初始化栈 Svoid InitStack(sqstack &s){ s.top==-1;}//判断栈 S 是否为空 bool StackEmpty(sqstack s){ if(s.top==-1) return true; else return false;}//入栈函数 bool Push(sqstack &s,elemtype x){ if(s.top==MAXSIZE-1) return false; s.data[++s.top]=x; return true;}//出栈函数 bool Pop(sqstack &s,elemtype &x){ if(s.top==-1) return false; x=s.data[s.top--]; return true;}//十进制转八进制 int conversion(){ sqstack s; elemtype e; int n; printf("请输入一个十进制数:"); scanf("%d",&n); InitStack(s); while(n){ Push(s,n%8); n=n/8; } while(StackEmpty(s)==NULL){ Pop(s,e); printf("%d",e); }}int main(){ conversion(); return 0;}

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define MAXSIZE 50

typedef int elemtype;

typedef struct

{

elemtype data[MAXSIZE];

int top;

} sqstack;

//初始化栈 S

void InitStack(sqstack& s)

{

s.top = -1;

}

//判断栈 S 是否为空

bool StackEmpty(sqstack s)

{

if (s.top == -1)

return true;

else

return false;

}

//入栈函数

bool Push(sqstack& s, elemtype x)

{

if (s.top == MAXSIZE - 1)

return false;

s.data[++(s.top)] = x;

return true;

}

//出栈函数

bool Pop(sqstack& s, elemtype& x)

{

if (s.top == -1)

return false;

x = s.data[s.top--];

return true;

}

//十进制转八进制

void conversion()

{

sqstack s;

elemtype e;

int n;

printf("请输入一个十进制数:");

scanf("%d", &n);

InitStack(s);

while (n)

{

Push(s, n % 8);

n = n / 8;

}

while (StackEmpty(s) == false)

{

Pop(s, e);

printf("%d", e);

}

}

int main()

{

conversion();

return 0;

}

温馨提示:答案为网友推荐,仅供参考
相似回答