进制转换问题

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

输入

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

输出

转换后的二进制数。

样例输入

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;
}
原文地址:https://www.cnblogs.com/Lazy-Cat/p/9838443.html