这个顺序栈进制转换实现不成功的原因是什么呀求指点(图一是参考的算法,图二是参考图一写的
#include<iostream>
#include<malloc.h>
#include <string>
using namespace std;
#define MaxSize 100
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int top;
} SqStack;
void InitStack(SqStack * &s);
bool StackEmpty(SqStack * s) ;
bool Push(SqStack * &s,ElemType e);
int StackLength(SqStack * s);
void DisplayStack(SqStack *s);
bool GetTop(SqStack*s,ElemType &e);
bool Pop(SqStack * &s,ElemType &e);
void DestroyStack(SqStack * &s);
string dec2base(int num,int base);
int main() {
int num,base,numStr;
cout<<"输入十进制数:";
cin>>num;
cout<<"要将其转换为几进制:";
cin>>base;
cout<<"转换后为:";
dec2base(num,base);
return 0;
}
void InitStack(SqStack * &s) {
s = (SqStack*) malloc(sizeof(SqStack));
s->top=-1;
}
bool StackEmpty(SqStack * s) {
return (s->top==-1);
}
bool Push(SqStack * &s,ElemType e) {
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
int StackLength(SqStack * s) {
return(s->top+1);
}
void DisplayStack(SqStack *s) {
for(int i=s->top; i>=0; i--) {
cout<<s->data[i]<<" ";
}
cout<<endl;
}
bool GetTop(SqStack*s,ElemType &e) {
if(s->top==-1)
return false;
e=s->data[s->top];
return true;
}
bool Pop(SqStack * &s,ElemType &e) {
if(s->top== -1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
void DestroyStack(SqStack * &s) {
free(s);
}
string dec2base(int num,int base) {
string digitChar = "0123456789ABCDEF";
string numStr = "";
SqStack * s;//
InitStack(s);//
ElemType e;//
do {
Push(s, digitChar[num%base]); //将余数入栈
num/=base;
} while(num != 0);
char temp;
while(!StackEmpty(s)) {
temp = s->top; //取栈顶
Pop(s,e); //出栈
printf("%d",e);
numStr += temp; //将余数出栈并附到numStr字符串中
}
return numStr;
}