#include <stdio.h>
#include <conio.h>
#define MaxLength 300
typedef char ElementType;
typedef struct
{
ElementType Elements[MaxLength];
int Top_idx;
}Stack;
void MakeNull_Stack(Stack *S)
{
S->Top_idx=MaxLength;
}
int Full_Stack (Stack S)
{
return (S.Top_idx==0);
}
int Empty_Stack(Stack S)
{
return (S.Top_idx==MaxLength);
}
ElementType Top(Stack S)
{
if(!Empty_Stack(S))
return S.Elements[S.Top_idx];
else
{
printf("\n loi!Stack rong");
return NULL;
}
}
void Push(ElementType X, Stack *S)
{
if(Full_Stack(*S))
printf("\n loi! Stack day khong the them");
else
{
S->Top_idx=(S->Top_idx-1);
S->Elements[S->Top_idx]=X;
}
}
void Pop(Stack *S)
{
if(Empty_Stack(*S))
printf("\n loi!Stack rong");
else
{
S->Top_idx=(S->Top_idx+1);
}
}
int main()
{
int n,m;
Stack S;
printf("\n nhap vao so thap phan can doi: "); scanf("%d",&n);
m=n;
if(n==0)
printf("\n so nhi phan la 0 ");
else
{
MakeNull_Stack(&S);
while(m!=0)
{
Push(m%2,&S);
m=m/2;
}
while(!Empty_Stack(S))
{
printf("%d",Top(S));
Pop(&S);
}
}
getch();
return 0;
}
Không có nhận xét nào:
Đăng nhận xét