Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #include <cstdio>
- using namespace std;
- double factorial(int fact);
- bool print = true;
- int main()
- {
- char operador;
- double resultado = 0;
- int c;
- while (true)
- {
- c = cin.peek();
- if (c == EOF)
- {
- break;
- }
- else if (isspace(c))
- {
- cin.ignore();
- if (c == '\n') {
- if (print) {
- cout << resultado << '\n';
- }
- print = true;
- }
- }
- else if (c == 'r' || c == '!')
- {
- cin >> operador;
- switch (operador)
- {
- case 'r':
- if(resultado > 0)
- {
- resultado = sqrt(resultado);
- }
- else
- {
- cout<<"Error, numero es negativo\n";
- }
- break;
- case '!':
- resultado = factorial(resultado);
- break;
- }
- }
- else if ((c == '+') ||(c == '-') || (c == '*') || (c == '/') || (c == '^'))
- {
- cin >> operador;
- }
- else if (isdigit(c) || c == '.')
- {
- double operando = 0;
- cin >> operando;
- switch(operador) //switch para hacer las operaciones dependiendo del operador
- {
- case 0:
- resultado = operando;
- break;
- case '+':
- resultado += operando;
- break;
- case '-':
- resultado -= operando;
- break;
- case '*':
- resultado *= operando;
- break;
- case '/':
- resultado /= operando;
- break;
- case '^': {
- double operando1 = resultado;
- double frac, intpart, temp;
- frac = modf(operando, &intpart);
- if((frac + intpart) > intpart)
- {
- cout<<"Exponente no es entero\n";
- print = 0;
- }
- else{
- temp = operando1;
- if(operando == 0)
- {
- operando1 = 1;
- }else if(operando == 1)
- {
- operando1 = operando1;
- }else{
- while(operando > 1)
- {
- operando1 = operando1 * temp;
- operando--;
- }
- }
- }
- resultado = operando1;
- break;
- }
- default:
- cout << "Hubo un error\n";
- }
- }
- else
- {
- cout << "Hubo un error\n";
- cin.ignore();
- }
- }
- return 0;
- }
- 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
- double n;
- if(fact == 0){ n = 1;}
- else if(fact >= 1)
- {
- n = fact; //guardar el valor original en n
- fact--; //decrementer el valor de fact, ya que el factorial es X * (X-1) * (X-1).... * 1
- while(fact > 1)
- {
- 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
- fact--;
- }
- }else
- {
- cout<<"Factorial debe ser positivo";
- print = 0;
- }
- return n;
- }
Add Comment
Please, Sign In to add comment