zoukankan      html  css  js  c++  java
  • 进制转换问题

    建立顺序栈或链栈,编写程序实现十进制数到二进制数的转换。

    输入

    输入只有一行,就是十进制整数。

    输出

    转换后的二进制数。

    样例输入

    10

    样例输出

    1010
    #include <iostream>
    #include<stdlib.h>
    #define Max_Size 100
    #define STACKINCRMENT 10
    using namespace std;
    typedef int Elemtype;
    typedef struct SqStack
    {
        Elemtype *base;
        Elemtype *top;
        int stacksize;
    }SqStack;
    void InitStack(SqStack &S)
    {
        S.base = (Elemtype*)malloc(sizeof(Elemtype)*Max_Size);
        S.top = S.base;
        S.stacksize = Max_Size;
    }
    void Push(SqStack &S, Elemtype e)
    {
        if (S.top - S.base >= S.stacksize)
        {
            S.base = (Elemtype*)realloc(S.base, (S.stacksize + STACKINCRMENT) * sizeof(Elemtype));
            S.top = S.base + S.stacksize;
            S.stacksize += STACKINCRMENT;
        }
        *(S.top) = e;
        S.top++;
    }
    void Pop(SqStack &S, Elemtype &e)
    {
        if (S.top == S.base)
        {
            return;
        }
        S.top--;
        e = *(S.top);
    }
    void conversation(SqStack &S)
    {
        int count, radix;
        InitStack(S);
        cin >> count;
        while (count)
        {
            Push(S, count % 2);
            count = count / 2;
        }
        while (S.top != S.base)
        {
            Pop(S, count);
            cout << count;
        }
    }
    int main()
    {
        SqStack S;
        conversation(S);
        return 0;
    }
  • 相关阅读:
    C语言考点例题解析
    五笔打字
    常用快捷键
    网络基础知识
    人口增长
    8 封装
    9 绑定方法和非绑定方法
    6 抽象类
    7 多态和多态性
    5 组合
  • 原文地址:https://www.cnblogs.com/Lazy-Cat/p/9838443.html
Copyright © 2011-2022 走看看