Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. double factorial(int fact);
  8. bool print = true;
  9.  
  10. int main()
  11. {
  12.     char operador;
  13.     double resultado = 0;
  14.     int c;
  15.  
  16.     while (true)
  17.     {
  18.         c = cin.peek();
  19.         if (c == EOF)
  20.         {
  21.             break;
  22.         }
  23.         else if (isspace(c))
  24.         {
  25.             cin.ignore();
  26.             if (c == '\n') {
  27.                 if (print) {
  28.                     cout << resultado << '\n';
  29.                 }
  30.                 print = true;
  31.             }
  32.         }
  33.         else if (c == 'r' || c == '!')
  34.         {
  35.             cin >> operador;
  36.             switch (operador)
  37.             {
  38.             case 'r':
  39.                 if(resultado > 0)
  40.                 {
  41.                     resultado = sqrt(resultado);
  42.                 }
  43.                 else
  44.                 {
  45.                     cout<<"Error, numero es negativo\n";
  46.                 }
  47.                 break;
  48.             case '!':
  49.                 resultado = factorial(resultado);
  50.                 break;
  51.             }
  52.         }
  53.         else if ((c == '+') ||(c == '-') || (c == '*') || (c == '/') || (c == '^'))
  54.         {
  55.             cin >> operador;
  56.         }
  57.         else if (isdigit(c) || c == '.')
  58.         {
  59.             double operando = 0;
  60.             cin >> operando;
  61.  
  62.             switch(operador) //switch para hacer las operaciones dependiendo del operador
  63.             {
  64.             case 0:
  65.                 resultado = operando;
  66.                 break;
  67.             case '+':
  68.                 resultado += operando;
  69.                 break;
  70.             case '-':
  71.                 resultado -= operando;
  72.                 break;
  73.             case '*':
  74.                 resultado *= operando;
  75.                 break;
  76.             case '/':
  77.                 resultado /= operando;
  78.                 break;
  79.             case '^': {
  80.                 double operando1 = resultado;
  81.                 double frac, intpart, temp;
  82.                 frac = modf(operando, &intpart);
  83.                 if((frac + intpart) > intpart)
  84.                 {
  85.                     cout<<"Exponente no es entero\n";
  86.                     print = 0;
  87.                 }
  88.  
  89.                 else{
  90.  
  91.                     temp = operando1;
  92.  
  93.                     if(operando == 0)
  94.                     {
  95.                         operando1 = 1;
  96.  
  97.                     }else if(operando == 1)
  98.                     {
  99.                         operando1 = operando1;
  100.                     }else{
  101.  
  102.                         while(operando > 1)
  103.                         {
  104.                             operando1 = operando1 * temp;
  105.                             operando--;
  106.                         }
  107.                     }
  108.                 }
  109.                 resultado = operando1;
  110.                 break;
  111.             }
  112.  
  113.             default:
  114.                 cout << "Hubo un error\n";
  115.             }
  116.         }
  117.         else
  118.         {
  119.             cout << "Hubo un error\n";
  120.             cin.ignore();
  121.         }
  122.     }
  123.  
  124.     return 0;
  125. }
  126.  
  127. double factorial(int fact){  //aqui he usado una funcion en vez de poner todo en el bucle switch, si tuviera mas tiempo lo haria para ^, y r tambien
  128.  
  129.     double n;
  130.  
  131.  
  132.     if(fact == 0){ n = 1;}
  133.     else if(fact >= 1)
  134.     {
  135.         n = fact; //guardar el valor original en n
  136.         fact--; //decrementer el valor de fact, ya que el factorial es X * (X-1) * (X-1).... * 1
  137.  
  138.         while(fact > 1)
  139.         {
  140.             n = n * fact; //guardar el producto de n por fact (uno menos que n) en n, hasta que el valor de la variable fact = 2, ya que X * 1 = X
  141.             fact--;
  142.         }
  143.  
  144.     }else
  145.     {
  146.         cout<<"Factorial debe ser positivo";
  147.         print = 0;
  148.     }
  149.  
  150.  
  151.     return n;
  152. }
Add Comment
Please, Sign In to add comment