Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. stack<double> wynik;
  7. enum dzialania{DODAWANIE=1,ODEJMOWANIE=2,MNOZENIE=3,DZIELENIE=4,CZYSZCZENIE=5};
  8.  
  9. float dodaj(double x)
  10. {
  11.     return wynik.top()+x;
  12. }
  13.  
  14. float odejmij(double x)
  15. {
  16.     return wynik.top()-x;
  17. }
  18.  
  19. float pomnoz(double x)
  20. {
  21.     return wynik.top()*x;
  22. }
  23.  
  24. float podziel(double x)
  25. {
  26.     return wynik.top()/x;
  27. }
  28.  
  29. void wyczysc()
  30. {
  31.     while(!wynik.empty())
  32.     {
  33.         wynik.pop();
  34.     }
  35.     wynik.push(0);
  36. }
  37.  
  38. void wykonajDzialanie(int a, float b)
  39. {
  40.     switch(a)
  41.     {
  42.     case DODAWANIE:
  43.         wynik.push(dodaj(b));
  44.         break;
  45.     case ODEJMOWANIE:
  46.         wynik.push(odejmij(b));
  47.         break;
  48.     case MNOZENIE:
  49.         wynik.push(pomnoz(b));
  50.         break;
  51.     case DZIELENIE:
  52.         wynik.push(podziel(b));
  53.         break;
  54.     case CZYSZCZENIE:
  55.         wyczysc();
  56.         break;
  57.     }
  58. }
  59.  
  60. void menu_startowe(double &x, int & y)
  61. {
  62.     cout << "KALKULATOR" << endl;
  63.     cout << "--------------------------------------------------------------" << endl;
  64.     cout << "1.Dodawanie 2.Odejmowanie 3.Mnozenie 4.Dzielenie 5.Czyszczenie" << endl;
  65.     cout << "--------------------------------------------------------------" << endl;
  66.     cout << "WYNIK: "<<wynik.top()<<endl;
  67.     cout << "Podaj liczbe: ";
  68.     cin >> x;
  69.     wynik.push(x);
  70.     cout <<"Jakie dzialanie: ";
  71.     cin >> y;
  72.     cout <<"Podaj liczbe: ";
  73.     cin >> x;
  74.  
  75. }
  76.  
  77. void menu(double &x, int &y)    //uzyskanie dostêpu do oryginalnych wartoœci a nie ich kopii
  78. {
  79.     cout<<endl;
  80.     cout << "KALKULATOR" << endl;
  81.     cout << "--------------------------------------------------------------" << endl;
  82.     cout << "1.Dodawanie 2.Odejmowanie 3.Mnozenie 4.Dzielenie 5.Czyszczenie" << endl;
  83.     cout << "--------------------------------------------------------------" << endl;
  84.     cout <<"WYNIK: "<<wynik.top()<<endl;
  85.     cout <<"Jakie dzialanie: ";
  86.     cin >> y;
  87.     if(y!=5)
  88.     {
  89.         cout <<"Podaj liczbe: ";
  90.         cin >> x;
  91.     }
  92. }
  93.  
  94. //główna funkcja programu//
  95. int main()
  96. {
  97.     double a;
  98.     int wybor;
  99.     wynik.push(0);
  100.  
  101.     for(;;)
  102.     {
  103.         if(wynik.top()==0)
  104.         {
  105.             menu_startowe(a,wybor);
  106.             wykonajDzialanie(wybor,a);
  107.         } else
  108.         {
  109.             menu(a,wybor);
  110.             wykonajDzialanie(wybor,a);
  111.         }
  112.  
  113.     }
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement