Advertisement
Guest User

dc

a guest
Nov 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.21 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "pila.h"
  7. #include "strutil.h"
  8. #include <ctype.h>
  9. /* LINEAS PARA GDB
  10.     display arreglo[i]
  11.     display (char*)pila->datos[0]
  12.     display i
  13. */
  14.  
  15. /* --------------------------------------------------------------*/
  16. /* --------------------Funciones matematicas---------------------*/
  17. /* --------------------------------------------------------------*/
  18.  
  19. long sqrtp(long x){
  20.     if(x<0) return -1;
  21.     if(x==1) return 1;
  22.     long m = x/2;
  23.     while(true){
  24.         if (m*m <= x && (m+1)*(m+1)> x)
  25.             return m;  
  26.         if(m*m > x)
  27.             m = m/2;
  28.         else
  29.             m = m + m/2;
  30.     }
  31. }  
  32.  
  33. long powp(long x,long y){
  34.     long res=1;
  35.     for(int i=0;i<y;i++){
  36.         res*=x;
  37.     }
  38.     return res;
  39.  
  40. }
  41.  
  42.  
  43. long logp(long x1,long x2){
  44.     long v=1; long cont=0;
  45.     if(x1 == 1) return -1;
  46.     if(x2 <= 0) return -1;
  47.     while(true){
  48.         v*=x1;
  49.         cont++;
  50.         if(v>x2)
  51.             return cont-1;
  52.     }
  53.      
  54. }
  55. /* --------------------------------------------------------------*/
  56. /* --------------------Funciones Auxiliares----------------------*/
  57. /* --------------------------------------------------------------*/
  58.  
  59. bool cadena_es_digit(char* cadena){
  60.     if(cadena[0] == '-' || isdigit(cadena[0])){
  61.         for(int i = 0;i<strlen(cadena);i++){
  62.             if(!isdigit(cadena[i]))
  63.                 return false;
  64.         }  
  65.         return true;    
  66.     }
  67.     return false;
  68. }
  69.  
  70.  
  71. /* Esta funcion recibe una pila, segun su operador desapila n */
  72. /*              Elementos y apila el elemento total           */
  73.  
  74. void operacion(pila_t* pila,char* op, int entrada,bool booleano){
  75.     long resultado=0, x1,x2,x3;
  76.     bool cond_gral = false , sobre_0 = true, raiz = true,log = true;
  77.     void* dato1=NULL; void* dato2=NULL; void* dato3=NULL;
  78.     if(entrada == 1){
  79.         dato1 = pila_desapilar(pila);
  80.         if(dato1){
  81.             sscanf((char*)dato1,"%ld",&x1);
  82.             if(x1>=0)
  83.                 resultado = sqrtp(x1);
  84.             else
  85.                 raiz = false;
  86.         }
  87.         cond_gral = dato1;
  88.         if(dato1)
  89.             free(dato1);
  90.     }
  91.     else if(entrada == 2){
  92.         dato1 = pila_desapilar(pila);
  93.         dato2 = pila_desapilar(pila);
  94.         if (dato1 && dato2){
  95.             sscanf((char*)dato1,"%ld",&x1);
  96.             sscanf((char*)dato2,"%ld",&x2);
  97.             if(strstr("+\n",op))
  98.                 resultado = x1+x2;
  99.             else if(strstr("-\n",op))
  100.                 resultado = x2-x1;
  101.             else if(strstr("*\n",op))
  102.                     resultado = x1*x2;
  103.             else if(strstr("/\n",op)){
  104.                 if(x1)
  105.                     resultado = x2/x1;
  106.                 else
  107.                     sobre_0=false;
  108.             }  
  109.             else if(strstr("^\n",op))
  110.                 resultado = powp(x2,x1);
  111.             else if(strstr("log\n",op)){
  112.                
  113.                 if(x2 != 1 && x1>0){
  114.                     resultado = logp(x2,x1);
  115.                 }
  116.                 else
  117.                     log = false;       
  118.             }
  119.         }  
  120.         cond_gral = dato1 && dato2;
  121.         free(dato1);
  122.         free(dato2);
  123.     }
  124.         else{
  125.             dato1 = pila_desapilar(pila);
  126.             dato2 = pila_desapilar(pila);
  127.             dato3 = pila_desapilar(pila);
  128.             if(dato1 && dato2 && dato3){
  129.                 sscanf((char*)dato1,"%ld",&x1);
  130.                 sscanf((char*)dato2,"%ld",&x2);
  131.                 sscanf((char*)dato3,"%ld",&x3);
  132.                 resultado = x3 ? x2 : x1 ;
  133.         }
  134.             cond_gral = dato1 && dato2 && dato3;
  135.             free(dato1);
  136.             free(dato2);
  137.             free(dato3);
  138.     }
  139.  
  140.     if(cond_gral && raiz && log && sobre_0){
  141.         char* total = malloc(sizeof(char*));
  142.         snprintf(total,10,"%ld",resultado);
  143.         pila_apilar(pila, total);
  144.         booleano = true;
  145.     }
  146.     else
  147.         booleano = false;
  148.    
  149.    
  150.     }
  151.  
  152.  
  153. void notacion_polaca(char* linea){
  154.     bool booleano=true;
  155.     char** arreglo = split(linea,' ');
  156.     pila_t* pila = pila_crear();
  157.     char* op_unitarias = "sqrt\n";
  158.     char* op_binarias = "log\n+\n-\n*\n/\n^\n";
  159.     char* op_ternarias = "?\n";
  160.     size_t i=0;
  161.  
  162. /*Este ciclo si sale bien me deja una pila de un elemento*/
  163.  
  164.     while(arreglo[i]){
  165.         if (strstr("",arreglo[i])){
  166.             i++;
  167.             continue;
  168.         }
  169.         else if(strstr(op_unitarias,arreglo[i])){
  170.             operacion(pila,arreglo[i],1,booleano);
  171.             if(!booleano)
  172.                 break;
  173.         }
  174.         else if(strstr(op_binarias,arreglo[i])){
  175.             operacion(pila,arreglo[i],2,booleano);
  176.             if(!booleano)
  177.                 break;
  178.         }
  179.         else if(strstr(op_ternarias,arreglo[i])){
  180.             operacion(pila,arreglo[i],3,booleano);
  181.             if(!booleano)
  182.                 break;
  183.         }
  184.         else if(cadena_es_digit(arreglo[i])){  
  185.             char* puntero = malloc(sizeof(char*));
  186.             if(!puntero) return;
  187.             strcpy(puntero,arreglo[i]);
  188.             pila_apilar(pila,puntero);
  189.         }
  190.         else
  191.             break;
  192.         i++;
  193.     }
  194.  
  195.     char* resultado = pila_desapilar(pila);
  196.     if (resultado && pila_esta_vacia(pila))
  197.         printf("%s\n",resultado);
  198.     else
  199.         printf("ERROR\n");
  200.     free(resultado);
  201.    
  202.     char* p;
  203.     while(!pila_esta_vacia(pila)){
  204.         p = pila_desapilar(pila);
  205.         free(p);
  206.     }
  207.     pila_destruir(pila);
  208.     free_strv(arreglo);
  209. }
  210.  
  211.  
  212.  
  213. /* -------------------------------------------------------------- */
  214. /* ---------------------------- MAIN ---------------------------- */
  215. /* -------------------------------------------------------------- */
  216.  
  217.  
  218. int main(int argc, char* argv[]){
  219.  
  220.     if(argc>2){
  221.         fprintf(stderr, "Error: No pasa archivo o muchos parametros\n");
  222.         return 0;
  223.     }
  224.  
  225.     size_t bufsize = 40;
  226.     char* buffer = malloc(bufsize * sizeof(char));
  227.     char* aux;
  228.  
  229.     FILE* archivo;
  230.     //archivo=fopen("notacionpolaca.txt","r");
  231.  
  232.     if (argv[1])
  233.         archivo = fopen(argv[1],"r");
  234.     else
  235.         archivo = stdin;
  236.  
  237.     while(!feof(archivo)){
  238.         getline(&buffer,&bufsize,archivo);
  239.         aux = strdup(buffer);
  240.         notacion_polaca(aux);
  241.         free(aux);
  242.     }
  243.     free(buffer);
  244.     fclose(archivo);
  245.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement