//用棧實現進制轉換十進制轉換成:二進制數、八進制數、十六進制數
#include<stdio.h> //進制轉換 實現十進制轉換成:二進制數、八進制數、十六進制數
#include<malloc.h>
#include<process.h>
#define MAXSIZE 50
typedef struct
{
int data[MAXSIZE];
int top;
}list_stack;
int main()
{
printf("(程序功能:實現十進制轉換成二進制數、八進制數、十六進制數)\n");
list_stack *p;
p=(list_stack*)malloc(sizeof(list_stack));
p->top=0;
int i,j;
printf("請輸入你要轉換的十進制數:\n");
scanf("%d",&i);
printf("轉換后的數為:",i);
for(;;)
{
printf("你要轉化成何種進制的數:\n0.退出\t2.二進制\t8.八進制\t16.十六進制\n",i);
scanf("%d",&j);
if(j==0)
{
exit(0);
}
if(j==2)
{
void change_stack(list_stack *p,int i,int j);//調用進制轉換的函數
change_stack(p,i,j);
}
if(j==8)
{
void change_stack(list_stack *p,int i,int j);//調用進制轉換的函數
change_stack(p,i,j);
}
if(j==16)
{
void change_stack(list_stack *p,int i,int j);//調用進制轉換的函數
change_stack(p,i,j);
}
if(j!=0 ||j!=2||j!=8||j!=16)
printf("您輸入有誤,請重新輸入!");
printf("\n");
}
return 0;
}
//實現進制轉換的函數
void change_stack(list_stack *p,int i,int j)
{
int chushu,shang,yushu;
while(shang!=0)
{
chushu=i;
shang=i/j;
yushu=i%j;
p->data[p->top]=yushu;//余數入棧
p->top=p->top+1;
i=shang;
}
void delet_stack(list_stack *p);//調用出棧函數
delet_stack(p);
}
void delet_stack(list_stack *p)//出棧
{
int x;
if(p->top==0)
printf("當前棧為空!");
else
while(p->top!=NULL)
{
x=p->data[p->top-1]; //存儲需要刪除的棧頂元素
printf("%d",x);
p->top--;
}
}