//顺序栈,不知为何写数据结构的时候总是很纠结的。传参应用我一直很弱,真的要加强训练
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef struct node
{
struct node *next;
int data;
}*link;
typedef struct
{
int *base;
int *top;
int stacksize;
}stack;
int Initstack(stack &s)
{
s.base = (int *)malloc(100*sizeof(int));
if(!s.base) return 0;//exit(0);
s.top = s.base;
s.stacksize = 100;
return 1;
}
int stackempty(stack s)
{
if(s.base == s.top)
return 0;
else
return 1;
}
int push(stack &s, int n)
{
/*
if(s.top - s.base >= s.stacksize)
{
s.base = (int *)realloc(s.base,(s.stacksize +100)*sizeof(int));
if(!s.base) exit(0);
s.top = s.base + s.stacksize;
s.stacksize += 10;
}
*/
*s.top++ = n;
return 1;
}
int pop(stack &s, int &n)
{
if(s.top == s.base)
{
printf("ERROR");
return 0;
}
n = *--s.top;
return 1;
}
int main()
{
int n, e, r;
stack s;
while(cin>>n>>r)
{
if(n < 0)
{
printf("-");
n = 0 - n;
}
Initstack(s);
while(n)
{
push(s,n % r);
n = n / r;
}
while(stackempty(s))
{
pop(s, e);
if(e > 9)
printf("%c", e - 10 + 'A');
else
printf("%d", e);
}
printf("/n");
}
}
//链栈,不知为何写数据结构的时候总是很纠结的。传参应用我一直很弱,真的要加强训练
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef struct node
{
struct node *next;
int data;
}Node,*link;
void Initstack(Node *&st)//st 为指针。 引用型参数
{
st = NULL;
}
int stackempty(Node *st)
{
if(st == NULL)
return 0;
else
return 1;
}
void push(Node *&st, int n)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = n;
p->next = NULL;
if(st == NULL)
st = p;
else
{
p->next = st;
st = p;
}
}
int pop(Node *&st, int &n)
{
Node *p;
p = st;
if(st == NULL)
return 0;
else
{
n = st->data;
st = st->next;
free(p);
return 1;
}
}
int main()
{
int n, e, r;
Node *s;
while(cin>>n>>r)
{
if(n < 0)
{
printf("-");
n = 0 - n;
}
Initstack(s);
while(n)
{
push(s,n % r);
n = n / r;
}
while(stackempty(s))
{
pop(s, e);
if(e > 9)
printf("%c", e - 10 + 'A');
else
printf("%d", e);
}
printf("/n");
}
}