您的位置:首页 > 其它

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

2014-11-24 15:23 190 查看
例子:清华大学数据结构C语言版 P48

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

#define _CRT_SECURE_NO_DEPRECATE  /*取消scanf,printf不安全之类的错误提示*/

#include <stdio.h>
#include <stdlib.h>
typedef int Item;
Item *As;
/******数组的方式实现--栈******/
static int N;   //栈的数组下标
void ArryStackInit( int maxN)
{
As = (int *)malloc(maxN * sizeof(Item));
N = 0;
}
int If_ArryStackEmpty()
{
return N;
}
void ArryStackPush(Item item)
{
As[N++] = item;
}
Item ArryStackPop( )
{
return As[--N];
}
/******************************/
int main()
{
int num,num8;
ArryStackInit(10);
scanf("%d", &num);
while (num){
ArryStackPush(num % 8);
num = num / 8;
}
while (N)
{
num8 = ArryStackPop(As);
printf("%d", num8);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: