Created
November 28, 2022 05:02
-
-
Save Samu31Nd/fd5efe1cf8d8355ba472504a44064e42 to your computer and use it in GitHub Desktop.
Problema 1,2 y 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Pilas.h" | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| /* Realice un programa que compruebe si los paréntesis de una expresión dada | |
| * están equilibrados utilizando para ello un contador de paréntesis. | |
| * El programa debe contener las siguientes funciones: | |
| * leerExpresion solicita al usuario la expresión a evaluarla | |
| * la función checaParentesis que permite realizar el análisis de los paréntesis | |
| * dentro de la expresión y devuelve verdadero o falso. | |
| * El módulo de mostrar que desplegará al usuario si los paréntesis de la | |
| * expresión si están o no equilibrados. | |
| */ | |
| /* Modifique el programa del problema anterior agregando las funciones checaCorchetes | |
| * y checaLlaves para comprobar el buen equilibrado de paréntesis, corchetes y llaves | |
| * mediante contadores. | |
| * */ | |
| // Solicita al usuario la expresion a evaluar | |
| void leerExpresion(char *); | |
| char *crearArrChar(); | |
| int checaParentesis(char *); | |
| int checaCorchetes(char *); | |
| int checaLlaver(char *); | |
| void mostrar(char *); | |
| void liberarChar(char *); | |
| int main() { | |
| char *expresion = crearArrChar(); | |
| leerExpresion(expresion); | |
| mostrar(expresion); | |
| liberarChar(expresion); | |
| return 0; | |
| } | |
| void leerExpresion(char *expresion) { | |
| int count = 0; | |
| printf("\n\tEvaluacion de expresiones\n"); | |
| printf("\n\n Introduzca la expresion matematica a evaluar: "); | |
| while ((expresion[count++] = getchar()) != '\n') | |
| ; | |
| // nvim formatting does that weird semicolon, dunno why | |
| expresion[--count] = '\0'; | |
| } | |
| char *crearArrChar() { | |
| char *ap = (char *)malloc(sizeof(char) * TAM); | |
| if (ap == NULL) { | |
| manejaError(2); | |
| exit(1); | |
| } | |
| return ap; | |
| } | |
| int checaParentesis(char *expresion) { | |
| PILA p = crearPila(); | |
| char aux; | |
| int pos = 0; | |
| while (expresion[pos] != '\0') { | |
| aux = expresion[pos++]; | |
| switch (aux) { | |
| case '(': | |
| apilar(p, aux); | |
| break; | |
| case ')': | |
| desapilar(p); | |
| break; | |
| } | |
| } | |
| int opcion = es_vaciaPila(p); | |
| liberarPila(p); | |
| return opcion; | |
| } | |
| int checaCorchetes(char *expresion) { | |
| PILA p = crearPila(); | |
| char aux; | |
| int pos = 0; | |
| while (expresion[pos] != '\0') { | |
| aux = expresion[pos++]; | |
| switch (aux) { | |
| case '[': | |
| apilar(p, aux); | |
| break; | |
| case ']': | |
| desapilar(p); | |
| break; | |
| } | |
| } | |
| int opcion = es_vaciaPila(p); | |
| liberarPila(p); | |
| return opcion; | |
| } | |
| int checaLlaves(char *expresion) { | |
| PILA p = crearPila(); | |
| char aux; | |
| int pos = 0; | |
| while (expresion[pos] != '\0') { | |
| aux = expresion[pos++]; | |
| switch (aux) { | |
| case '{': | |
| apilar(p, aux); | |
| break; | |
| case '}': | |
| desapilar(p); | |
| break; | |
| } | |
| } | |
| int opcion = es_vaciaPila(p); | |
| liberarPila(p); | |
| return opcion; | |
| } | |
| void mostrar(char *expresion) { | |
| printf("\nLa expresion { %s } ",expresion); | |
| if (checaParentesis(expresion) ) { | |
| printf("tiene los parentesis, corchetes y llaves equilibrados."); | |
| } else { | |
| printf("no esta equilibrada."); | |
| } | |
| printf("\n\n"); | |
| } | |
| void liberarChar(char *ap) { | |
| free(ap); | |
| manejaError(3); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Pilas.h" | |
| #include <math.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| PILA crearPila() { | |
| PILA tmp = (PILA)malloc(sizeof(pila)); | |
| if (tmp == NULL) { | |
| manejaError(0); | |
| exit(-1); | |
| } | |
| tmp->tope = -1; | |
| return tmp; | |
| } | |
| // num de errores posibles: 4 | |
| void manejaError(int e) { | |
| char *errores[] = {"No hay memoria disponible\n", "Pila Llena\n", | |
| "No se pudo crear el arrChar\n", | |
| "Se ha liberado la memoria...\n"}; | |
| printf("%s", errores[e]); | |
| } | |
| int es_vaciaPila(PILA S) { | |
| if (S->tope < 0) | |
| return 1; | |
| return 0; | |
| } | |
| void apilar(PILA S, char e) { | |
| if (S->tope == (TAM - 1)) { | |
| manejaError(2); | |
| exit(0); | |
| } | |
| (S->tope)++; | |
| S->pila[S->tope] = e; | |
| } | |
| float desapilar(PILA S) { | |
| float a; | |
| if (!(es_vaciaPila(S))) { | |
| a = S->pila[S->tope]; | |
| S->pila[S->tope] = 0; | |
| (S->tope)--; | |
| return a; | |
| } | |
| manejaError(3); // Pila vacia | |
| exit(0); | |
| } | |
| float elemTope(PILA S) { | |
| float a; | |
| if (!(es_vaciaPila(S))) { | |
| a = S->pila[S->tope]; | |
| return a; | |
| } | |
| manejaError(3); // Pila vacia | |
| exit(0); | |
| } | |
| void liberarPila(PILA ap) { free(ap); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef _pilas_ | |
| #define _pilas_ | |
| #include <math.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define TAM 100 | |
| typedef struct { | |
| char pila[TAM]; | |
| int tope; | |
| } pila; | |
| typedef pila *PILA; | |
| PILA crearPila(); | |
| void manejaError(int); | |
| void apilar(PILA, char); | |
| float desapilar(PILA); | |
| int es_vaciaPila(PILA); | |
| float elemTope(PILA); | |
| void liberarPila(PILA); | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment