#include<malloc.h> #include<stdio.h> #include<stdlib.h> #define STACK_INT_SIZE 100 #define STACKINCREMENT 10 int N, R; typedef int SElemType; typedef bool Status; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack &s) { //构建一个空栈 s.base = (SElemType *)malloc(STACK_INT_SIZE * sizeof(SElemType)); if (!s.base)exit(0); s.top = s.base; s.stacksize = STACK_INT_SIZE; return 1; } Status GetTop(SqStack &s, SElemType &e) { if (s.top ==s.base) return 0; e = *(s.top - 1); return 1; } Status Push(SqStack &s, SElemType e) { if (s.top - s.base >= s.stacksize) { s.base = (SElemType *)realloc(s.base, (s.stacksize + STACKINCREMENT) * sizeof(SElemType)); if (!s.base) exit(0); s.top = s.base + s.stacksize; s.stacksize += STACKINCREMENT; } *s.top++= e;
return 1;} Status Pop(SqStack &s, SElemType &e) { if (s.top==s.base) return 0; e = *–s.top; return 1; } Status StackEmpty(SqStack &s) { if (s.top == s.base) return 1; return 0; } void conversion(SqStack &S, int R, int N, int &e) { int b; while (N) { b = N % R; Push(S, b); N = N / R;
} while (!StackEmpty(S)) { Pop(S, e); if (e < 10) { printf("%d", e); } else { e = e + 55; printf("%c\n", e); } } printf("\n");}
int main() { int e, p, q; SqStack S; InitStack(S); printf(" 请输入十进制整数 N:"); scanf_s("%d", &N); printf(" 请输入将要转换为的进制数 R:"); scanf_s("%d", &R); conversion(S,R,N,e); system(“pause”); }
