建立顺序栈或链栈,编写程序实现十进制数到二进制数的转换。
输入
输入只有一行,就是十进制整数。
输出
转换后的二进制数。
样例输入
10
样例输出
1010
#include<stdio.h>
#include<stdlib.h>
#define Max 1000
typedef struct
{
int stack[Max];
int top;
}Seqstack;
void Initstack(Seqstack *&S)
{
S=(Seqstack *)malloc(sizeof(Seqstack));
S->top=-1;
}
void Pushstack(Seqstack *&S,int x)//进栈
{
S->top++;
S->stack[S->top]=x;
}
void Getstack(Seqstack *S)//取栈顶
{
int n=S->stack[S->top],x=1,b=0,a;//n表栈顶元素
while(n!=0)
{
a=n%2;//a表示元素取余
n=n/2;
b+=a*x;//b表示二进制数
x*=10;//x是控制个位,十位等
}
printf("%d",b);//不用空格
}
int main()
{
int x;
scanf("%d",&x);
Seqstack *S;
Initstack(S);
Pushstack(S,x);
Getstack(S);
return 0;
}