#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef struct Stack {
int top;
int data[MAX_STACK_SIZE];
} Stack;
void push(Stack* stack, int item) {
stack->data[++stack->top] = item;
}
int pop(Stack* stack) {
return stack->data[stack->top--];
}
void initStack(Stack* stack) {
stack->top = -1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
void decimalToOtherBase(int decimal, int base) {
Stack stack;
initStack(&stack);
int i = 0;
while (decimal > 0) {
int remainder = decimal % base;
push(&stack, remainder);
decimal /= base;
}
while (!isEmpty(&stack)) {
printf("%d", pop(&stack));
if (i++ % base == 0) {
printf(" "); // 每输出四位数后换行
}
}
printf("\n"); // 换行输出结果
}
int main() {
int decimal, base;
printf("输入想要转换的数字: ");
scanf("%d", &decimal);
printf("输入想要的进制数: ");
scanf("%d", &base);
decimalToOtherBase(decimal, base);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
typedef struct Stack {
int top;
int data[MAX_STACK_SIZE];
} Stack;
void push(Stack* stack, int item) {
stack->data[++stack->top] = item;
}
int pop(Stack* stack) {
return stack->data[stack->top--];
}
void initStack(Stack* stack) {
stack->top = -1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
void decimalToOtherBase(int decimal, int base) {
Stack stack;
initStack(&stack);
int i = 0;
while (decimal > 0) {
int remainder = decimal % base;
push(&stack, remainder);
decimal /= base;
}
while (!isEmpty(&stack)) {
printf("%d", pop(&stack));
if (i++ % base == 0) {
printf(" "); // 每输出四位数后换行
}
}
printf("\n"); // 换行输出结果
}
int main() {
int decimal, base;
printf("输入想要转换的数字: ");
scanf("%d", &decimal);
printf("输入想要的进制数: ");
scanf("%d", &base);
decimalToOtherBase(decimal, base);
return 0;
}