Facebook
From Whipped Mockingjay, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 60
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <conio.h>
  5. #include <stdio.h>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. /*deklaracja zmiennych statycznych stalych przez slowa kluczowe "static" i "const".
  11. Slowo kluczowe "const" sprawi, ¿e zmiennej nie bêdzie mo¿na zmodyfikowac w trakcie trwania programu,
  12. natomiast "static" oznacza, ze zmienne te beda wspólne dla wszystkich klas, poniewa¿ zainicjowane zostaly globalnie */
  13.  
  14. static const int FIRST_OPERATION_ID = 1;
  15. static const int LAST_OPERATION_ID = 9;
  16.  
  17. class UserInput
  18.  
  19. {  /*modyfikator dostepu private, powoduje że dostęp do zmiennych zadeklarowanych w tej klasie
  20.      maja dostęp tylko obiekty tej klasy */
  21.  
  22.     private:
  23.         string inputValue;
  24.         string initialMessage;
  25.         string errorMessage;
  26.         double parsedValue;
  27.  
  28.    /*modyfikator public sprawia że zmienne z tej klasy są dostępne z każdego miejsca w kodzie */
  29.  
  30.     public:
  31.         UserInput(string initialMessage, string errorMessage) : initialMessage(initialMessage), errorMessage(errorMessage) {}
  32.  
  33.         // pobiera wartosc od uzytkownika i konwertuje string -> liczba,
  34.         // wczytuje do momentu az uzytkownik wprowadzi poprawna liczbe
  35.  
  36.         void getInputValue()
  37.         {
  38.             while (1)
  39.             {
  40.                 cout << initialMessage;
  41.                 cin >> inputValue;
  42.                 stringstream input(inputValue);
  43.  
  44.                 if (input >> parsedValue)
  45.                 {
  46.                     if (input.eof())
  47.                     {
  48.                         break;
  49.                     }
  50.                 }
  51.                 cout << errorMessage << endl;
  52.             }
  53.         }
  54.  
  55.         //pobierz wartosc liczbowa ktora uzytkownik wprowadzil
  56.         double getValue()
  57.         {
  58.             return parsedValue;
  59.         }
  60. };
  61.  
  62. int main()
  63. {
  64.     int wybor;
  65.     double result;
  66.    // inicjalizujemy wszystkie zmienne
  67.     UserInput a = UserInput("a:", "a is not a number!");
  68.     UserInput b = UserInput("b:", "b is not a number!");
  69.  
  70. //glowna petla programu
  71.     while(true)
  72.     {   cout<< "Title: Multifunction calculator"<<endl;
  73.         cout<<"Author: Patryk Bartmanowicz"<<endl;
  74.         cout<<"Program creation date: 4.11.2020nn"<<endl;
  75.  
  76.         cout << "Multifunction calculator" << endl;
  77.         cout << endl;
  78.         cout << "[1] Addition" << endl;
  79.         cout << "[2] Subtraction" << endl;
  80.         cout << "[3] Multiplication" << endl;
  81.         cout << "[4] Division" << endl;
  82.         cout << "[5] Inverse" << endl;
  83.         cout << "[6] Sin"<<endl;
  84.         cout << "[7] Cos"<<endl;
  85.         cout << "[8] Tan"<<endl;
  86.         cout << "[9] Ctan"<<endl;
  87.         cout << "[10] Exit" << endl;
  88.         cout << "" << endl;
  89.  
  90. //wybieramy z menu jakie obliczenia chcemy wykonac, lub wyjsc z programu
  91.  
  92.         cout << "Choose: ";
  93.         cin >> wybor;
  94.  
  95.  
  96.         if(wybor >= FIRST_OPERATION_ID && wybor <= LAST_OPERATION_ID)
  97.  
  98.         {
  99.             //uzycie instrukcji switch, warunek wieloktrotnego wyboru, kazdy case odpowiada osobnym obliczeniom
  100.             switch(wybor)
  101.             {
  102.                 case 1:
  103.                     a.getInputValue();
  104.                     b.getInputValue();
  105.                     cout<<"Sum = ";
  106.                     cout << "a" << " + " << "b" << " = " << a.getValue() << " + "
  107.                     << b.getValue() << " = " << a.getValue() + b.getValue() << "n";
  108.                     break;
  109.                 case 2:
  110.                     a.getInputValue();
  111.                     b.getInputValue();
  112.                     cout<<"Difference: ";
  113.                     cout << "a" << " - " << "b" << " = " << a.getValue() << " - "
  114.                     << b.getValue() << " = " << a.getValue() - b.getValue() << "n";
  115.                     break;
  116.                 case 3:
  117.                     a.getInputValue();
  118.                     b.getInputValue();
  119.                     cout<<"The result of the multiplication = ";
  120.                     cout << "a" << " * " << "b" << " = " << a.getValue() << " * "
  121.                     << b.getValue() << " = " << a.getValue() * b.getValue() << "n";
  122.                     break;
  123.                 case 4:
  124.                     a.getInputValue();
  125.                     b.getInputValue();
  126.                     if(b.getValue() == 0) cout <<"Wrong! Division by zero!" << endl;
  127.                     else cout<<"The result of the division = " << "a" << " / " << "b" << " = "
  128.                     << a.getValue() << " / "<< b.getValue() << " = " << a.getValue() / b.getValue() << "n";
  129.                     break;
  130.                 case 5:
  131.                     a.getInputValue();
  132.                     if(a.getValue()==0) cout << "Does not exist";
  133.                     else cout << "The opposite number: " << 1/a.getValue() ;
  134.                     break;
  135.                 case 6:
  136.                     a.getInputValue();
  137.                     result = sin(a.getValue());
  138.                     cout << "sin(a) = " << result << endl;
  139.                     break;
  140.                 case 7:
  141.                     a.getInputValue();
  142.                     result = cos(a.getValue());
  143.                     cout << "cos(a) = " << result << endl;
  144.                     break;
  145.                 case 8:
  146.                     a.getInputValue();
  147.                     result = tan(a.getValue());
  148.                     cout << "tan(a) = " << result;
  149.                      break;
  150.                 case 9:
  151.                     a.getInputValue();
  152.                     result = 1/tan(a.getValue());
  153.                     cout << "ctg(a) = " << result;
  154.                      break;
  155.                 default:
  156.                     cout << "Wrong" <<endl;
  157.                     break;
  158.                     //czyszczenie ekranu i powrot do poczatkowego menu
  159.  
  160.                     system("cls");
  161.             }
  162.         }   //instrukcja else if pozwala nam po wyborze nr 10 wyjsc z programu
  163.             else if (wybor == 10) {
  164.             exit(0);
  165.         }
  166.             else
  167.         {
  168.             cin.clear();
  169.             cout<<"Wrong choice!"<<endl;
  170.         }
  171.             getchar();getchar();
  172.             system("cls");
  173.     }
  174.     return 0;
  175. }
  176.