c语言通过栈实现进制转换器 实现10进制、2进制、8进制和16进制的相互转换。
时间: 2023-11-28 12:04:41 浏览: 58
好的,以下是通过栈实现进制转换器的示例代码,使用 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STACK_SIZE 100
typedef struct {
int top;
char items[MAX_STACK_SIZE];
} Stack;
void init_stack(Stack *stack) {
stack->top = -1;
}
int is_empty(Stack *stack) {
return stack->top == -1;
}
int is_full(Stack *stack) {
return stack->top == MAX_STACK_SIZE - 1;
}
void push(Stack *stack, char item) {
if (is_full(stack)) {
printf("Stack is full\n");
exit(EXIT_FAILURE);
}
stack->items[++stack->top] = item;
}
char pop(Stack *stack) {
if (is_empty(stack)) {
printf("Stack is empty\n");
exit(EXIT_FAILURE);
}
return stack->items[stack->top--];
}
char peek(Stack *stack) {
if (is_empty(stack)) {
printf("Stack is empty\n");
exit(EXIT_FAILURE);
}
return stack->items[stack->top];
}
int size(Stack *stack) {
return stack->top + 1;
}
void decimal_to_binary(int decimal_num) {
Stack stack;
init_stack(&stack);
while (decimal_num > 0) {
int remainder = decimal_num % 2;
push(&stack, remainder + '0');
decimal_num = decimal_num / 2;
}
printf("Binary: ");
while (!is_empty(&stack)) {
printf("%c", pop(&stack));
}
printf("\n");
}
void decimal_to_octal(int decimal_num) {
Stack stack;
init_stack(&stack);
while (decimal_num > 0) {
int remainder = decimal_num % 8;
push(&stack, remainder + '0');
decimal_num = decimal_num / 8;
}
printf("Octal: ");
while (!is_empty(&stack)) {
printf("%c", pop(&stack));
}
printf("\n");
}
void decimal_to_hexadecimal(int decimal_num) {
Stack stack;
init_stack(&stack);
while (decimal_num > 0) {
int remainder = decimal_num % 16;
if (remainder < 10) {
push(&stack, remainder + '0');
} else {
push(&stack, remainder - 10 + 'A');
}
decimal_num = decimal_num / 16;
}
printf("Hexadecimal: ");
while (!is_empty(&stack)) {
printf("%c", pop(&stack));
}
printf("\n");
}
void binary_to_decimal(char *binary_string) {
int decimal_num = 0;
int power = 0;
for (int i = strlen(binary_string) - 1; i >= 0; i--) {
int digit = binary_string[i] - '0';
decimal_num += digit * (1 << power);
power++;
}
printf("Decimal: %d\n", decimal_num);
}
void octal_to_decimal(char *octal_string) {
int decimal_num = 0;
int power = 0;
for (int i = strlen(octal_string) - 1; i >= 0; i--) {
int digit = octal_string[i] - '0';
decimal_num += digit * (1 << (power * 3));
power++;
}
printf("Decimal: %d\n", decimal_num);
}
void hexadecimal_to_decimal(char *hexadecimal_string) {
int decimal_num = 0;
int power = 0;
for (int i = strlen(hexadecimal_string) - 1; i >= 0; i--) {
char c = hexadecimal_string[i];
int digit;
if (c >= '0' && c <= '9') {
digit = c - '0';
} else if (c >= 'A' && c <= 'F') {
digit = c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
digit = c - 'a' + 10;
} else {
printf("Invalid hexadecimal string\n");
exit(EXIT_FAILURE);
}
decimal_num += digit * (1 << (power * 4));
power++;
}
printf("Decimal: %d\n", decimal_num);
}
int main() {
int decimal_num = 123;
decimal_to_binary(decimal_num);
decimal_to_octal(decimal_num);
decimal_to_hexadecimal(decimal_num);
char *binary_string = "1111011";
char *octal_string = "173";
char *hexadecimal_string = "7B";
binary_to_decimal(binary_string);
octal_to_decimal(octal_string);
hexadecimal_to_decimal(hexadecimal_string);
return 0;
}
```
以上是一个基本的栈实现进制转换器的示例代码,可以实现十进制、二进制、八进制和十六进制之间的相互转换。其中,我们定义了一个 `Stack` 结构体来实现栈的基本操作,然后分别实现了将十进制转换为二进制、八进制和十六进制的函数 `decimal_to_binary`、`decimal_to_octal` 和 `decimal_to_hexadecimal`,以及将二进制、八进制和十六进制转换为十进制的函数 `binary_to_decimal`、`octal_to_decimal` 和 `hexadecimal_to_decimal`。
阅读全文