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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
using namespace std;
class Calculator
{
//private properties to protect data
private:
float answer;
float memory;
//public properties to allow input and access to private properties
public:
float Add(float, float);
float Subtract(float, float);
float Multiply(float, float);
float Divide(float, float);
float GetAnswer();
float GetMemoryNumber();
void SetMemoryNumber(float);
};
//functions declared:
float Calculator::GetAnswer()
{
return answer;
main();
}
float Calculator::GetMemoryNumber()
{
return memory;
}
//set memory to be able to integrate in calculations
void Calculator::SetMemoryNumber(float x)
{
memory = x;
}
//addition function
float Calculator::Add(float x, float y)
{
answer = (x+y);
}
//subtraction function
float Calculator::Subtract(float x,float y)
{
answer = (x-y);
}
//multiplication function
float Calculator::Multiply(float x,float y)
{
answer = (x*y);
}
//division function
float Calculator::Divide(float x,float y)
{
answer = (x/y);
}
int main()
{
Calculator calc;
float x;
float y;
string op;
string func;
cout <<"\n*Possible operations are: (+, -, *, / mem, clearmem, prime)*\n" << endl;
cout <<"Please enter function: " << endl;
cin >> op;
if (op == "mem")
{
cout << "\nMEM = " << calc.GetMemoryNumber() << endl;
cout << "\nEnter operation then second number or q to quit:\n";
cin >> op >> y;
}
else if (op == "setmem")
{
cout << "\nEnter MEM = ";
cin >> x;
calc.SetMemoryNumber(x);
cout << "MEM set to: " << calc.GetMemoryNumber() <<endl;
cout << "\nEnter operation then second number or q to quit:\n";
cin >> op >> y;
}
if (op == "+")
{
calc.Add(x,y);
cout << "\nAnswer = " << calc.GetAnswer() << endl;
}
else if (op == "-")
{
calc.Subtract(x,y);
cout << "\nAnswer = " << calc.GetAnswer() << endl;
}
else if (op == "*")
{
calc.Multiply(x,y);
cout << "\nAnswer = " << calc.GetAnswer() << endl;
}
else if (op == "/")
{
calc.Divide(x,y);
cout << "\nAnswer = " << calc.GetAnswer() << endl;
}
return 0;
}
|