vendredi 10 mars 2017

Math operator as dynamic variable

I have a question that has not really been totally answered before.

Is there a c++ library that lets you pass a math operator as a variable? ex,

std::cout << "answer: " << std::mathBasic(firstNum, mathOperator, secondNum) << std::endl;

I know this can be done locally but I was wondering if there was a library that did it.

Example code just in case im not specific enought:

double_t mathBasic(double_t firstNum, char mathOperator, double_t secondNum)
{
 switch (mathOperator)
 {
  case '+': 
     return(firstNum + secondNum);
  case '-':
     return(firstNum - secondNum);
  case '*':
     return(firstNum * secondNum);
  case '/':
     return(firstNum / secondNum);
 }

 return 1;
}

void printResults(double_t firstNum, char mathOperator, double_t secondNum)
{

std::cout << firstNum << " " << mathOperator << " " << secondNum << " = " << mathBasic(firstNum, mathOperator, secondNum) << std::endl;

}

Thanks.

Edit: I'm not asking for code, I am asking for knowledge.

Aucun commentaire:

Enregistrer un commentaire