十进制整型(int)数转换为二进制数

//头文件   *.h
//.........................................................
#include <iostream>
using namespace std;

typedef struct zhan
{
    int data;
    struct zhan *next;
}zhan;

typedef struct
{
    struct zhan *first;
    struct zhan *last;
}pointer;

//构造一个空栈
void Creat_zhan(pointer &Z)
{
    Z.first = Z.last = (zhan *)malloc(sizeof(zhan));
    if (Z.first == NULL) exit(0);
    Z.first->next = NULL;
}

//进栈
void Push(pointer &Z, int e)
{
    zhan *p = Z.first;
    p->data = e;
    Z.first = (zhan *)malloc(sizeof(zhan));
    if (Z.first == NULL) exit(0);
    Z.first->next = p;
}

//出栈
void Pop(pointer &Z, int &e)
{
    zhan *q = Z.first;
    e = (q->next)->data;
    Z.first = Z.first->next;
    free(q);
}

//栈空判断
int zhan_emp(pointer &Z)
{
    if (Z.first == Z.last) return 0;
    else return 1;
}
//.........................................................`


//源文件     *.cpp
//.........................................................
#include "Zhan.h"
#include <Windows.h>


void input_data(void)
{
    int a, k;
    pointer Z;
    Creat_zhan(Z);
    cout << "请输入一个十进制整数:";
    cin >> a;
    do
    {
        k = a % 2;
        a = a / 2;
        Push(Z, k);
    } while (a);
    while (zhan_emp(Z))
    {
        Pop(Z, k);
        cout << k;
    }
    cout << endl;
}

int main(void)
{
    input_data();
    system("pause");
    return 0;
}
//.........................................................

猜你喜欢

转载自blog.csdn.net/qinghong_xiao/article/details/51597716