数组的方式实现--栈 数制转换

例子:清华大学数据结构C语言版 P48

十进制数N和其他d进制数的转换: N = (N div d)*d + N mod d; 其中,div为整除运算,mod为求余运算。

 1 #define _CRT_SECURE_NO_DEPRECATE  /*取消scanf,printf不安全之类的错误提示*/
 2 
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 typedef int Item;
 6 Item *As;
 7 /******数组的方式实现--栈******/
 8 static int N;   //栈的数组下标
 9 void ArryStackInit( int maxN)
10 {
11     As = (int *)malloc(maxN * sizeof(Item));
12     N = 0;
13 }
14 int If_ArryStackEmpty()
15 {
16     return N;
17 }
18 void ArryStackPush(Item item)
19 {
20     As[N++] = item;
21 }
22 Item ArryStackPop( )
23 {
24     return As[--N];
25 }
26 /******************************/
27 int main()
28 {
29     int num,num8;
30     ArryStackInit(10);
31     scanf("%d", &num);
32     while (num){
33         ArryStackPush(num % 8);
34         num = num / 8;
35     }
36     while (N)
37     {
38         num8 = ArryStackPop(As);
39         printf("%d", num8);
40     }
41 }
原文地址:https://www.cnblogs.com/mrethan/p/4118775.html