无套路,均已上机通过,求个关注求个赞,提供答疑解惑服务。
实现十进制二进制小数整数之间的转换
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
// 定义栈结构
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
// 定义队列结构
typedef struct {
int *items;
int front, rear, size;
} Queue;
// 初始化栈
void initializeStack(Stack *stack) {
stack->top = -1;
}
// 初始化队列
void initializeQueue(Queue *queue, int size) {
queue->items = (int *)malloc(sizeof(int) * size);
queue->front = -1;
queue->rear = -1;
queue->size = size;
}
// 判断栈是否为空
int isStackEmpty(Stack *stack) {
return stack->top == -1;
}
// 判断队列是否为空
int isQueueEmpty(Queue *queue) {
return queue->front == -1;
}
// 入栈操作
void push(Stack *stack, int item) {
if (stack->top == MAX_SIZE - 1) {
printf("栈溢出\n");
exit(EXIT_FAILURE);
}
stack->items[++stack->top] = item;
}
// 出栈操作
int pop(Stack *stack) {
if (isStackEmpty(stack)) {
printf("栈下溢\n");
exit(EXIT_FAILURE);
}
return stack->items[stack->top--];
}
// 入队操作
void enqueue(Queue *queue, int item) {
if ((queue->rear + 1) % queue->size == queue->front) {
printf("队列已满\n");
exit(EXIT_FAILURE);
}
if (isQueueEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % queue->size;
queue->items[queue->rear] = item;
}
// 出队操作
int dequeue(Queue *queue) {
if (isQueueEmpty(queue)) {
printf("队列下溢\n");
exit(EXIT_FAILURE);
}
int item = queue->items[queue->front];
if (queue->front == queue->rear) {
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % queue->size;
}
return item;
}
// 十进制转换为二进制
void decimalToBinary(double decimal) {
if (decimal < 0) {
printf("二进制:-");
decimal = fabs(decimal);
} else {
printf("二进制:");
}
int integerPart = (int)decimal;
double fractionalPart = decimal - integerPart;
// 整数部分
Stack stackInteger;
initializeStack(&stackInteger);
while (integerPart > 0) {
push(&stackInteger, integerPart % 2);
integerPart /= 2;
}
while (!isStackEmpty(&stackInteger)) {
printf("%d", pop(&stackInteger));
}
// 小数部分
if (fractionalPart > 0) {
printf(".");
Queue queueFractional;
initializeQueue(&queueFractional, MAX_SIZE);
int count = 0;
while (count < 10) {
fractionalPart *= 2;
int bit = (int)fractionalPart;
enqueue(&queueFractional, bit);
fractionalPart -= (double)bit;
count++;
}
while (!isQueueEmpty(&queueFractional)) {
printf("%d", dequeue(&queueFractional));
}
}
printf("\n");
}
// 二进制转换为十进制
double binaryToDecimal(char *binary) {
double decimal = 0.0;
int length = 0;
int pointIndex = -1;
while (binary[length] != '\0') {
if (binary[length] == '.') {
pointIndex = length;
break;
}
length++;
}
for (int i = 0; i < length; i++) {
if (binary[i] == '1') {
decimal += pow(2, length - i - 1);
}
}
if (pointIndex != -1) {
for (int i = pointIndex + 1; binary[i] != '\0'; i++) {
if (binary[i] == '1') {
decimal += pow(2, pointIndex - i);
}
}
}
return decimal;
}
int main() {
int choice;
double decimal;
do {
printf("选择功能:\n");
printf("1. 十进制转二进制\n");
printf("2. 二进制转十进制\n");
printf("0. 退出\n");
printf("输入选择: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("输入一个十进制数:");
scanf("%lf", &decimal);
decimalToBinary(decimal);
break;
case 2:
printf("输入一个二进制数:");
char binary[32];
scanf("%s", binary);
printf("十进制数: %.10lf\n", binaryToDecimal(binary));
break;
case 0:
printf("退出程序\n");
break;
default:
printf("无效的选择,请重新输入\n");
}
} while (choice != 0);
return 0;
}