支持任意進制轉換
#include
#include
#define MAXSIZE 100
typedef int datacontent;
/*定義棧*/
typedef struct{
datacontent a[MAXSIZE];
int toppoint;
}sequence_list;
/*初始化棧*/
void init(sequence_list *pt){
pt->toppoint=0;
}
/*輸入函數*/
void input(int *a,int *b){
printf("請輸入待轉化的數:\n");
scanf("%d",a);
printf("請輸入待轉化的進制:\n");
scanf("%d",b);
}
/*進棧函數*/
void push(int a,int b,sequence_list *pt){
int i,j,c;
if(pt->toppoint>=MAXSIZE-1){
printf("棧是滿的,無法操作\n");
return;
}
else{
do{
c=a%b;
a=a/b;
pt->a[pt->toppoint]=c;
pt->toppoint++;
}while(a!=0);
}
pt->toppoint--;
}
/*出棧及輸出函數*/
void pop(sequence_list L){
int i;
printf("轉換後爲:\n");
for(i=L.toppoint;i>=0;i--){
printf("%d",L.a[i]);
}
printf("\n");
}
int main()
{
int a,b;
sequence_list L;
init(&L);
input(&a,&b);
push(a,b,&L);
pop(L);
return 0;
}