数据结构 study 7: 栈 实现 十进制 转换为 8进制 |
您所在的位置:网站首页 › 十进制整数转换为八进制 › 数据结构 study 7: 栈 实现 十进制 转换为 8进制 |
数据结构 study 6: 栈 实现 十进制 转换为 8进制 先用文字和图片 ,描述清楚这个问题。 然后自己通过代码实现。 代码纯手工写。 文字描述 达到 只要看完文字描述,就知道怎么写代码 题目描述十进制数据 123,分别获取它的 百分位,十分位和个位的数字 步骤1: 123 % 10 = 3 步骤2: 123 / 10 = 12 12 %10 = 2 步骤3: 123/10 =12 12/10 = 1 1%10 = 1 3 第一个进去, 2 第二个进去, 1第三个进去 需要打印出来 123 这个字符串. 先进后厨,后进先出. 栈 打印1,打印2 打印3 看到 123 这个字符串在屏幕上面 十进制数据1348 ,转换为8进制数据1348 = 168x8 + 4 = 1344 +4 1348 % 8 = 4 1348/8 = 168 168 = 21*8 168 % 8 = 0 (1348/8)/8 = 21 21 % 8 = 5 ((1348/8)/8)/8 = 2 2%8 = 2 typedef struct SqStack { SElemType *base; /* 在栈构造之前和销毁之后,base的值为NULL */ SElemType *top; /* 栈顶指针 */ int stacksize; /* 当前已分配的存储空间,以元素为单位 */ }SqStack; /* 顺序栈 */top指针指向的是,下一个入栈的元素存放的位置,当前是一个空位置。 top指针指向的是,最新插入元素的 上面的位置 题目分解: (1)需要一个输入十进制数据的函数: scanf(); (2)对十进制 取余的过程,临时数据 放到 栈中 (3)从栈中取出数据,逐个打印。 (4)说到栈,需要实现一个栈。 (5)栈的特性 (6)出栈,入栈 基本操作 信息基本足够了,先不考虑特殊情况。 先实现再优化。 用到的头文件。 /* c1.h (程序名) */ #include #include #include /* malloc()等 */ #include /* INT_MAX等 */ #include /* EOF(=^Z或F6),NULL */ #include /* atoi() */ #include /* eof() */ #include /* floor(),ceil(),abs() */ #include /* exit() */ /* 函数结果状态代码 */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 /* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */ typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */ typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */ typedef int ElemType; /* 定义栈元素类型为整型 */ /* c3-1.h 栈的顺序存储表示 */ #define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */ #define STACKINCREMENT 2 /* 存储空间分配增量 */如果有折叠功能就好了, 可以把下面的参考代码给折叠起来。 先来一个没有 栈的普通版本: /* c1.h (程序名) */ #include #include #include /* malloc()等 */ #include /* INT_MAX等 */ #include /* EOF(=^Z或F6),NULL */ #include /* atoi() */ #include /* eof() */ #include /* floor(),ceil(),abs() */ #include /* exit() */ /* 函数结果状态代码 */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 /* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */ typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */ typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */ typedef int ElemType; /* 定义栈元素类型为整型 */ /* c3-1.h 栈的顺序存储表示 */ #define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */ #define STACKINCREMENT 2 /* 存储空间分配增量 */ int main() { unsigned int data ; unsigned int num ; unsigned int div = 0 ; unsigned int array[10] = {0}; int array_index =0 ; int i ; printf("请输入一个非负数的十进制数:(eg:1348 对应八进制2504)\n"); scanf("%u",&data); printf("data = %u\n", data); while(data){ array[array_index] = data%8; array_index ++ ; data = data/8; } for(i=0;ibase = (ElemType *) malloc(sizeof(ElemType) * STACK_INIT_SIZE); S->top = S->base ; S->stack_size = STACK_INIT_SIZE ; return OK ; } Status push_stack(SqStack *S, ElemType e) { if( S->top - S->base stack_size){ *(S->top) = e ; S->top ++ ; }else{ return ERROR ; } return OK ; } Status pop_stack(SqStack *S, ElemType * e) { if(S->top != S->base){ *e =*(--S->top); }else{ return ERROR ; } return OK ; } int main() { unsigned int data ; unsigned int num ; unsigned int div = 0 ; unsigned int array[10] = {0}; int array_index =0 ; int i ; SqStack S ; ElemType e ; Status st =OK; init_stack(&S); printf("请输入一个非负数的十进制数:(eg:1348 对应八进制2504)\n"); scanf("%u",&data); printf("data = %u\n", data); while(data){ array[array_index] = data%8; array_index ++ ; push_stack(&S, data%8); data = data/8; } for(i=0;i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |