kiki有一个十进制数,他想转成r进制,请你编程帮他实现。C语言
时间: 2024-06-13 19:07:41 浏览: 101
以下是C语言实现将十进制数转换为r进制的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct {
int *base;
int *top;
int stacksize;
} SqStack;
SqStack *Init_seqstack() {
SqStack *s = (SqStack *) malloc(sizeof(SqStack));
s->base = (int *) malloc(STACK_INIT_SIZE * sizeof(int));
if (!s->base) {
exit(0);
}
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return s;
}
void Push_seqstack(SqStack *s, int e) {
if (s->top - s->base >= s->stacksize) {
s->base = (int *) realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(int));
if (!s->base) {
exit(0);
}
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*(s->top) = e;
s->top++;
}
int Pop_seqstack(SqStack *s) {
int e;
if (s->top == s->base) {
return -1;
}
s->top--;
e = *(s->top);
return e;
}
void Conversion(int num, int r) {
SqStack *s = Init_seqstack();
int sum = 0;
while (num) {
sum = num % r;
Push_seqstack(s, sum);
num = num / r;
}
while (s->top != s->base) {
int e = Pop_seqstack(s);
if (e >= 10) {
printf("%c ", e + 55);
} else {
printf("%d ", e);
}
}
printf("\n");
}
int main() {
int num, r;
scanf("%d%d", &num, &r);
Conversion(num, r);
return 0;
}
```
阅读全文