1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <math.h>
using namespace std;
bool math(char op, float x, float y, float& res);
void main()
{
char op;
int val;
float x, y, res=0;
cout << " enter the desired operator: * + / s - , and two numbers: \n";
cin >> op >> x >> y;
bool math(char op, float x, float y, float &res);
if (math == 0)
cout << "unable to calculate result due to wrong parameters. please try again";
else
cout << "your result is " << res << endl;
}
bool math(char op, float x, float y, float &res)
{
if (op != '+', '*', '/', '-', 's')
return false;
else
{
switch (op)
{
case '+':
res = x + y;
return true;
break;
case '*':
res = x * y;
return true;
break;
case '/':
if (y = 0)
{
return false;
break;
}
else
{
res = x / y;
return true;
break;
}
case '-':
res = x - y;
return true;
break;
case 's':
if (x < 0)
{
return false;
break;
}
else
{
res = sqrt(x);
return true;
break;
}
}
}
}
|