#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 100
typedef int ElemType;
typedef struct {
ElemType data[STACK_MAX_SIZE];
int stacktop;
}Sqstack;
//棧的初始化
Sqstack InitStack(Sqstack &l){
l.stacktop=-1;
return l;
}
//判斷棧空(如果為空棧傳回0,否則傳回1)
int EmptyStack(Sqstack &l){
if(l.stacktop==-1){
return 0;
}
else
return 1;
}
//進棧
void Pushstack(Sqstack &l,int temp){
if(l.stacktop==STACK_MAX_SIZE-1) {
printf("此棧空間已滿!\n");
}
l.stacktop++;
l.data[l.stacktop]=temp;
}
//出棧
int Popstack(Sqstack &l){
int temp;
if(l.stacktop==-1){
printf("此棧為空!\n");
}
temp=l.data[l.stacktop];
l.stacktop--;
return temp;
}
//進制轉換
void conversion(){
Sqstack l;
int n,e,m;
InitStack(l);
printf("請輸入任意一個非負十進制整數:\n");
scanf("%d",&n);
printf("請輸入要轉換的進制數(二進制或八進制):\n");
scanf("%d",&m);
while(n){
Pushstack(l,n%m);
n=n/m;
}
printf("轉換為%d進制的結果為:\n",m);
while(EmptyStack(l)){
e=Popstack(l);
printf("%d",e);
}
}
int main() {
while(1){
conversion();
printf("\n");
}
return 0;
}