Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. void calculadora_polaca(char** comandos){
  2.     pila_t* pila = pila_crear();
  3.     if(!pila){
  4.         fprintf(stdout, "%s", "ERROR\n");
  5.         return;
  6.     }
  7.     bool ok = true;
  8.     for(int i = 0; comandos[i]; i++){
  9.         int* n = atoi(comandos[i]);
  10.         if(*n || strcmp(comandos[i], "0") == 0){
  11.             pila_apilar(pila, n);
  12.         }
  13.         else{
  14.             free(n);
  15.             calcular(pila, comandos[i], &ok);
  16.             if(!ok){
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.     if(!ok){
  22.         fprintf(stdout, "%s\n", "ERROR");
  23.     }
  24.     else{
  25.         int* respuesta = pila_desapilar(pila);
  26.         if(pila_esta_vacia(pila)){
  27.             fprintf(stdout, "%d\n", *respuesta);
  28.         }
  29.         else{
  30.             fprintf(stdout, "%s\n", "ERROR");
  31.             free(respuesta);
  32.         }
  33.     }
  34.     free_strv(comandos);
  35.     pila_destruir(pila)
  36. }
  37.  
  38. int main(int argc, char* argv[]){
  39.     size_t tam = 0;
  40.     FILE* archivo;
  41.     if(argc == 1){
  42.         archivo = stdin;
  43.     }
  44.     else if(argc == 2){
  45.         char* entrada = argv[1];
  46.         archivo = fopen(entrada, "r");
  47.         if(!archivo){
  48.             fprintf(stderr, "%s", "No se pudo leer el archivo\n");
  49.             return -1;
  50.         }
  51.     }
  52.     else{
  53.         fprintf(stderr, "%s", "Cantidad de parametros erroneo\n");
  54.         return -1;
  55.     }
  56.     char* linea;
  57.     while(getline(&linea, &tam, archivo) != EOF){
  58.         char** linea_act = split(linea, ' ')
  59.         if(!linea_act){
  60.             fclose(archivo);
  61.             fprintf(archivo, "%s", "ERROR\n");
  62.             return -1;
  63.         }
  64.         calculadora_polaca(linea_act);
  65.         free_strv(linea_act);
  66.     }
  67.     free(linea);
  68.     fclose(archivo);
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement