Calculator does not give solutions

-1

the program performs basic arithmetic operations (addition, subtraction, multiplication ...) and calculates functions sin, cos, Ln (neperian logarithm.) the calculator must be able to validate grouping signs in order to calculate complex expressions such as ((3 + 5) * 14) + 4 * sin (pi))

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
double division(char *, char*);
double operador(char *);
bool mapeo2(char*);
bool mapeo(char *);
double detectar(char *);
using namespace std;

int main() {

char ecuacion[100]= " ";
char ecuacion2[100] = " ";
char *ptr;
bool c=false;
double resultado =0;
cout << "Ingrese la ecuacion: ";
    cin.getline(ecuacion, 100);
    strncpy(ecuacion2, ecuacion, strlen(ecuacion));

if(mapeo(ecuacion)){ 
    c = mapeo2(ecuacion);
    ptr = strtok(ecuacion, "()");
    cout << ptr << endl;
    while(ptr!=NULL){
        if(c== true){
        resultado = detectar(ptr);
        }else{
            resultado = operador(ptr);
        }

        ptr= strtok(NULL, "()");
    }
}   else{
    resultado=operador(ecuacion);
}

cout << "El resultado es : " << resultado << endl;
return 0;
}


double operador(char *e){ //divide la cadena por operador aritmetico
double result = 0;
for(int i=0; i < strlen(e); i++){
    switch(e[i]){
        case '+':
            result = division(e,"+");
            break;
        case '-':
            result = division(e,"-");
            break;
        case '*':
            result = division(e,"*");
            break;
        case '/':
            result = division(e,"/");
            break;
        case '^':
            result = division(e,"^");
            break;
    }
}
return result;
}


double division(char *ecuacion, char *x){ //divide la cadena por parentesis 
char *ptr;
double result =0;
switch(*x){
case '+':
    ptr= strtok(ecuacion,x);
    while(ptr!=NULL){
        result += atof(ptr);
        ptr = strtok(NULL,x);
    }
    break;

case '-':
    ptr= strtok(ecuacion,x);
    result = atof(ptr);
    cout << result << endl;
    ptr++;
    while(ptr!=NULL){
        cout << ptr << endl;
        result -= atof(ptr);
        ptr = strtok(NULL,x);
    }
    break;

case '*':
    ptr= strtok(ecuacion,x);
    result = atof(ptr);
    ptr++;
    while(ptr!=NULL){
        result = (result) * (atof(ptr));
        ptr = strtok(NULL,x);
    }
    break;

case '/':
    ptr= strtok(ecuacion,x);
    result = atof(ptr);
    ptr++;
    while(ptr!=NULL){
        result = result/ atof(ptr);
        ptr = strtok(NULL,x);
    }
    break;

case '^':
    ptr=strtok(ecuacion, x);
    result = atof(ptr);
    ptr++;
    while(ptr!=NULL){
        result = pow(result, *(ptr+1));
        ptr = strtok(NULL, x);
    break;

    }
}
return result;
}


bool mapeo(char *e){
bool t= false;
for(int i=0; i<strlen(e); i++){
    if((e[i] == '(') || (e[i]==')')){
        t = true;
        i = strlen(e);
    } 

}
return t;
}


double detectar(char *e){ //resuelve funciones de sen,cos, Ln y radicales
int s, c, l, r, ex;
double result=0;
s = strcmp(e, "sen");
c = strcmp(e, "cos");
l = strcmp(e, "ln");
r = strcmp(e, "sqrt");
if ( s ==0){
    result = sin(*(e+1));
}
if ( c ==0){
    result = cos(*(e+1));
}
if ( l ==0){
    if(atof((e+1)) >0){
    result = log(*(e+1));
    }
}
if ( r ==0){
    result = pow(*(e+1),(1.0/ *(e+2)));
}
return result;
}


bool mapeo2(char *e){
for(int i=0; i < strlen(e); i++){
    switch(e[i]){
        case 's':
            return true;
            i= strlen(e);
            break;
        case 'c':
            return true;
            i= strlen(e);
            break;
        case 'l':
            return true;
            i= strlen(e);
            break;
    }
}
}

The problem is that the solution always gives = 0. sorry for the length of the code, I would appreciate your help.

    
asked by justAprogrammer 09.11.2016 в 06:36
source

1 answer

1

You have many problems in your code related to the search of the operands that intervene in the operation.

I only fix the multiplication operation to give you an idea of what happens to you in the code:

case '*':
    ptr = strtok(ecuacion, x);
    result = atof(ptr);
    cout << result << endl;
    ptr = strtok(NULL, x);
    while (ptr != NULL) {
        cout << ptr << endl;
        result *= atof(ptr);
        ptr = strtok(NULL, x);
    }
    break;

Mainly, as I told you, you do not get the operands correctly, so you always get a second empty operand that the atof translates it as a zero (0), which during a multiplication results in 0 whatever the rest of operands or the division results in infinite by making a division between zero.

By the way, if you want to remove the obsolete conversion warning messages, set the function division as double division(char *, const char*); and later as double division(char* ecuacion, const char* x) { .

    
answered by 09.11.2016 в 10:08