这是改后的
# include <iostream>
using namespace std;
typedef struct snode
{
int data;
struct snode *next;
}LinkSTACK;
LinkSTACK *top;
//初始化
void initSTACK(LinkSTACK **top)
{
*top=(LinkSTACK *)malloc(sizeof(LinkSTACK));
(*top)->next=NULL;
}
//压栈运算
int Push(LinkSTACK **top,int x)
{
LinkSTACK *s;
s=(LinkSTACK *)malloc(sizeof(LinkSTACK));
s->data=x;
s->next=(*top)->next;
(*top)->next=s;
return 1;
}
//判断栈空
int empty(LinkSTACK **top)
{
return ((*top)->next==NULL?1:0);
}
//出栈运算
int Pop(LinkSTACK **top,int *x)
{
LinkSTACK *s;
if(empty(top))
{
printf("\n 栈为空!");
cout<<endl;
return 0;
}
s=(*top)->next;
*x=s->data;
(*top)->next=s->next;
free(s);
return 1;
}
//取栈顶元素
int gettop(LinkSTACK **top,int *x)
{
if(empty(top))
{
printf("\n 栈为空!");
cout<<endl;
return 0;
}
*x=(*top)->next->data;
return 0;
}
//进制数转换*************************
void DecToOthers(int n,int b)
{
char B[]="0123456789ABCDEF";
int x;
LinkSTACK l,*t;
t=&l;
initSTACK(&t);
if(n=0)
{
Push(&t,n);
}
else
{
while(n)
{
Push(&t,n%b);
n=n/b;
}
}
while(!empty(&t))
{
Pop(&t,&x);
printf("%c",B[x]);
}
}
int main()
{
LinkSTACK l1,*t;
t=&l1;
initSTACK(&t);
int a,b;
cout<<"请输入要压栈的元素个数:";
cin>>a;
for(int i=0;i<a;i++)
{
int a1;
cout<<"请输入所要压入的元素:";
cin>>a1;
Push(&t,a1);
}
gettop(&t,&b);
cout<<"栈顶元素为:"<<b<<endl;
cout<<"从栈顶依次取出元素:";
for(i=0;i<a;i++)
{
int n;
Pop(&t,&n);
cout<<n<<" ";
}
cout<<endl;
char ch;
do
{
int c,d;
cout<<"请输入想转换的进制数及转换成何种进制(依次输入,两者之间按空格键):";
cin>>c>>d;
cout<<c<<"的"<<d<<"进制数为:";
DecToOthers(c,d);
cout<<endl;
cout<<"是否要继续进行数值转换(按y继续,其他结束):";
cin>>ch;
}
while(ch=='y');
return 0;
}