栈的应用数制转换问题,要求:键盘输入一个十进制数,输出对应的二级制数。如:65à1000001B(要求采用顺序栈或链栈实现,或者分别实现。)

#include<stdio.h>
#include<stdlib.h>
#define StackSize 50
typedef struct
{
    int a[StackSize];
    int top;
}SeqStack;

void InitStack(SeqStack *S)
{
    S->top = -1;
}

int Push(SeqStack *S, int x)
{
    if(S->top == StackSize-1)
    {
    printf("栈满\n");
    }
    else
    {
    S->top++;
    S->a[S->top] = x;
    }
    return 0;
}

int Pop(SeqStack *S, int *x)
{
    if(S->top == -1)
    {
        printf("空栈\n");
    }
    else
    {
        *x = S->a[S->top];
        S->top--;
    }
    return 0;
}

int GetTop(SeqStack *S, int *x)
{
    if(S->top == -1)
    {
        printf("栈为空");
    }
    else
    {
        *x = S->a[S->top];
        printf("%d",*x);
    }
    return 0;
}

int TTB(SeqStack S, int x)
{
    while(x > 0)
    {
      if(x%2 == 0)
      {
        Push(&S,0);
      }
      else
      {
        Push(&S,1);
      }
      x = x/2;
    }
    while(S.top > -1)
    {
        GetTop(&S, &x);
        S.top--;
    }
    return 0;
}

int main()
{
    SeqStack S;
    int j;
    InitStack(&S);
    scanf_s("%d", &j);
    TTB(S,j);
    system("pause");
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值