利用栈 转换进制 碰到 大于 10的进制 需要在进栈元素处做相应的处理
/*** // 数制 转换问题 所得商 入栈 计算完毕后 出栈 即为所得 InitStack ( &S) : 栈的初始化 Push ( &S,e) :进栈 Pop (&S ) : 出栈 GetTop (S ): 取栈顶元素 IsEmpty (S): 判栈空否 ***/ #include <stdio.h> #include <stdlib.h> #define STACK_MAX 1000 typedef int ElemType; typedef struct Stack { int top; ElemType elem[STACK_MAX]; }Stack,*Pstack; void InitStack (Pstack S) ; // 栈的初始化 void Push (Pstack S,ElemType elem) ; // 进栈 ElemType Pop (Pstack S ) ; // 出栈 ElemType GetTop (Pstack S ); // 取栈顶元素 bool IsEmpty (Pstack S); // 判栈空否 void main() { int num = 0; int binary = 0; Stack stack; Pstack pstack = &stack; InitStack(pstack); printf("输入十进制数字:"); scanf("%d",&num); printf("输入转换成的进制:"); scanf("%d",&binary); while (num != 0) { Push(pstack,num%binary); num = num/binary; } while (!IsEmpty(pstack)) { printf("%d",Pop(pstack)); } printf("\n"); } void InitStack (Pstack S) // 栈的初始化 { S->top = 0; for (int i = 0; i < STACK_MAX; i++) S->elem[i] = 0; } void Push (Pstack S,ElemType elem) // 进栈 { if (S->top != STACK_MAX) // 判断是否栈满 S->elem[(S->top)++] = elem; } ElemType Pop (Pstack S ) // 栈顶元素出栈 { return S->elem[-- (S->top)]; } ElemType GetTop (Pstack S ) // 取栈顶元素 { if (!IsEmpty(S)) return S->elem[S->top-1]; else printf("栈空~~\n"); } bool IsEmpty (Pstack S) // 判栈空否 { return (S->top == 0); }原文链接:https://www.f2er.com/datastructure/383102.html