Advertisement
Bibodui

ООП Лаба 3

Mar 2nd, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.91 KB | None | 0 0
  1. /*Во всех заданиях требуется реализовать по два-три класса.
  2. * Один класс является основным, остальные - вспомогательные.
  3. * Вспомогательные классы должны быть определены как независимые.
  4. * Объекты вспомогательных классов должны использоваться в качестве полей основного класса.
  5. *
  6. *
  7. * Страница 25, номер 45:
  8. * Реализовать класс Bankomat (задание 34), используя для представления суммы класс Money из задания 33.
  9. */
  10.  
  11. #include <iostream>
  12. #include <Windows.h>
  13. #include <math.h>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18.  
  19.  
  20. class Bankomat
  21. {
  22. protected:
  23.     class Money
  24.     {
  25.     public:
  26.         int* ruble10, * ruble50, * ruble100, * ruble500, * ruble1000, * ruble5000;
  27.  
  28.         double* sum;
  29.  
  30.     public:
  31.         Money()//конструктор
  32.         {
  33.             ruble5000 = new int;
  34.             ruble1000 = new int;
  35.             ruble500 = new int;
  36.             ruble100 = new int;
  37.             ruble50 = new int;
  38.             ruble10 = new int;
  39.             sum = new double;
  40.             *ruble5000 = 0;
  41.             *ruble1000 = 0;
  42.             *ruble500 = 0;
  43.             *ruble100 = 0;
  44.             *ruble50 = 0;
  45.             *ruble10 = 0;
  46.             *sum = 0;
  47.         }
  48.         Money(const Money& other)//конструктор копирования
  49.         {
  50.             this->ruble5000 = new int;
  51.             this->ruble1000 = new int;
  52.             this->ruble500 = new int;
  53.             this->ruble100 = new int;
  54.             this->ruble50 = new int;
  55.             this->ruble10 = new int;
  56.             this->sum = new double;
  57.  
  58.             *this->ruble5000 = *other.ruble5000;
  59.             *this->ruble1000 = *other.ruble1000;
  60.             *this->ruble500 = *other.ruble500;
  61.             *this->ruble100 = *other.ruble100;
  62.             *this->ruble50 = *other.ruble50;
  63.             *this->ruble10 = *other.ruble10;
  64.  
  65.             *this->sum = *other.sum;
  66.         }
  67.         ~Money()//деструктор
  68.         {
  69.             delete ruble5000;
  70.             delete ruble1000;
  71.             delete ruble500;
  72.             delete ruble100;
  73.             delete ruble50;
  74.             delete ruble10;
  75.             delete sum;
  76.         }
  77.         Money operator+ (const Money& other)//перегруженный оператор сложения сумм
  78.         {
  79.             Money tmp;
  80.  
  81.             *tmp.ruble5000 = *this->ruble5000 + *other.ruble5000;
  82.             *tmp.ruble1000 = *this->ruble1000 + *other.ruble1000;
  83.             *tmp.ruble500 = *this->ruble500 + *other.ruble500;
  84.             *tmp.ruble100 = *this->ruble100 + *other.ruble100;
  85.             *tmp.ruble50 = *this->ruble50 + *other.ruble50;
  86.             *tmp.ruble10 = *this->ruble10 + *other.ruble10;
  87.             *tmp.sum = *this->sum + *other.sum;
  88.  
  89.             return tmp;
  90.         }
  91.         Money operator- (const Money& other)//перегруженный оператор вычитания сумм
  92.         {
  93.             Money tmp;
  94.  
  95.             *tmp.ruble5000 = *this->ruble5000 - *other.ruble5000;
  96.             *tmp.ruble1000 = *this->ruble1000 - *other.ruble1000;
  97.             *tmp.ruble500 = *this->ruble500 - *other.ruble500;
  98.             *tmp.ruble100 = *this->ruble100 - *other.ruble100;
  99.             *tmp.ruble50 = *this->ruble50 - *other.ruble50;
  100.             *tmp.ruble10 = *this->ruble10 - *other.ruble10;
  101.             *tmp.sum = *this->sum - *other.sum;
  102.  
  103.             return tmp;
  104.         }
  105.         bool operator== (const Money& other)//перегруженный оператор сравнения
  106.         {
  107.             double fraction1, integer1, fraction2, integer2;
  108.             fraction1 = modf(*other.sum, &integer1) * 100;
  109.             fraction2 = modf(*this->sum, &integer2) * 100;
  110.             return (round(fraction1) == round(fraction2) && integer1 == integer2);
  111.         }
  112.         double mult_number(const double number)//умножение суммы на дробное число
  113.         {
  114.             double tmp = *this->sum * number;
  115.             return tmp;
  116.         }
  117.         double div_number(const double number)//деление суммы на дробное число
  118.         {
  119.             double tmp = *this->sum / number;
  120.  
  121.             return tmp;
  122.         }
  123.         double div(const Money& other)//деление сумм
  124.         {
  125.             double tmp = *this->sum / *other.sum;
  126.  
  127.             return tmp;
  128.         }
  129.     };
  130.  
  131. private:
  132.     int *identification_number;
  133.     double *max_amount, *min_amount;
  134.     Money current_amount;
  135.  
  136. public:
  137.     Bankomat()
  138.     {
  139.         identification_number = new int;
  140.         *identification_number = 0;
  141.         max_amount = new double;
  142.         *max_amount = 0;
  143.         min_amount = new double;
  144.         *min_amount = 0;
  145.     }
  146.     ~Bankomat()
  147.     {
  148.         delete identification_number;
  149.         delete max_amount;
  150.         delete min_amount;
  151.     }
  152.     string toString()
  153.     {
  154.         string s1 = to_string(*current_amount.sum);
  155.         s1 = s1.replace(s1.find('.'), 1, ",");
  156.         return s1.erase(s1.find(',')+3,5);
  157.     }
  158.     void add()
  159.     {
  160.         int n;
  161.         cout << "Введите количество купюр номиналом 5000 рублей:" << endl;
  162.         cin >> n;
  163.             *current_amount.ruble5000 += n;
  164.             *current_amount.sum += 5000 * n;
  165.         cout << "Введите количество купюр номиналом 1000 рублей:" << endl;
  166.         cin >> n;
  167.             *current_amount.ruble1000 += n;
  168.             *current_amount.sum += 1000 * n;
  169.         cout << "Введите количество купюр номиналом 500 рублей:" << endl;
  170.         cin >> n;
  171.             *current_amount.ruble500 += n;
  172.             *current_amount.sum += 500 * n;
  173.         cout << "Введите количество купюр номиналом 100 рублей:" << endl;
  174.         cin >> n;
  175.             *current_amount.ruble100 += n;
  176.             *current_amount.sum += 100 * n;
  177.         cout << "Введите количество купюр номиналом 50 рублей:" << endl;
  178.         cin >> n;
  179.             *current_amount.ruble50 += n;
  180.             *current_amount.sum += 50 * n;
  181.         cout << "Введите количество купюр номиналом 10 рублей:" << endl;
  182.         cin >> n;
  183.             *current_amount.ruble10 += n;
  184.             *current_amount.sum += 10 * n;
  185.     }
  186.     void withdraw(int number)
  187.     {
  188.         if (number <= *max_amount && number >= *min_amount)
  189.         {
  190.             while (number != 0)
  191.             {
  192.                 if (number >= 5000 && *current_amount.ruble5000 > 0)
  193.                 {
  194.                     number -= 5000;
  195.                     *current_amount.ruble5000-= 1;
  196.                     *current_amount.sum -= 5000;
  197.                     cout << "Выдано: 5000 рублей" << endl;
  198.                 }
  199.                 else if (number >= 1000 && *current_amount.ruble1000 > 0)
  200.                 {
  201.                     number -= 1000;
  202.                     *current_amount.ruble1000-= 1;
  203.                     *current_amount.sum -= 1000;
  204.                     cout << "Выдано: 1000 рублей" << endl;
  205.                 }
  206.                 else if (number >= 500 && *current_amount.ruble500 > 0)
  207.                 {
  208.                     number -= 500;
  209.                     *current_amount.ruble500-= 1;
  210.                     *current_amount.sum -= 500;
  211.                     cout << "Выдано: 500 рублей" << endl;
  212.                 }
  213.                 else if (number >= 100 && *current_amount.ruble100 > 0)
  214.                 {
  215.                     number -= 100;
  216.                     *current_amount.ruble100-= 1;
  217.                     *current_amount.sum -= 100;
  218.                     cout << "Выдано: 100 рублей" << endl;
  219.                 }
  220.                 else if (number >= 50 && *current_amount.ruble50 > 0)
  221.                 {
  222.                     number -= 50;
  223.                     *current_amount.ruble50-= 1;
  224.                     *current_amount.sum -= 50;
  225.                     cout << "Выдано: 50 рублей" << endl;
  226.                 }
  227.                 else if (number >= 10 && *current_amount.ruble10 > 0)
  228.                 {
  229.                     number -= 10;
  230.                     *current_amount.ruble10-= 1;
  231.                     *current_amount.sum -= 10;
  232.                     cout << "Выдано: 10 рублей" << endl;
  233.                 }
  234.                 else if (number < 10)
  235.                     break;
  236.             }
  237.         }
  238.     }
  239.     friend istream& operator>> (istream& in, Money& other);
  240.     friend ostream& operator<< (ostream& out, const Money& other);
  241.     friend istream& operator>> (istream& in, Bankomat& other);
  242. };
  243.  
  244. istream& operator>> (istream& in, Bankomat& other)
  245. {
  246.     cout << "Введите идентификационный номер:" << endl;
  247.     in >> *other.identification_number;
  248.     in >> other.current_amount;
  249.     cout << "Введите максимальную сумму:" << endl;
  250.     in >> *other.max_amount;
  251.     cout << "Введите минимальную сумму:" << endl;
  252.     in >> *other.min_amount;
  253.  
  254.     return in;
  255. }
  256.  
  257. istream& operator>> (istream& in, Bankomat::Money& other)//перегруженный оператор ввода
  258. {
  259.     cout << "Введите количество купюр номиналом 5000 рублей:" << endl;
  260.     in >> *other.ruble5000;
  261.     cout << "Введите количество купюр номиналом 1000 рублей:" << endl;
  262.     in >> *other.ruble1000;
  263.     cout << "Введите количество купюр номиналом 500 рублей:" << endl;
  264.     in >> *other.ruble500;
  265.     cout << "Введите количество купюр номиналом 100 рублей:" << endl;
  266.     in >> *other.ruble100;
  267.     cout << "Введите количество купюр номиналом 50 рублей:" << endl;
  268.     in >> *other.ruble50;
  269.     cout << "Введите количество купюр номиналом 10 рублей:" << endl;
  270.     in >> *other.ruble10;
  271.  
  272.     *other.sum = 5000 * *other.ruble5000 + 1000 * *other.ruble1000 + 500 * *other.ruble500 + 100 * *other.ruble100 + 50 * *other.ruble50 + 10 * *other.ruble10;
  273.  
  274.     return in;
  275. }
  276.  
  277. ostream& operator<< (ostream& out, const Bankomat::Money& other)//перегруженный оператор вывода
  278. {
  279.     double fraction, integer;
  280.     fraction = modf(*other.sum, &integer) * 100;
  281.    
  282.     //Функция modf() разбивает число на целую и дробную часть.
  283.     //Она возвращает дробную часть и помещает целую часть в переменную integer.
  284.  
  285.     out << integer << ',' << abs(fraction) << endl;
  286.  
  287.     return out;
  288. }
  289.  
  290. int main()
  291. {
  292.     SetConsoleOutputCP(1251);
  293.     SetConsoleCP(1251);
  294.  
  295.     int q;
  296.  
  297.     Bankomat A;
  298.     cin >> A;
  299.     cout << A.toString() << endl;
  300.  
  301.     do
  302.     {
  303.         cout << "Выберите действие:" << endl;
  304.         cout << "1. Добавить деньги в банкомат" << endl;
  305.         cout << "2. Снять деньги из банкомата" << endl;
  306.         cout << "0. Выход" << endl;
  307.         cin >> q;
  308.         if (q == 1)
  309.         {
  310.             A.add();
  311.             cout << "В банкомате осталось:" << endl;
  312.             cout << A.toString() << endl;
  313.         }
  314.         else if (q == 2)
  315.         {
  316.             int number;
  317.             cout << "Введите желаемую сумму:" << endl;
  318.             cin >> number;
  319.             A.withdraw(number);
  320.             cout << "В банкомате осталось:" << endl;
  321.             cout << A.toString() << endl;
  322.         }
  323.     } while (q != 0);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement