主题:帮帮忙,改改错,谢谢!
利用栈的基本操作将一个十进制正整数转换成二进制数据,并将结果输出.
#include<stdio.h>
#define STACK_INTSIZE 50
typedef int datatype;
typedef struct
{
datatype st[STACK_INTSIZE];
int top;
}stack;
void initstack(&st)
{
st->maxsize=10;
st->top=-1;
}
void push(stack *st,datatype x)
{
if(st->top==STACK_INTSIZE-1)
printf("the stack is full!");
else
{
st->top++;
st->s[st->top]=x;
}
}
void pop(stack*st)
{
datatype x;
if(st->top==0)
printf("the stack is empty!");
else
{
x=st->s[st->top];
printf("the elem of exiting is:%d\n",x);
st->top--;
}
}
void conversion()
{
int n;
initstack(st);
stack(st);
scanf("%d",&n);
while(n)
{
push(&st,n%2);
n=n/2;
}
while(!stackempty(st))
{
pop(&st,e);
printf("%d",e);
}
}
int stackempty(&st)
{
if(st->top==-1)
return(1);
else return(0);
}
void main()
{
stack st;
datatype x;
conversion();
getchar();
}
#include<stdio.h>
#define STACK_INTSIZE 50
typedef int datatype;
typedef struct
{
datatype st[STACK_INTSIZE];
int top;
}stack;
void initstack(&st)
{
st->maxsize=10;
st->top=-1;
}
void push(stack *st,datatype x)
{
if(st->top==STACK_INTSIZE-1)
printf("the stack is full!");
else
{
st->top++;
st->s[st->top]=x;
}
}
void pop(stack*st)
{
datatype x;
if(st->top==0)
printf("the stack is empty!");
else
{
x=st->s[st->top];
printf("the elem of exiting is:%d\n",x);
st->top--;
}
}
void conversion()
{
int n;
initstack(st);
stack(st);
scanf("%d",&n);
while(n)
{
push(&st,n%2);
n=n/2;
}
while(!stackempty(st))
{
pop(&st,e);
printf("%d",e);
}
}
int stackempty(&st)
{
if(st->top==-1)
return(1);
else return(0);
}
void main()
{
stack st;
datatype x;
conversion();
getchar();
}