#include <stdio.h>
#include <malloc.h>
#define ElemType int
//链栈类型
typedef struct Node{
ElemType data; //数据域
struct Node *next; //指针域
}SNode, *LinkStack;
//判断存储分配是否成功
int MallocOK(LinkStack P){
if(P) return 1;
return 0;
}//MallocOK
//建立一个带头结点的空链栈S
int InitStack(LinkStack &S){
S=(LinkStack) malloc(sizeof(SNode));
if(MallocOK(S)) S->next=NULL;
return MallocOK(S);
}//InitStack
//输出链栈S中的所有数据元素
void Print(LinkStack S){
LinkStack p=S->next;
printf("/nS = ( ");
while(p){
printf("%d ",p->data);
p=p->next;
}//while
printf(")/n");
}//Print
//向链栈S的栈顶压入一个新的数据元素x
int Push(LinkStack &S,ElemType x){
LinkStack p;
p=(LinkStack) malloc(sizeof(SNode));
if(MallocOK(p)==0) return 0; //存储分配失败
p->data=x;
p->next=S->next;
S->next=p;
return 1;
}//Push
// 销毁链栈S
void ClearStack(LinkStack &S){
free(S);
S->next=NULL;
printf("/n链栈S已销毁/n");
}//ClearStack
//对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
//void conversion (int Num)
void main(){
LinkStack S;
int n,Num1,Num2,flag=1;
while (flag==1){
printf("/n请输入10进制数: ");
scanf("%d",&Num1);
printf("/n将10进制数转换成(2~9)进制数: ");
scanf("%d",&Num2);
if(Num2<2 || Num2>9) Num2=2;
else{
if(Num1<Num2){n=Num1; Num1=Num2; Num2=n;}
InitStack(S); //构造空链栈S
while(Num1){
Push(S, Num1 % Num2);
Num1 = Num1/Num2;
}//while
printf("/n将10进制数%d转换成%d进制数是",Num1,Num2);
Print(S);
}//if-else
printf("/nContinue conversion ? (1-Continue /0-Over) ");
scanf("%d",&flag);
}
ClearStack(S);
}//main