#include<stdio.h> #include<stdlib.h> #define OK 1 #define EROOR 0 #define OVERFLOW -1 #define stackincrement 10 #define stack_size 100 typedef struct { int *base; int *top; int stacksize; }Sqstack; //构造空栈 int Initstack (Sqstack &s) { s.base=(int *)malloc(stack_size*sizeof(int)); if(!s.base) exit(OVERFLOW); s.top=s.base; s.stacksize=stack_size; return OK; } //插入 int push(Sqstack &s,int e) { if(s.top-s.base>=s.stacksize) { s.base=(int *)realloc(s.base,(stackincrement+s.stacksize)*sizeof(int)); if(!s.base) exit(OVERFLOW); s.top=s.base+s.stacksize; s.stacksize+=stackincrement; } *s.top++=e; return OK; } //进制转换 void conversion() { int m,n; Sqstack s; Initstack(s); printf("请输入10进制数:\n"); scanf("%d",&m); printf("请输入转换进制:\n"); scanf("%d",&n); while(m) { push(s,m%n); m=m/n; } while(s.top!=s.base) { s.top--; if(*s.top<10) printf("%d",*s.top); else printf("%c",*s.top+55); } printf("\n"); printf("%d",*s.base); } void main() { conversion(); }