栈 —— 顺序栈 —— 二进制转换成十进制

这里写图片描述

代码示例

/*
    function:sequence stack
    created by : xilong
    date: 2017.2.7
*/

#include "iostream"
#include <stdlib.h>
#include <math.h>
using namespace std;

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 20
#define SARCKINCREMENT 10

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

/*
    function: initialize the stack
*/
void Stack_Init(sqStack *s)
{
    s->base = (Elemtype *)malloc(STACK_INIT_SIZE * sizeof(Elemtype));
    if (!s->base)
    {
        exit(0);
    }
    s->top = s->base;  // at the very begining, the top of the stack is as same as the base of the stack
    s->stackSize = STACK_INIT_SIZE;   // size of the stack
}

/*
    function: push an element "e" into the top of the stack
*/
int Stack_Push(sqStack *s, Elemtype e)
{
    if (s->top - s->base >= s->stackSize)
    {
        s->base = (Elemtype *)realloc(s->base, (s->stackSize + SARCKINCREMENT) * sizeof(Elemtype));
        if (!s->base)
        {
            exit(0);
        }
        s->top = s->base + s->stackSize;  // set the top of the stack because of using the function "realloc"(realloc==>malloc, copy)
        s->stackSize = s->stackSize + SARCKINCREMENT;  // set the maximal size of the stack
    }
    *(s->top) = e;
    s->top++;
}

/*
    function: pop the top element
*/
Status Stack_Pop(sqStack *s, Elemtype *e)
{
    if (s->top == s->base)  // empty stack
    {
        return ERROR;
    }
    *e = *--(s->top); // move the pointer top down first, then take away the top element
    return OK;
}

/*
    function: clear the stack
*/
Status Stack_Clear(sqStack *s)
{
    s->base = s->top;
    return OK;
}
/*
    function: destory the stack
*/
Status Stack_Destory(sqStack *s)
{
    int i;
    for (i = 0; i < s->stackSize; i++)
    {
        free(s->base);
        s->base++;
    }
    s->base = s->top = NULL;
    s->stackSize = 0;
    return OK;
}
/*
    function: return the length of the stack
*/
int Stack_Length(sqStack s)
{
    return(s.top - s.base);
}

void main()
{
    Elemtype c;
    sqStack s;
    int len, i, sum = 0;
    Stack_Init(&s);
    cout << "请输入二进制数,输入#符号表示结束!" << endl;
    scanf_s("%c", &c);
    while (c != '#')
    {
        Stack_Push(&s, c);
        scanf_s("%c", &c);
    }
    getchar();
    cout << "打印当前容量:";
    len = Stack_Length(s);
    cout << len << endl;
    cout << "出栈顺序为:";
    for (i = 0; i < len; i++)
    {
        Stack_Pop(&s, &c);
        cout << c << " ";
        sum = sum + (c - 48) * pow(2, i);
    }
    cout << endl;
    cout << "二进制转换成十进制为:";
    cout << sum << endl;


    system("pause");

}

程序截图

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值