Skip to content

Instantly share code, notes, and snippets.

@Samu31Nd
Last active November 25, 2022 20:06
Show Gist options
  • Select an option

  • Save Samu31Nd/d96c1c5faf5efe6f0bdcbdff84df56fd to your computer and use it in GitHub Desktop.

Select an option

Save Samu31Nd/d96c1c5faf5efe6f0bdcbdff84df56fd to your computer and use it in GitHub Desktop.
Código Postfija
#include "Postfija.h"
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
void lee(char*);
char *crearEq();
void evalua(char*,PILA);
void mostrarSalida(char*,PILA);
void main(){
PILA p;
//char *ecuacion;
//ecuacion = crearEq();
char ecuacion[100];
lee(ecuacion);
evalua(ecuacion,p);
mostrarSalida(ecuacion,p);
}
void lee(char *ent){
int pos=0;
printf("\n\tEvaluacion de expresiones Postfijas\n");
printf("Operaciones Basicas: Suma, Resta, Producto y Division\n\n");
printf("\n\n Introduzca la expresion en Postfija: ");
fgets(ent,TAM,stdin);
printf("\n%s. No más",ent);
}
char *crearEq(){
char *ap;
ap = (char*)malloc(sizeof(char));
if(ap == NULL){
manejaError(4);
exit(-1);
}
printf("Tamaño: %ld",sizeof(ap));
return ap;
}
void evalua(char *eq, PILA ap){
double conv;
int pos = 0;
char op[1];
//inicio de la evaluacion
while (eq[pos] != '\0'){
*op = eq[pos++];
switch(*op){
case '+': apilar(ap,desapilar(ap) + desapilar(ap));
break;
case '-': apilar(ap, desapilar(ap) - desapilar(ap));
break;
case '*': apilar(ap, desapilar(ap) * desapilar(ap));
break;
case '/': apilar(ap, dividir(desapilar(ap),desapilar(ap)) );
break;
case '^': apilar(ap, pow(desapilar(ap), desapilar(ap)) );
break;
default: conv = atof(op);//convierte un caracter a float
printf("paso 1. %.2f",conv);
apilar(ap,conv); //Almacena el valor (numero) en la pila
printf("paso 2.\n");
}
}
}
void mostrarSalida(char *eq, PILA ap){
printf("\n\n\t %s = %.2f \n",eq,desapilar(ap));
}
#include"Postfija.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.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", "Pila Vacia\n","Division por CERO\n","Error con char\n"};
printf("%s", errores[e]);
}
int es_vaciaPila(PILA S){
if( S -> tope < 0)
return 1;
return 0;
}
void apilar(PILA S, float 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);
}
float dividir(float b, float a){
float result;
if (b == 0){
manejaError(3);
exit(0);
}
result = a/b;
return result;
}
#ifndef _postfija_
#define _postfija_
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define TAM 100
typedef struct{
float pila[TAM];
int tope;
} pila;
typedef pila * PILA;
PILA crearPila();
void manejaError(int);
void apilar(PILA,float);
float desapilar(PILA);
int es_vaciaPila();
float elemTope(PILA);
//inicio postfija
float dividir(float,float);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment