#include <iostream.h>
#include <math.h>
#include <string.h>
int nVar = 0;
char varName[50];
double varValue[50];
double getValue(char ch);
void setValue(char ch, double d);
int main()
{
cout << "This program enables the user to perform Math Functions such as fabs, sqrt, pow, log, log10, sin cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, ceil, floor, mod. Just input a character(a - z ) followed by an equal sign and the numerical value then press enter. Then in the preceding line input the function you want to use followed by a parenthesis and the alpha character then press enter. To quit, type quit then press enter:\n\n\n";
for(int i = 0; i < 50; i++)
{
varName[i] = '(';
}
char ch;
char input[6];
double operand1;
double operand2;
double res;
while(1)
{
cout << ">>";
cin >> ch;
if(ch =='q'|| ch =='Q')
{
break;
}
if(ch =='quit')
{
break;
}
input[0] = ch;
operand1 = getValue(ch);
cin >> ch;
input[1] = ch;
if(ch == '=')
{
cin >> operand1;
setValue(input[0], operand1);
cout << input[0] << "=\n\n\n\t" <<operand1 << "\n";
}
else
{
switch(ch)
{
case'+':
cin >> ch;
operand2 = getValue(ch);
res = operand1 + operand2;
cout << "ans=\n\n\n\t" << res << "\n";
break;
case'-':
cin >> ch;
operand2 = getValue(ch);
res = operand1 - operand2;
cout << "ans=\n\n\n\t" << res << "\n";
break;
case'*':
cin >> ch;
operand2 = getValue(ch);
res = operand1 * operand2;
cout << "ans=\n\n\n\t" << res << "\n";
break;
case'/':
cin >> ch;
operand2 = getValue(ch);
res = operand1 / operand2;
cout << "ans=\n\n\n\t" << res << "\n";
break;
default:
cin >> input[2];
int i = 3;
while (ch != '(')
{
cin >> ch;
input [i] = ch;
i++;
}
input[i-1] = '\0';
if(strcmp(input, "sin") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = sin(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "cos") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = cos(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "tan") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = tan(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "exp") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = exp(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "log") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = log(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "pow") == 0)
{
cin >> ch;
operand1 = getValue(ch);
cin >> ch;
cin >> ch;
operand2 = getValue(ch);
res = pow(operand1, operand2);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "asin") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = asin(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "acos") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = acos(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "atan") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = atan(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "fabs") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = fabs(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "sqrt") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = sqrt(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "ceil") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = ceil(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "sinh") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = sinh(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "cosh") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = cosh(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "tanh") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = tanh(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "fmod") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = fmod(operand1, operand2);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "floor") == 0)
{
cin >> ch;
operand1 = getValue(ch);
res = floor(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "log10") == 0)
{
cin >> ch;
//cin >> ch;
operand1 = getValue(ch);
res = log10(operand1);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
if(strcmp(input, "atan2") == 0)
{
cin >> ch;
operand1 = getValue(ch);
cin >> ch;
cin >> ch;
res = atan2(operand1, operand2);
cout << "ans=\n\n\n\t" << res << "\n";
cin >> ch;
}
break;
}
}
}
return 0;
}
double getValue(char ch)
{
for(int i = 0; i < nVar; i++)
{
if(ch == varName[i])
{
return varValue[i];
}
}
varName[nVar] = ch;
varValue[nVar] = 0.0;
nVar++;
return 0.0;
}
void setValue(char ch, double d)
{
for(int i = 0; i <nVar; i++)
{
if(ch == varName[i])
{
varValue[i] = d;
}
}
}