Algoritmia - ecuacion a posfijo e infijo

 
Vista:

ecuacion a posfijo e infijo

Publicado por shivett (1 intervención) el 19/06/2007 22:27:54
hola necesito un programa que analice una ecuacion y la pase a posfijo e infijo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Conversión de expresiones algebraicas: Infixa a postfija

Publicado por Alejandro (307 intervenciones) el 11/03/2024 19:53:20
Entendido, Shivett, aquí te proporcionaré un programa simple en C que toma una expresión algebraica en notación infija y la convierte a notación posfija (postfija) utilizando la técnica del "algoritmo shunting yard". Además, mostrará la notación infija original.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_SIZE 100
 
// Estructura de la pila
typedef struct {
    char items[MAX_SIZE];
    int top;
} Stack;
 
// Funciones para la pila
void initialize(Stack* stack) {
    stack->top = -1;
}
 
int isEmpty(Stack* stack) {
    return stack->top == -1;
}
 
void push(Stack* stack, char item) {
    stack->items[++stack->top] = item;
}
 
char pop(Stack* stack) {
    if (!isEmpty(stack)) {
        return stack->items[stack->top--];
    }
    return '\0';
}
 
char peek(Stack* stack) {
    if (!isEmpty(stack)) {
        return stack->items[stack->top];
    }
    return '\0';
}
 
// Funciones para la conversión
int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}
 
int getPrecedence(char c) {
    if (c == '+' || c == '-')
        return 1;
    else if (c == '*' || c == '/')
        return 2;
    return 0;
}
 
void infixToPostfix(char infix[], char postfix[]) {
    Stack stack;
    initialize(&stack);
 
    int i, j;
    i = j = 0;
 
    while (infix[i] != '\0') {
        if (infix[i] >= 'a' && infix[i] <= 'z') {
            postfix[j++] = infix[i++];
        } else if (isOperator(infix[i])) {
            while (!isEmpty(&stack) && getPrecedence(peek(&stack)) >= getPrecedence(infix[i])) {
                postfix[j++] = pop(&stack);
            }
            push(&stack, infix[i++]);
        } else if (infix[i] == '(') {
            push(&stack, infix[i++]);
        } else if (infix[i] == ')') {
            while (!isEmpty(&stack) && peek(&stack) != '(') {
                postfix[j++] = pop(&stack);
            }
            pop(&stack); // Pop '('
            i++;
        } else {
            // Ignorar otros caracteres (espacios, números, etc.)
            i++;
        }
    }
 
    while (!isEmpty(&stack)) {
        postfix[j++] = pop(&stack);
    }
 
    postfix[j] = '\0';
}
 
int main() {
    char infix[MAX_SIZE], postfix[MAX_SIZE];
 
    printf("Ingrese una expresión algebraica infija: ");
    fgets(infix, sizeof(infix), stdin);
    infix[strcspn(infix, "\n")] = '\0';  // Elimina el salto de línea de fgets
 
    infixToPostfix(infix, postfix);
 
    printf("\nNotación infija original: %s\n", infix);
    printf("Notación posfija resultante: %s\n", postfix);
 
    return 0;
}

Este programa solicitará al usuario que ingrese una expresión algebraica en notación infija y luego imprimirá la notación infija original y la notación posfija resultante utilizando el algoritmo shunting yard. Ten en cuenta que este programa asume que la expresión ingresada es válida y que no tiene errores de sintaxis.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar