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.