利用栈的知识将十进制的数转换为r进制的数 c语言 数据结构 超详细~~

#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct node
{
Elemtype data;
struct node* next;
}node;
typedef struct stack
{
struct node* top;//栈顶指针
struct node* bottom;//栈底指针
}stack;
void initstack(stack*);//栈的初始化
void pushstack(stack*,int);//入栈
bool popstack(stack*,int*);//出栈
//出栈的第二个参数可以要,可以不要,如果想查看出栈的元素的数据的话
//就可以用指针先修改它的值,之后在其他地方也是可以访问的
bool empty(stack* );//判断栈是否为空
int main(void)
{
stack s;
int N,r,val;
printf(“请输入你想转换的数\n”);
scanf_s("%d" , &N);
printf(“请输入你想转换尾多少进制的数\n”);
scanf_s("%d", &r);
initstack(&s);
while (N)
{
pushstack(&s, N % r);
N = N / r;
}
while (!empty(&s))//当栈不为空的时候才进行出栈
{
popstack(&s, &val);
printf("%d\t", val);
}
printf(“计算完毕\n”);
system(“pause”);
return 0;
}
void initstack(stackps)
{
ps->top = (node
)malloc(sizeof(node));
if (!ps->top)
{
printf(“分配内存空间失败\n”);
exit(-1);
}
else
{
ps->bottom = ps->top;
ps->top->next = NULL;//一定要将ps的下一个节点
//也就是链表中的头节点的指针域指向尾空
//也可以这样写
//ps->bottom->next = NULL;
}
}
void pushstack(stackps,int val)
{
//由于入栈不需要判断栈满,所以不需要返回值进行标识
//先生成一个节点,再将数据域进行赋值,然后再进行指针的连接
//再将栈的栈顶指针指向这个新生成的节点
node
p = (node*)malloc(sizeof(node));
if (!p)
{
printf(“分配内存空间失败\n”);
return;
}
else
{
p->data = val;
p->next = ps->top;
ps->top = p;
}
}
bool empty(stack* ps)
{
if (ps->bottom == ps->top)
{
return true;
}
else
{
return false;
}
}
bool popstack(stackps, intval)
{//将出栈元素的值保存在val这个变量中
if (!empty(ps))//这个地方就不用传入ps的地址了,因为ps本身就是一个指针
{
//如果栈不为空
node* p = ps->top;
*val = p->data;
ps->top = p->next;
free§;
p = NULL;
return true;
}
else
{
return false;
}
}

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该问题可以通过以下步骤实现: 1. 定义顺序数据结构和相关操作,包括初始化、入、出空、满等。 2. 将输入的非负十进制数不断除以指定数,将余数依次压入中。 3. 当商为0时,停止循环。 4. 从顶开始输出元素,即为指定下的数字表示。 以下是示例代码实现: ```c #include <stdio.h> #include <stdbool.h> #define STACK_SIZE 100 // 定义的最大大小 typedef struct { int top; // 顶指针 int data[STACK_SIZE]; } SeqStack; // 定义顺序结构体 // 初始化 void InitStack(SeqStack* stack) { stack->top = -1; } // 判断是否为空 bool StackEmpty(SeqStack* stack) { return stack->top == -1; } // 判断是否已满 bool StackFull(SeqStack* stack) { return stack->top == STACK_SIZE - 1; } // 入操作 bool Push(SeqStack* stack, int x) { if (StackFull(stack)) { return false; } else { stack->top++; stack->data[stack->top] = x; return true; } } // 出操作 bool Pop(SeqStack* stack, int* x) { if (StackEmpty(stack)) { return false; } else { *x = stack->data[stack->top]; stack->top--; return true; } } // 将一个非负十进制转换为指定数,并输出结果 void conversion(int dec, int radix) { SeqStack stack; int remainder; InitStack(&stack); while (dec > 0) { remainder = dec % radix; Push(&stack, remainder); dec /= radix; } while (!StackEmpty(&stack)) { Pop(&stack, &remainder); printf("%d", remainder); } printf("\n"); } int main() { int dec, radix; printf("请输入一个非负十进制数和要转换数(2-16):"); scanf("%d%d", &dec, &radix); if (radix < 2 || radix > 16) { printf("数无效!\n"); return 0; } printf("转换结果为:"); conversion(dec, radix); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值