数据结构—栈的应- 数制的转换

//因为数值转换的最后的结果是逆序排列,故可以用堆来实现

//这里用静态的顺序栈来实现数制的转换

include<iostream>
#include<stdio.h>
using namespace std;

#define max_stacksize 200

typedef int ElemType;
typedef struct SqStack{
    ElemType Stack_arry[max_stacksize];
    int top;
}SqStack;


void init_stack(SqStack &s){         //初始化协议栈 
    s.top=-1;
}

int push(SqStack &s,int x){         //进栈操作 
    if(s.top>=max_stacksize-1){
        printf("栈满!");
    }
    else{
        s.Stack_arry[++s.top]=x;
    }
    return 0;
}

int pop(SqStack &s,int &x){        //退栈操作 
    if(s.top==-1){
        printf("栈空!");    
    }
    else{
        x=s.Stack_arry[s.top--]; 
    } 
    return x;
}

void conversion(int n,int d){  //将n进制的数转化为d进制的数
    SqStack s;
    init_stack(s);
    int b,e;
    while(n!=0){
        b=n%d;             //求余
        n=n/d;               //求商
        push(s,b);

    } 
    while(s.top!=-1){
      pop(s,e);
      printf("%d",e);
   }
}

int main(){
    int N,D;
    cin>>N>>D;
    conversion(N,D);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值