# Thread: handling basic math functions

1. ## handling basic math functions

Okay so I have my program almost completed but, I am stuck on something... I need to include basic math functions such as abs, sqrt, pow, exp, log, sin, cos, tan, asin, acos, atan, sinh, cosh, and tanh.

Do I put these math functions as a case... and if so how do I put it as the result of a function or variable inputed... did that question make sense?

ok lets say...

if one of my cases is

Code:
```case '+':
res=a+b;
cout<<res;```
how do I get one of the basic math functions to process one of the prior cases... so in better words i want to say something like

sqrt(res)...

except when it gets to pow of course... I will NOT use "res".

heres my code

Code:
```#include <iostream>
#include <cmath>
using namespace std;

int getOperand(char ch);
int IsInList(char ch);

char cVar[10];
double dVar[10];
int varNum;

int main ( )
{
char ch;
char op;
int op1;
int op2;
double res;
varNum = 0;

cout<<"You will be inputing numbers associated with variables.\n";
cout<<"For Example a=3, b=2, etc.  Once the values are inputed\n";
cout<<"you are able to input a specific math function, such as\n";
cout<<"a+b, a*b, etc. Please input a value below:\n";
cout<<"\n";
cout<<"\n";

do
{
cout << ">> ";
cin >> ch;
if (ch == 'q')
{
break;
}

op1 = getOperand(ch);

cin >> ch;
if(ch == '=')
{
cin >> dVar[op1];
cout << cVar[op1] << " =\n\n";
cout << "      " << dVar[op1] << "\n\n";
}
else
{
op = ch;

cin >> ch;
op2 = getOperand(ch);

switch(op)
{
case '+':
res = dVar[op1] + dVar[op2];
cout << "Ans = \n\n";
cout << "       " << res << "\n\n";
break;
case '*':
res = dVar[op1] * dVar[op2];
cout << "Ans = \n\n";
cout << "        " << res << "\n\n";
break;
case '/':
res = dVar[op1]/dVar[op2];
cout << "Ans = \n\n";
cout << "        " << res << "\n\n";
break;
case '-':
res = dVar[op1] - dVar[op2];
cout << "Ans = \n\n";
cout << "        " << res << "\n\n";
break;
case '%':
res = fmod(dVar[op1], dVar[op2]);
cout << "Ans = \n\n";
cout << "        " << res << "\n\n";
break;

}
}

}
while (1);

}

int getOperand(char ch)
{
int iVar;
iVar = IsInList(ch);

if (iVar == -1)
{
cVar[varNum] = ch;
iVar = varNum;
varNum++;
}

return iVar;
}

int IsInList(char ch)
{
for (int i = 0; i < 10; i++)
{
if(ch == cVar[i])
{
return i;
}
}

return -1;
}```
I hope that made sense... thanks.

2. I think you need to read in a string, then decide whether that string has the form
value operator value
or
function( value )

Then you can make the appropriate determination in your code.

3. Um, not much sense... You cannot use strings in switch statements, only integer values (and similar). You may want to do some kind of special treatment for these cases:
Code:
```std::string operator;

std::cin >> operator

switch(operator[0]) //Check the first letter
{
case '+':
{
...
break;
}

...

default:
{
if(operator == "sin")
{
...
}
else if(operator == "cos")
{
...
}

...

break;
}
}```

4. I'd just use a number mapped to an operator using a menu:
Code:
```int opNum;
cout << "enter the number next to the operation you want to perform" << endl;
cout << "1) +" << endl;
cout << "2) - " << endl;
cout << "3) %" << endl;
cout << "4) sin" << endl;
cout << "5) sqrt" << endl;
//etc
cin >> opNum;

switch (opNum)
{
case 1:
return op1 + op2;
break;
case 2:
return op1 - op2;
break;
.
.
.
case 4:
return sin(op1);
break;
case 5:
return sqrt(op1);
break;
//etc.
}```
Alternatively, you could declare an enum type to "encapsulate" the values and allow user to type in name of operation to use (instead of indicating the number). Probably other approaches, too.