您的位置:首页 > 理论基础 > 数据结构算法

数据结构C语言版进制转换

2015-12-24 12:21 351 查看
//进制转换:
#include<stdio.h>
#include<stdlib.h>
#define Stack_size 100
#define Stackincreament 10
typedef struct{
int *base;
int top;
int stacksize;
}sqstack;
void initstack(sqstack &la)
{
la.base=(int*)malloc(Stack_size*sizeof(int));
la.stacksize=Stack_size;
if(!la.base)
exit(0);
la.top=0;
}
void push(sqstack &L,int e)
{
if(L.top>=L.stacksize)
{
L.base=(int*)realloc(L.base,(Stack_size+Stackincreament)*sizeof(int));
if(!L.base)
exit(0);
L.stacksize+=Stackincreament;
}
L.base[L.top++]=e;
}
void pop(sqstack &l,int &e)
{
if(l.top==0)
return ;
e=l.base[--l.top];

}
void trans(int x,int n,sqstack &s)
{
int e;
while(x!=0)
{
e=x%n;
x/=n;
push(s,e);

}
while(s.top!=0)
{
pop(s,e);
printf("%5d",e);
}
printf("\n");

}

int main()
{
sqstack m;
int x,n;
printf("请输入需转换的十进制数及转换后的进制数:\n");
scanf("%d",&x);
scanf("%d",&n);
initstack(m);
trans(x,n,m);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 c语言