数据结构 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<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 ;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值