C++利用顺序栈实现将10进制数转换为16进制数

利用顺序栈来写进制转换比将数组逆序简单许多,本文章采用的栈定义为顺序栈定义

 

下面这个是头文件代码:

#include <iostream>  //C++头文件
#include "stdio.h"
#include "string.h"
using namespace std;
#define MAXLEN 100  //定义线性表的最大容量
typedef int elementType;

typedef struct sStack  //定义顺序栈结构
{
    elementType data[MAXLEN];
    int top;
}seqStack;

void initialStack(seqStack &S) //初始化栈
{
    S.top=-1;
}

bool pushStack(seqStack &S, elementType x) //入栈
{
    if(stackFull(S))
        return false;
    else
    {
        S.top++;
        S.data[S.top]=x;
        return true;
    }
}

bool popStack(seqStack &S, elementType &x)  //出栈
{
    if(stackEmpty(S))
        return false;
    else
    {
        x=S.data[S.top];
        S.top--;
        return true;
    }
}

void Change(int n) //转换函数
{
    seqStack S;
    int mod,x, i=0;
    char str[MAXLEN];
    initialStack(S);  //初始化栈

    while(n!=0)
    {
        mod=n%16;  //取余数
        pushStack(S,mod);  //将余数入栈
        n=n/16;  //更新被除后的数
    }

    while(!stackEmpty(S))  //出栈存入字符数组
    {
        popStack(S,x);
        if(x<10)
        {
            str[i]=x+'0';
        }
        else  //若弹出栈的数位于10~15之间的需要转换
        {
            str[i]=x+'A'-10;
        }
        i++;
    }
    str[i]='\0';

    cout<<"十六进制数为:";
    for(i=0;str[i]!='\0';i++)
    {
        cout<<str[i];
    }
}

 

下面的是主函数代码:

#include <iostream>
#include "seqStack.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
    int n;
    cout<<"输入要转换的十进制数:";
    cin>>n;
    Change(n);
}

 

 

  • 14
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以回答这个问题。以下是一个使用顺序栈实现十进制转换为十二进制、八进制和十六进制的C语言程序: ``` #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef struct { int top; int data[STACK_SIZE]; } Stack; void init(Stack *s) { s->top = -1; } int is_empty(Stack *s) { return s->top == -1; } int is_full(Stack *s) { return s->top == STACK_SIZE - 1; } void push(Stack *s, int x) { if (is_full(s)) { printf("Stack overflow\n"); exit(1); } s->data[++s->top] = x; } int pop(Stack *s) { if (is_empty(s)) { printf("Stack underflow\n"); exit(1); } return s->data[s->top--]; } void convert(int n, int base) { Stack s; init(&s); while (n > 0) { push(&s, n % base); n /= base; } while (!is_empty(&s)) { int digit = pop(&s); if (digit < 10) { printf("%d", digit); } else { printf("%c", digit - 10 + 'A'); } } } int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("Decimal to binary: "); convert(n, 2); printf("\n"); printf("Decimal to octal: "); convert(n, 8); printf("\n"); printf("Decimal to hexadecimal: "); convert(n, 16); printf("\n"); return 0; } ``` 这个程序使用了一个顺序栈实现进制转换。首先,我们定义了一个`Stack`结构体来表示栈,其中`top`表示栈顶的位置,`data`组用来存储栈中的元素。然后,我们定义了一些基本的栈操作,如`init`、`is_empty`、`is_full`、`push`和`pop`。这些操作都是比较常见的,不需要过多解释。 接下来,我们定义了一个`convert`函实现进制转换。这个函接受两个参一个十进制`n`和一个目标进制`base`。我们首先创建一个空栈`s`,然后使用一个循环将`n`转换为目标进制,并将每一位字压入栈中。最后,我们使用另一个循环从栈中弹出每一位字,并将其打印出来。如果字小于10,则直接打印字;否则,我们将其转换对应的大写字母并打印出来。 在`main`函中,我们首先读入一个十进制`n`,然后分别调用`convert`函将其转换为二进制、八进制和十六进制,并打印出来。 希望这个程序能够帮助你理解如何使用顺序栈实现进制转换

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圆圆小哆啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值