Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- #include "pila.h"
- #include "strutil.h"
- #include <ctype.h>
- /* LINEAS PARA GDB
- display arreglo[i]
- display (char*)pila->datos[0]
- display i
- */
- /* --------------------------------------------------------------*/
- /* --------------------Funciones matematicas---------------------*/
- /* --------------------------------------------------------------*/
- long sqrtp(long x){
- if(x<0) return -1;
- if(x==1) return 1;
- long m = x/2;
- while(true){
- if (m*m <= x && (m+1)*(m+1)> x)
- return m;
- if(m*m > x)
- m = m/2;
- else
- m = m + m/2;
- }
- }
- long powp(long x,long y){
- long res=1;
- for(int i=0;i<y;i++){
- res*=x;
- }
- return res;
- }
- long logp(long x1,long x2){
- long v=1; long cont=0;
- if(x1 == 1) return -1;
- if(x2 <= 0) return -1;
- while(true){
- v*=x1;
- cont++;
- if(v>x2)
- return cont-1;
- }
- }
- /* --------------------------------------------------------------*/
- /* --------------------Funciones Auxiliares----------------------*/
- /* --------------------------------------------------------------*/
- bool cadena_es_digit(char* cadena){
- if(cadena[0] == '-' || isdigit(cadena[0])){
- for(int i = 0;i<strlen(cadena);i++){
- if(!isdigit(cadena[i]))
- return false;
- }
- return true;
- }
- return false;
- }
- /* Esta funcion recibe una pila, segun su operador desapila n */
- /* Elementos y apila el elemento total */
- void operacion(pila_t* pila,char* op, int entrada,bool booleano){
- long resultado=0, x1,x2,x3;
- bool cond_gral = false , sobre_0 = true, raiz = true,log = true;
- void* dato1=NULL; void* dato2=NULL; void* dato3=NULL;
- if(entrada == 1){
- dato1 = pila_desapilar(pila);
- if(dato1){
- sscanf((char*)dato1,"%ld",&x1);
- if(x1>=0)
- resultado = sqrtp(x1);
- else
- raiz = false;
- }
- cond_gral = dato1;
- if(dato1)
- free(dato1);
- }
- else if(entrada == 2){
- dato1 = pila_desapilar(pila);
- dato2 = pila_desapilar(pila);
- if (dato1 && dato2){
- sscanf((char*)dato1,"%ld",&x1);
- sscanf((char*)dato2,"%ld",&x2);
- if(strstr("+\n",op))
- resultado = x1+x2;
- else if(strstr("-\n",op))
- resultado = x2-x1;
- else if(strstr("*\n",op))
- resultado = x1*x2;
- else if(strstr("/\n",op)){
- if(x1)
- resultado = x2/x1;
- else
- sobre_0=false;
- }
- else if(strstr("^\n",op))
- resultado = powp(x2,x1);
- else if(strstr("log\n",op)){
- if(x2 != 1 && x1>0){
- resultado = logp(x2,x1);
- }
- else
- log = false;
- }
- }
- cond_gral = dato1 && dato2;
- free(dato1);
- free(dato2);
- }
- else{
- dato1 = pila_desapilar(pila);
- dato2 = pila_desapilar(pila);
- dato3 = pila_desapilar(pila);
- if(dato1 && dato2 && dato3){
- sscanf((char*)dato1,"%ld",&x1);
- sscanf((char*)dato2,"%ld",&x2);
- sscanf((char*)dato3,"%ld",&x3);
- resultado = x3 ? x2 : x1 ;
- }
- cond_gral = dato1 && dato2 && dato3;
- free(dato1);
- free(dato2);
- free(dato3);
- }
- if(cond_gral && raiz && log && sobre_0){
- char* total = malloc(sizeof(char*));
- snprintf(total,10,"%ld",resultado);
- pila_apilar(pila, total);
- booleano = true;
- }
- else
- booleano = false;
- }
- void notacion_polaca(char* linea){
- bool booleano=true;
- char** arreglo = split(linea,' ');
- pila_t* pila = pila_crear();
- char* op_unitarias = "sqrt\n";
- char* op_binarias = "log\n+\n-\n*\n/\n^\n";
- char* op_ternarias = "?\n";
- size_t i=0;
- /*Este ciclo si sale bien me deja una pila de un elemento*/
- while(arreglo[i]){
- if (strstr("",arreglo[i])){
- i++;
- continue;
- }
- else if(strstr(op_unitarias,arreglo[i])){
- operacion(pila,arreglo[i],1,booleano);
- if(!booleano)
- break;
- }
- else if(strstr(op_binarias,arreglo[i])){
- operacion(pila,arreglo[i],2,booleano);
- if(!booleano)
- break;
- }
- else if(strstr(op_ternarias,arreglo[i])){
- operacion(pila,arreglo[i],3,booleano);
- if(!booleano)
- break;
- }
- else if(cadena_es_digit(arreglo[i])){
- char* puntero = malloc(sizeof(char*));
- if(!puntero) return;
- strcpy(puntero,arreglo[i]);
- pila_apilar(pila,puntero);
- }
- else
- break;
- i++;
- }
- char* resultado = pila_desapilar(pila);
- if (resultado && pila_esta_vacia(pila))
- printf("%s\n",resultado);
- else
- printf("ERROR\n");
- free(resultado);
- char* p;
- while(!pila_esta_vacia(pila)){
- p = pila_desapilar(pila);
- free(p);
- }
- pila_destruir(pila);
- free_strv(arreglo);
- }
- /* -------------------------------------------------------------- */
- /* ---------------------------- MAIN ---------------------------- */
- /* -------------------------------------------------------------- */
- int main(int argc, char* argv[]){
- if(argc>2){
- fprintf(stderr, "Error: No pasa archivo o muchos parametros\n");
- return 0;
- }
- size_t bufsize = 40;
- char* buffer = malloc(bufsize * sizeof(char));
- char* aux;
- FILE* archivo;
- //archivo=fopen("notacionpolaca.txt","r");
- if (argv[1])
- archivo = fopen(argv[1],"r");
- else
- archivo = stdin;
- while(!feof(archivo)){
- getline(&buffer,&bufsize,archivo);
- aux = strdup(buffer);
- notacion_polaca(aux);
- free(aux);
- }
- free(buffer);
- fclose(archivo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement