69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
typedef struct conversion {
double data;
struct conversion *next;
}conver;
void pushit(conver *top, int data); //入栈
void outit(conver *top); //出栈
void integer_translate(int Integer, conver *top, int Base);
void decimal_translate(double Decimal, int Base);
int main() {
conver *TOP1 = NULL; //头节点,分别对应整数和小数
TOP1 = (conver *)malloc(sizeof(TOP1));
TOP1->data = NULL;
TOP1->next = NULL;
double data, decimal;
int integer, base;
printf("请输入数字:");
scanf("%lf", &data); //读入数据
integer = (int)data; //整数部分
decimal = data - integer; //小数部分
printf("数字读入完成!\n");
printf("请输入需要转换的进制数(小写整数):");
scanf("%d", &base); //读入进制数
integer_translate(integer, TOP1, base); //整数进制转换
printf(".");
decimal_translate(decimal, base);
getchar();
return 0;
}
void pushit(conver *top, int data) { //入栈
conver *node = (conver *)malloc(sizeof(node));
node->next = NULL;
node->data = data;
node->next = top->next;
top->next = node;
node = NULL;
}
void outit(conver *top) { //出栈
conver *node = NULL;
node = top->next;
printf("%d", (int)node->data);
top->next = node->next;
}
void integer_translate(int Integer, conver *top, int Base) { //整数进制转换
while (Integer > 0) {
pushit(top, Integer%Base);
Integer /= Base;
}
while (top->next != NULL) {
outit(top);
}
}
void decimal_translate(double Decimal, int Base) { //进制转换
int i = 0;
while (Decimal > 0 && i <= 10) {
Decimal *= Base;
printf("%d", (int)Decimal);
if (Decimal >= 1)
Decimal -= (int)Decimal;
i++;
}
}
conver *node = (conver *)malloc(sizeof(node));
conver *node = (conver *)malloc(sizeof(*node));
conver *node = (conver *)malloc(sizeof(conver));