中易网

用栈实现进制的转换

答案:1  悬赏:40  
解决时间 2021-01-11 01:31
  • 提问者网友:风月客
  • 2021-01-10 07:02
用栈实现进制的转换
最佳答案
  • 二级知识专家网友:玩世
  • 2021-01-10 07:52
程序如下,运行通过:
#include
#include
#define maxsize 50

typedef int elemtype;

typedef struct
{
elemtype data[50];
int top;
}sqstack;

void initstack(sqstack *s)
{
s=(sqstack *)malloc(sizeof(sqstack));
s->top=-1;
}

int stacklength(sqstack *s)
{
return (s->top+1);
}

elemtype gettop(sqstack *s)
{
elemtype x;
if(s->top==-1) return 0;
x=s->data[s->top];
return x;
}

int push(sqstack *s,elemtype x)
{
s->top++;
s->data[s->top]=x;
return 1;
}

int pop(sqstack *s)
{
if(s->top==-1) return 0;
else s->top--;
return 1;
}

int stackempty(sqstack *s)
{
if(s->top==-1) return 1;
else return 0;
}

void visit(sqstack *s)
{
int i;
elemtype x;
i=s->top;
while(i!=-1)
{x=s->data[i];
printf("%3d",x);
i--;
}

printf("\n");
}

void clearstack(sqstack *s)
{
s->top=-1;
}

void convert(int n,int e)
{
int i;
sqstack *s;
initstack(s);
while(n)
{
i=n%e;
n=n/e;
push(s,i);
}
printf("the number after conversion:\n");
while(!stackempty(s))
{
printf("%d",gettop(s));
pop(s);
}
}

void main()
{
int i,j;
clrscr();
printf("Enter the number you want to convert:");
scanf("%d",&i);

printf("Enter the hexadecimal :");
scanf("%d",&j);

convert(i,j);
getch();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息