跪求利用栈实现数制转换的C源程序!!!!
答案:1 悬赏:70 手机版
解决时间 2021-04-05 09:43
- 提问者网友:夢醒日落
- 2021-04-05 04:44
跪求利用栈实现数制转换的C源程序!!!!
最佳答案
- 五星知识达人网友:封刀令
- 2021-04-05 05:37
#include"stdio.h"
#include"stdlib.h"
#define null 0
#define n 10
struct stack{
int *base;
int *top;
int stacksize;
};
void initstack(struct stack *s)
{
s->base=(int*)malloc(20*sizeof(int));
if(!s->base) exit(0);
s->top=s->base;
s->stacksize=20;
return;
}
void push(struct stack*s,int e )
{
if(s->top-s->base>=s->stacksize)
{ s->base=(int*)realloc(s->base,(s->stacksize+n)*sizeof(int));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=n;
}
*(s->top)++=e;
return;
}
void pop(struct stack *s)
{ int e;
if(s->top==s->base) return;
e=*--s->top; printf("%d",e);
return;
}
void clearstack(struct stack*s)
{
if(s->top==s->base) return;
s->top=s->base;
return;
}
void conversion(int m)
{ struct stack s;
int*p=null;
initstack(&s);
while(m)
{ push(&s,m%2);
m=m/2;
}
for(p=s.top-1;p>=s.base;p--)
printf("%d",*p);
clearstack(&s);
}
main()
{int x;
printf("\nplease input a num (radix 10):");
scanf("%d",&x);
printf("\n after conversion (10)%d to 2: ",x);
conversion(x);
printf("\n");
}
这是我以前写的,运行通过的 数制转换程序,供参考.
#include"stdlib.h"
#define null 0
#define n 10
struct stack{
int *base;
int *top;
int stacksize;
};
void initstack(struct stack *s)
{
s->base=(int*)malloc(20*sizeof(int));
if(!s->base) exit(0);
s->top=s->base;
s->stacksize=20;
return;
}
void push(struct stack*s,int e )
{
if(s->top-s->base>=s->stacksize)
{ s->base=(int*)realloc(s->base,(s->stacksize+n)*sizeof(int));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=n;
}
*(s->top)++=e;
return;
}
void pop(struct stack *s)
{ int e;
if(s->top==s->base) return;
e=*--s->top; printf("%d",e);
return;
}
void clearstack(struct stack*s)
{
if(s->top==s->base) return;
s->top=s->base;
return;
}
void conversion(int m)
{ struct stack s;
int*p=null;
initstack(&s);
while(m)
{ push(&s,m%2);
m=m/2;
}
for(p=s.top-1;p>=s.base;p--)
printf("%d",*p);
clearstack(&s);
}
main()
{int x;
printf("\nplease input a num (radix 10):");
scanf("%d",&x);
printf("\n after conversion (10)%d to 2: ",x);
conversion(x);
printf("\n");
}
这是我以前写的,运行通过的 数制转换程序,供参考.
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯