数据结构实验之栈一:进制转换
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
第二行输入R。
输出
示例输入
1279 8
示例输出
2377
#include <stdio.h>
#include <stdlib.h>
#define stackmax 10000 //存储空间初始分配量
#define stacknum 10000 //存储空间分配增量
typedef int ElemType;
typedef struct
{
ElemType *base; //栈底指针
ElemType *top; //栈顶指针
int stacksize;
} SqStack;
int InitStack(SqStack &S) // 构造一个空栈S
{
S.base = (ElemType*) malloc (stackmax*sizeof(ElemType));
if (! S.base)
exit(0); //存储分配失败
S.top = S.base;//空栈条件
S.stacksize = stackmax; //栈的存储空间初始分配量
return 0;
}
int Push(SqStack &S , int e)// 插入元素e为新的栈顶元素(创建一个栈)
{
if(S.top-S.base >= S.stacksize)// 当前存储空间已满,增加分配
{
{
S.base = (ElemType *)realloc(S.base,(S.stacksize+stacknum)*sizeof(ElemType));
if (! S.base ) exit(0); // 存储分配失败
S.top = S.base + S.stacksize;
S.stacksize += stacknum; //增加存储容量
}
}*S.top++=e;
return 0;
}
void conversion(SqStack &S, int n, int m)//进制转换
{
int t;
while (n)
{
t = n%m;
n = n/m;
Push(S, t);
}
}
void putstack(SqStack &S)//出栈
{
while(S.top>S.base)
{
printf("%d", *(S.top-1));
S.top--;
}
}
int main()
{
int n, m;
scanf("%d %d", &n, &m);
SqStack S;
InitStack(S);
conversion(S, n, m);
putstack(S);
return 0;
}