数据结构实验:用栈实现进制转化
进制转换(以十进制转二进制为例)
#include <stdio.h>
#include <malloc.h>
#define max 100 //宏定义
typedef int elemtype;//表明该类型的作用,便于修改
typedef struct//定义结构体类型
{
int data[100];
int top;
}stack;
stack *inits()
{
stack *s;
s=(stack *)malloc(sizeof(stack));
s->top=-1;
return s;
}
void push(stack *s,int x)
{
s->top++;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top--;
return x;
}
int main()//主函数
{
stack *s,a;
int x,n;
s=inits();
scanf("%d",&x);
while(x)
{
push(s,x%2);
x=x/2;
}
while(s->top!=-1)
{
n=pop(s);
printf("%d",n);
}
}