#include #include #include #include #include #include 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"<> 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" <