Advertisement
niqo1993

Untitled

Nov 18th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. int main(int argc, char* const argv[]){
  2.     FILE* archivo = stdin;
  3.     char* buffer = NULL;
  4.     pila_t* pila = pila_crear();
  5.     size_t n = 0;
  6.     size_t indice = 0;
  7.     bool se_calculo = NULL;
  8.     while(getline(&buffer,&n,archivo) > 0){
  9.         char** strv = split(buffer,' ');
  10.         size_t u = contar(strv);
  11.         strv[u-1] = substr(strv[u-1], strlen(strv[u-1])-1);
  12.         while(strv[indice]){
  13.             if (es_numero(strv[indice]) && strlen(strv[indice]) != 0){
  14.                 int* numero = (int*)malloc(sizeof(int*));
  15.                 *numero = atoi(strv[indice]);
  16.                 pila_apilar(pila, (void*)numero);
  17.             }
  18.             if (es_operacion(strv[indice])){
  19.                 se_calculo = calcular(pila, strv[indice]);
  20.                 if (!se_calculo) break;
  21.             }
  22.             free(strv[indice]);
  23.             indice++;
  24.         }
  25.         free(strv);
  26.         if (!se_calculo || pila_cantidad(pila) != 1){
  27.             fprintf(stdout,"ERROR\n");
  28.             while(!pila_esta_vacia(pila)) free(pila_desapilar(pila));
  29.             indice = 0;
  30.             continue;
  31.         }
  32.         indice = 0;
  33.         fprintf(stdout, "%d\n",*(int*)pila_ver_tope(pila) );
  34.         while(!pila_esta_vacia(pila)) free(pila_desapilar(pila));
  35.     }
  36.     free(buffer);
  37.     pila_destruir(pila);
  38.     return 0;
  39. }
  40.  
  41. bool calcular(pila_t* pila, char* operacion){
  42.     if (pila_esta_vacia(pila)) return false;
  43.     int* x = NULL;
  44.     int* y = NULL;
  45.     size_t cantidad_elementos = pila_cantidad(pila);
  46.     if (strcmp("sqrt",operacion) == 0) operacion = "s";
  47.     else if (strcmp("log",operacion) == 0) operacion = "l";
  48.     if(strcmp(operacion,"s") == 0) {
  49.         x = (int*)pila_desapilar(pila);
  50.     }else{
  51.         if (strcmp(operacion,"s") != 0 && cantidad_elementos > 1){
  52.             x = (int*)pila_desapilar(pila);
  53.             y = (int*)pila_desapilar(pila);
  54.         }
  55.         else return false;
  56.     }
  57.     int* res = (int*)malloc(sizeof(int*));
  58.     switch(*operacion){
  59.         case '+':
  60.             *res = (*x) + (*y);
  61.             break;
  62.         case'-':
  63.             *res = (*x) - (*y);
  64.             break;
  65.         case '*':
  66.             *res = (*x) * (*y);
  67.             break;
  68.         case '/':
  69.             if ((*y) == 0) return false;
  70.             *res = (*x) / (*y);
  71.             break;
  72.         case '^':
  73.             if ((*y) < 0) return false;
  74.             *res = potencia(*x,*y);
  75.             break;
  76.         case 's':
  77.             if ((*x) < 0) return false;
  78.             *res = raiz(*x, (*x)/2);
  79.             break;
  80.         case 'l':
  81.             if ((*y) <= 1 || (*x) < 1) return false;
  82.             *res = logaritmo((*y),(*x));
  83.             break;
  84.         case '?':
  85.             if(pila_esta_vacia(pila)) return false;
  86.             int* z = (int*)pila_desapilar(pila);
  87.             if ( *x == 0) *res = *z;
  88.             else *res = *y;
  89.             free(z);
  90.             break;
  91.         }
  92.         pila_apilar(pila, (void*)res);
  93.         free(x);
  94.         if (*operacion != 's') free(y);
  95.         return true;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement