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.