#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
#include <stdio.h>
#include <cmath>
using namespace std;
/*deklaracja zmiennych statycznych stalych przez slowa kluczowe "static" i "const".
Slowo kluczowe "const" sprawi, ¿e zmiennej nie bêdzie mo¿na zmodyfikowac w trakcie trwania programu,
natomiast "static" oznacza, ze zmienne te beda wspólne dla wszystkich klas, poniewa¿ zainicjowane zostaly globalnie */
static const int FIRST_OPERATION_ID = 1;
static const int LAST_OPERATION_ID = 9;
class UserInput
{ /*modyfikator dostepu private, powoduje że dostęp do zmiennych zadeklarowanych w tej klasie
maja dostęp tylko obiekty tej klasy */
private:
string inputValue;
string initialMessage;
string errorMessage;
double parsedValue;
/*modyfikator public sprawia że zmienne z tej klasy są dostępne z każdego miejsca w kodzie */
public:
UserInput(string initialMessage, string errorMessage) : initialMessage(initialMessage), errorMessage(errorMessage) {}
// pobiera wartosc od uzytkownika i konwertuje string -> liczba,
// wczytuje do momentu az uzytkownik wprowadzi poprawna liczbe
void getInputValue()
{
while (1)
{
cout << initialMessage;
cin >> inputValue;
stringstream input(inputValue);
if (input >> parsedValue)
{
if (input.eof())
{
break;
}
}
cout << errorMessage << endl;
}
}
//pobierz wartosc liczbowa ktora uzytkownik wprowadzil
double getValue()
{
return parsedValue;
}
};
int main()
{
int wybor;
double result;
// inicjalizujemy wszystkie zmienne
UserInput a = UserInput("a:", "a is not a number!");
UserInput b = UserInput("b:", "b is not a number!");
//glowna petla programu
while(true)
{ cout<< "Title: Multifunction calculator"<<endl;
cout<<"Author: Patryk Bartmanowicz"<<endl;
cout<<"Program creation date: 4.11.2020\n\n"<<endl;
cout << "Multifunction calculator" << endl;
cout << endl;
cout << "[1] Addition" << endl;
cout << "[2] Subtraction" << endl;
cout << "[3] Multiplication" << endl;
cout << "[4] Division" << endl;
cout << "[5] Inverse" << endl;
cout << "[6] Sin"<<endl;
cout << "[7] Cos"<<endl;
cout << "[8] Tan"<<endl;
cout << "[9] Ctan"<<endl;
cout << "[10] Exit" << endl;
cout << "" << endl;
//wybieramy z menu jakie obliczenia chcemy wykonac, lub wyjsc z programu
cout << "Choose: ";
cin >> wybor;
if(wybor >= FIRST_OPERATION_ID && wybor <= LAST_OPERATION_ID)
{
//uzycie instrukcji switch, warunek wieloktrotnego wyboru, kazdy case odpowiada osobnym obliczeniom
switch(wybor)
{
case 1:
a.getInputValue();
b.getInputValue();
cout<<"Sum = ";
cout << "a" << " + " << "b" << " = " << a.getValue() << " + "
<< b.getValue() << " = " << a.getValue() + b.getValue() << "\n";
break;
case 2:
a.getInputValue();
b.getInputValue();
cout<<"Difference: ";
cout << "a" << " - " << "b" << " = " << a.getValue() << " - "
<< b.getValue() << " = " << a.getValue() - b.getValue() << "\n";
break;
case 3:
a.getInputValue();
b.getInputValue();
cout<<"The result of the multiplication = ";
cout << "a" << " * " << "b" << " = " << a.getValue() << " * "
<< b.getValue() << " = " << a.getValue() * b.getValue() << "\n";
break;
case 4:
a.getInputValue();
b.getInputValue();
if(b.getValue() == 0) cout <<"Wrong! Division by zero!" << endl;
else cout<<"The result of the division = " << "a" << " / " << "b" << " = "
<< a.getValue() << " / "<< b.getValue() << " = " << a.getValue() / b.getValue() << "\n";
break;
case 5:
a.getInputValue();
if(a.getValue()==0) cout << "Does not exist";
else cout << "The opposite number: " << 1/a.getValue() ;
break;
case 6:
a.getInputValue();
result = sin(a.getValue());
cout << "sin(a) = " << result << endl;
break;
case 7:
a.getInputValue();
result = cos(a.getValue());
cout << "cos(a) = " << result << endl;
break;
case 8:
a.getInputValue();
result = tan(a.getValue());
cout << "tan(a) = " << result;
break;
case 9:
a.getInputValue();
result = 1/tan(a.getValue());
cout << "ctg(a) = " << result;
break;
default:
cout << "Wrong" <<endl;
break;
//czyszczenie ekranu i powrot do poczatkowego menu
system("cls");
}
} //instrukcja else if pozwala nam po wyborze nr 10 wyjsc z programu
else if (wybor == 10) {
exit(0);
}
else
{
cin.clear();
cout<<"Wrong choice!"<<endl;
}
getchar();getchar();
system("cls");
}
return 0;
}