数据结构 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<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include <sys/io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */#include<pthread.h> /* 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<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include <sys/io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */#include<pthread.h> /* 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;i< array_index ;i++){printf("%d\n", array[i]);} return 0 ;
}

再来一个有栈的版本

根据上面的分析
基本功能 都完成了,
现在 只需要关注 怎么实现栈本身了

怎么用程序描述一个栈呢。

栈的特点,先进后出。
栈是一个存储结构。
需要分配内存空间
栈可以存放整形,结构体,字符串等基本 元素
需要先定义一个基本的元素 ElemType ;
typedef int ElemType; /* 定义栈元素类型为整型 */

一个元素入栈的时候,需要一个指针指向要存放的地址。
入栈的元素 都放在 栈顶。
这个栈顶指针 指向的是元素,定义为
ElemType * top ;

栈需要一个基地址,是栈底。
ElemType *base ;

栈是一个存储空间,一般都有一个大小。
表示他的存储能力,存放了多少个元素。

int stack_size ;

简易有栈版本:

/* c1.h (程序名) */
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include <sys/io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */#include<pthread.h> /* 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 /* 存储空间分配增量 */typedef struct SqStack{ElemType *base ;ElemType *top ;int stack_size ;}SqStack;Status init_stack(SqStack *S)
{S->base = (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 < S->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< array_index ;i++){printf("%d\n", array[i]);} while(1){st = pop_stack(&S, &e);if(st ==OK){printf("e = %d\n",e);}else{break ;}}return 0 ;
}

数据结构 study 7: 栈 实现 十进制 转换为 8进制相关推荐

  1. 2131数据结构实验之栈与队列一:进制转换

    数据结构实验之栈与队列一:进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制非负整数,将其转换成对 ...

  2. c++语言将任意进制转化10进制,C++ 基础编程之十进制转换为任意进制及操作符重载...

    C++ 基础编程之十进制转换为任意进制及操作符重载 最近学习C++ 的基础知识,完成十进制转换为任意进制及操作符重载,在网上找的不错的资料,这里记录下, 实例代码: #include #include ...

  3. 十进制转换为32进制,并反转

    1 #region 十进制转换为32进制,并反转 2 ///<summary> 3 /// 十进制转换为32进制 4 ///</summary> 5 ///<param ...

  4. 获取时间戳,从十进制转换为16进制(4字节)

    获取时间戳,从十进制转换为16进制(4字节) #include <stdlib.h> #include <stdio.h> #include <string.h> ...

  5. **c语言八进制转换为十进制or十进制转换为8进制**

    c语言八进制转换为十进制or十进制转换为8进制 1.c语言八进制转换为十进制 ```c #include<stdio.h> int main() { char *p,s[20];int n ...

  6. 关于java实现十进制转换为任意进制

    这原本是Noip2000的一个题目 https://www.luogu.com.cn/problem/P1017 被我们的伟大的唯心主义编程大师拿来当成了作业题,我正好复习一下进制问题 首先十进制转换 ...

  7. 进制转换:十进制转换为任意进制、任意进制转换为十进制

    目录 十进制转换为任意进制 任意进制转换为十进制 十进制转换为任意进制 //将10进制数num转换为d进制数 void convert_base(int num, int d) {int k;stac ...

  8. 将十进制转换为8进制并输出

    编写程序,将十进制转换为8进制并输出(不允许使用printf("%o")) 代码: #include <stdio.h>//编写程序,将十进制转换为8进制并输出(不允许 ...

  9. Java中十进制转换为其他进制的方法

    package com.hehe.domain;public class NumberDemo {public static void main(String[] args) {//十进制20转换为二 ...

最新文章

  1. 【数据挖掘】聚类算法 简介 ( 基于划分的聚类方法 | 基于层次的聚类方法 | 基于密度的聚类方法 | 基于方格的聚类方法 | 基于模型的聚类方法 )
  2. python一元三次方程拟合_一元三次方程的求根公式
  3. 20130418代码
  4. MySQL视图一次踩坑经历
  5. CAD如何绘制带有弧形的箭头
  6. 固态硬盘重装系统后进入老系统找东西
  7. 计算电路门数和nand2的面积
  8. Online Adaptation of Convolutional Neural Networks for Video Object Segmentation论文阅读
  9. 联想微型计算机怎么恢复系统,联想一体机系统还原的方法 联想一体机如何还原系统...
  10. oracle存储过程报ORA-20000的错误
  11. IDM6.32的安装与激活IDM Crack 6.32 Build 8 + Patch 2019 free (100% working)
  12. IBM期望通过牺牲连通性以完成量子计算的规模化; QQCI宣布成立QUBT大学 | 全球量子科技与工业快讯第三十二期
  13. 80后年薪多少,才能摆脱中年危机?
  14. 读取本地文件转化成MultipartFile
  15. 如何让win XP实现自动登陆
  16. 广告dsp,ssp,adx
  17. 基于Winform开发S7.net协议 与Smart-200PLC通讯
  18. 质子之死:粒子衰变如何推出万有理论
  19. ORA-12034: materialized view log on SCOTT.T_ROWID younger than last refresh
  20. thinkphp 点击分类显示分类下的文章(完整)

热门文章

  1. 总结后端开发常见安全问题及应对方案
  2. 小游戏毕设 - 炸弹人小游戏设计与实现 (源码)
  3. empirecms php 很慢,关于帝国cms提高网站网页打开速度的方法
  4. 如何在Mac上关闭Siri?
  5. jmeter压测步骤
  6. 怎样将一个dataframe存入csv文件,不带行标
  7. 华为手机删掉热门推荐
  8. 军校空军士官计算机专业,军校分不够,没关系!18所士官学校报考名单来袭!...
  9. MODBUS RTU调试助手
  10. np.random.normal