首页 > 其他 > 详细

进制转换问题

时间:2018-10-23 19:52:06      阅读:121      评论:0      收藏:0      [点我收藏+]
建立顺序栈或链栈,编写程序实现十进制数到二进制数的转换。

输入

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

输出

转换后的二进制数。

样例输入

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!