堆栈解题
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef char DataType;
#define STACK_SIZE 100
typedef struct{
DataType *top;
DataType *base;
int stack_size;
}seqstack;
void init(seqstack *s)
{
s->base=(DataType *)malloc(STACK_SIZE*sizeof(DataType));
if(!s->base) exit(-1);
s->top=s->base;
s->stack_size=STACK_SIZE;
}
int IsEmpty(seqstack *s)
{
return s->base==s->top;
}
int IsFull(seqstack *s)
{
return s->top-s->base==STACK_SIZE-1;
}
void Push(seqstack *s,char ch)
{
if(IsFull(s))
{
printf("overflow\n!");
exit(1);
}
else
*s->top++=ch;
}
char Pop(seqstack *s)
{
if(IsEmpty(s))
{
printf("栈空\n");
exit(1);
}
return *--s->top;
}
char Top(seqstack *s)
{
if(IsEmpty(s))
{
printf("栈空\n");
exit(1);
}
return *(s->top-1);
}
void conversion(int a,int b)
{
int i;
seqstack s;
init(&s);
while(a)
{
Push(&s,a%b);
a=a/b;
}
while(!IsEmpty(&s))
{
i=Pop(&s);
printf("%d",i);
}
free(s.base);
}
int main(int argc,char *argv[])
{
int a,b;
printf("please input the data you want to conversion\n");
scanf("%d",&a);
printf("please input the scale you want to convert to\n");
scanf("%d",&b);
conversion(a,b);
return 0;
}