栈的实例分析—进制转换

 

 

 

/*
1.会对它进行修改的就以指针的形式传进来,eg:void Push(sqStack *s,ElemType *e)   、void Pop(sqStack *s,ElemType *e)
2.不会修改的只需要传数据就可以了,   eg:int StackLen(sqStack s)
3.指针用箭头:s->base
4.不是指针用.       :s.top
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define STACK_INIT_SIZE  20    //存储栈的长度,栈大小
#define STACKINCREMENT   10    //当栈空间不够时,每次增加的幅度

typedef char ElemType;
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stackSize;
} sqStack;

void InitStack(sqStack *s)
{
    s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if(!s->base)
    {
        exit(0);
    }

    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}

//压栈操作
void Push(sqStack *s,ElemType e)
{
    if(s->top-s->base>=s->stackSize)
    {
        s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT) * sizeof(ElemType));
        if(!s->base)
        {
            exit(0);
        }
    }
    *(s->top) = e;      //把数据e压入栈顶指针指向的地址
    s->top++;
}

//出栈操作
void Pop(sqStack *s,ElemType *e)
{
    if(s->top==s->base)
    {
        return;
    }
    *e = *--(s->top);   //先减再把值赋给e
}

int StackLen(sqStack s)
{
    return (s.top - s.base);
}

int main()
{
    ElemType c;
    sqStack s;
    int sum = 0;
    int len;

    InitStack(&s);
    printf("请输入二进制数,输入#符号表示结束!\n");
    scanf("%c",&c);
    while(c!='#')
    {
        Push(&s,c);
        scanf("%c", &c);
    }
    getchar();      //输入完成后会有回车\n,getchar把\n拿出来,键盘缓冲区就空了

    len = StackLen(s);
    printf("栈的当前容量:%d\n", len);

    for (int i = 0; i < len;i++)
    {
        Pop(&s, &c);
        //0的ASCII码是48,1的ASCII码是49
        sum = sum + (c - 48) * pow(2, i);
    }
    printf("二进制转化为十进制是:%d", sum);
    
    return 0;
}
二进制转换为十进制
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <math.h>
  4 
  5 #define STACK_INIT_SIZE  20    //存储栈的长度,栈大小
  6 #define STACKINCREMENT   10    //当栈空间不够时,每次增加的幅度
  7 
  8 typedef char ElemType;
  9 typedef struct
 10 {
 11     ElemType *base;
 12     ElemType *top;
 13     int stackSize;
 14 } sqStack;
 15 
 16 void InitStack(sqStack *s)
 17 {
 18     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
 19     if(!s->base)
 20     {
 21         exit(0);
 22     }
 23 
 24     s->top = s->base;
 25     s->stackSize = STACK_INIT_SIZE;
 26 }
 27 
 28 //压栈操作
 29 void Push(sqStack *s,ElemType e)
 30 {
 31     if(s->top-s->base>=s->stackSize)
 32     {
 33         s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT) * sizeof(ElemType));
 34         if(!s->base)
 35         {
 36             exit(0);
 37         }
 38     }
 39     *(s->top) = e;      //把数据e压入栈顶指针指向的地址
 40     s->top++;
 41 }
 42 
 43 //出栈操作
 44 void Pop(sqStack *s,ElemType *e)
 45 {
 46     if(s->top==s->base)
 47     {
 48         return;
 49     }
 50     *e = *--(s->top);   //先减再把值赋给e
 51 }
 52 
 53 int StackLen(sqStack s)
 54 {
 55     return (s.top - s.base);
 56 }
 57 
 58 int main()
 59 {
 60     ElemType c;
 61     sqStack s;
 62     int sum = 0;
 63     int oct=0;
 64     int len;
 65     int j = 0;
 66     int k = 0;
 67 
 68     InitStack(&s);
 69     printf("请输入二进制数,输入#符号表示结束!\n");
 70     scanf("%c",&c);
 71     while(c!='#')
 72     {
 73         Push(&s,c);
 74         scanf("%c", &c);
 75     }
 76     getchar();      //输入完成后会有回车\n,getchar把\n拿出来,键盘缓冲区就空了
 77 
 78     len = StackLen(s);
 79     printf("栈的当前容量:%d\n", len);
 80 //方法一:
 81     // for (int i = 1; i < len+1;i++)
 82     // {
 83     //     Pop(&s, &c);
 84     //     //0的ASCII码是48,1的ASCII码是49
 85     //     sum = sum + (c - 48) * pow(2, (i-1)%3);
 86     //     if(i%3==0 || i == len)
 87     //     {
 88     //         oct = oct+ sum * pow(10, j);
 89     //         j++;
 90     //         sum = 0;
 91     //     }
 92 
 93     // }
 94 
 95 //方法二:
 96     for (int i = 0; i < len;)
 97     {
 98         for (int j = 0; j < 3 && i<len;j++,i++)
 99         {
100              Pop(&s, &c);
101             //0的ASCII码是48,1的ASCII码是49
102             sum = sum + (c - 48) * pow(2, j);
103         }
104         oct = oct+ sum * pow(10, k++);
105         sum = 0;
106     }
107     printf("二进制转化为八进制是:%d", oct);
108     
109     return 0;
110 }
二进制转换为八进制

 

posted @ 2020-08-23 19:46  wind_y  阅读(230)  评论(0编辑  收藏  举报