Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Во всех заданиях требуется реализовать по два-три класса.
- * Один класс является основным, остальные - вспомогательные.
- * Вспомогательные классы должны быть определены как независимые.
- * Объекты вспомогательных классов должны использоваться в качестве полей основного класса.
- *
- *
- * Страница 25, номер 45:
- * Реализовать класс Bankomat (задание 34), используя для представления суммы класс Money из задания 33.
- */
- #include <iostream>
- #include <Windows.h>
- #include <math.h>
- #include <string>
- using namespace std;
- class Bankomat
- {
- protected:
- class Money
- {
- public:
- int* ruble10, * ruble50, * ruble100, * ruble500, * ruble1000, * ruble5000;
- double* sum;
- public:
- Money()//конструктор
- {
- ruble5000 = new int;
- ruble1000 = new int;
- ruble500 = new int;
- ruble100 = new int;
- ruble50 = new int;
- ruble10 = new int;
- sum = new double;
- *ruble5000 = 0;
- *ruble1000 = 0;
- *ruble500 = 0;
- *ruble100 = 0;
- *ruble50 = 0;
- *ruble10 = 0;
- *sum = 0;
- }
- Money(const Money& other)//конструктор копирования
- {
- this->ruble5000 = new int;
- this->ruble1000 = new int;
- this->ruble500 = new int;
- this->ruble100 = new int;
- this->ruble50 = new int;
- this->ruble10 = new int;
- this->sum = new double;
- *this->ruble5000 = *other.ruble5000;
- *this->ruble1000 = *other.ruble1000;
- *this->ruble500 = *other.ruble500;
- *this->ruble100 = *other.ruble100;
- *this->ruble50 = *other.ruble50;
- *this->ruble10 = *other.ruble10;
- *this->sum = *other.sum;
- }
- ~Money()//деструктор
- {
- delete ruble5000;
- delete ruble1000;
- delete ruble500;
- delete ruble100;
- delete ruble50;
- delete ruble10;
- delete sum;
- }
- Money operator+ (const Money& other)//перегруженный оператор сложения сумм
- {
- Money tmp;
- *tmp.ruble5000 = *this->ruble5000 + *other.ruble5000;
- *tmp.ruble1000 = *this->ruble1000 + *other.ruble1000;
- *tmp.ruble500 = *this->ruble500 + *other.ruble500;
- *tmp.ruble100 = *this->ruble100 + *other.ruble100;
- *tmp.ruble50 = *this->ruble50 + *other.ruble50;
- *tmp.ruble10 = *this->ruble10 + *other.ruble10;
- *tmp.sum = *this->sum + *other.sum;
- return tmp;
- }
- Money operator- (const Money& other)//перегруженный оператор вычитания сумм
- {
- Money tmp;
- *tmp.ruble5000 = *this->ruble5000 - *other.ruble5000;
- *tmp.ruble1000 = *this->ruble1000 - *other.ruble1000;
- *tmp.ruble500 = *this->ruble500 - *other.ruble500;
- *tmp.ruble100 = *this->ruble100 - *other.ruble100;
- *tmp.ruble50 = *this->ruble50 - *other.ruble50;
- *tmp.ruble10 = *this->ruble10 - *other.ruble10;
- *tmp.sum = *this->sum - *other.sum;
- return tmp;
- }
- bool operator== (const Money& other)//перегруженный оператор сравнения
- {
- double fraction1, integer1, fraction2, integer2;
- fraction1 = modf(*other.sum, &integer1) * 100;
- fraction2 = modf(*this->sum, &integer2) * 100;
- return (round(fraction1) == round(fraction2) && integer1 == integer2);
- }
- double mult_number(const double number)//умножение суммы на дробное число
- {
- double tmp = *this->sum * number;
- return tmp;
- }
- double div_number(const double number)//деление суммы на дробное число
- {
- double tmp = *this->sum / number;
- return tmp;
- }
- double div(const Money& other)//деление сумм
- {
- double tmp = *this->sum / *other.sum;
- return tmp;
- }
- };
- private:
- int *identification_number;
- double *max_amount, *min_amount;
- Money current_amount;
- public:
- Bankomat()
- {
- identification_number = new int;
- *identification_number = 0;
- max_amount = new double;
- *max_amount = 0;
- min_amount = new double;
- *min_amount = 0;
- }
- ~Bankomat()
- {
- delete identification_number;
- delete max_amount;
- delete min_amount;
- }
- string toString()
- {
- string s1 = to_string(*current_amount.sum);
- s1 = s1.replace(s1.find('.'), 1, ",");
- return s1.erase(s1.find(',')+3,5);
- }
- void add()
- {
- int n;
- cout << "Введите количество купюр номиналом 5000 рублей:" << endl;
- cin >> n;
- *current_amount.ruble5000 += n;
- *current_amount.sum += 5000 * n;
- cout << "Введите количество купюр номиналом 1000 рублей:" << endl;
- cin >> n;
- *current_amount.ruble1000 += n;
- *current_amount.sum += 1000 * n;
- cout << "Введите количество купюр номиналом 500 рублей:" << endl;
- cin >> n;
- *current_amount.ruble500 += n;
- *current_amount.sum += 500 * n;
- cout << "Введите количество купюр номиналом 100 рублей:" << endl;
- cin >> n;
- *current_amount.ruble100 += n;
- *current_amount.sum += 100 * n;
- cout << "Введите количество купюр номиналом 50 рублей:" << endl;
- cin >> n;
- *current_amount.ruble50 += n;
- *current_amount.sum += 50 * n;
- cout << "Введите количество купюр номиналом 10 рублей:" << endl;
- cin >> n;
- *current_amount.ruble10 += n;
- *current_amount.sum += 10 * n;
- }
- void withdraw(int number)
- {
- if (number <= *max_amount && number >= *min_amount)
- {
- while (number != 0)
- {
- if (number >= 5000 && *current_amount.ruble5000 > 0)
- {
- number -= 5000;
- *current_amount.ruble5000-= 1;
- *current_amount.sum -= 5000;
- cout << "Выдано: 5000 рублей" << endl;
- }
- else if (number >= 1000 && *current_amount.ruble1000 > 0)
- {
- number -= 1000;
- *current_amount.ruble1000-= 1;
- *current_amount.sum -= 1000;
- cout << "Выдано: 1000 рублей" << endl;
- }
- else if (number >= 500 && *current_amount.ruble500 > 0)
- {
- number -= 500;
- *current_amount.ruble500-= 1;
- *current_amount.sum -= 500;
- cout << "Выдано: 500 рублей" << endl;
- }
- else if (number >= 100 && *current_amount.ruble100 > 0)
- {
- number -= 100;
- *current_amount.ruble100-= 1;
- *current_amount.sum -= 100;
- cout << "Выдано: 100 рублей" << endl;
- }
- else if (number >= 50 && *current_amount.ruble50 > 0)
- {
- number -= 50;
- *current_amount.ruble50-= 1;
- *current_amount.sum -= 50;
- cout << "Выдано: 50 рублей" << endl;
- }
- else if (number >= 10 && *current_amount.ruble10 > 0)
- {
- number -= 10;
- *current_amount.ruble10-= 1;
- *current_amount.sum -= 10;
- cout << "Выдано: 10 рублей" << endl;
- }
- else if (number < 10)
- break;
- }
- }
- }
- friend istream& operator>> (istream& in, Money& other);
- friend ostream& operator<< (ostream& out, const Money& other);
- friend istream& operator>> (istream& in, Bankomat& other);
- };
- istream& operator>> (istream& in, Bankomat& other)
- {
- cout << "Введите идентификационный номер:" << endl;
- in >> *other.identification_number;
- in >> other.current_amount;
- cout << "Введите максимальную сумму:" << endl;
- in >> *other.max_amount;
- cout << "Введите минимальную сумму:" << endl;
- in >> *other.min_amount;
- return in;
- }
- istream& operator>> (istream& in, Bankomat::Money& other)//перегруженный оператор ввода
- {
- cout << "Введите количество купюр номиналом 5000 рублей:" << endl;
- in >> *other.ruble5000;
- cout << "Введите количество купюр номиналом 1000 рублей:" << endl;
- in >> *other.ruble1000;
- cout << "Введите количество купюр номиналом 500 рублей:" << endl;
- in >> *other.ruble500;
- cout << "Введите количество купюр номиналом 100 рублей:" << endl;
- in >> *other.ruble100;
- cout << "Введите количество купюр номиналом 50 рублей:" << endl;
- in >> *other.ruble50;
- cout << "Введите количество купюр номиналом 10 рублей:" << endl;
- in >> *other.ruble10;
- *other.sum = 5000 * *other.ruble5000 + 1000 * *other.ruble1000 + 500 * *other.ruble500 + 100 * *other.ruble100 + 50 * *other.ruble50 + 10 * *other.ruble10;
- return in;
- }
- ostream& operator<< (ostream& out, const Bankomat::Money& other)//перегруженный оператор вывода
- {
- double fraction, integer;
- fraction = modf(*other.sum, &integer) * 100;
- //Функция modf() разбивает число на целую и дробную часть.
- //Она возвращает дробную часть и помещает целую часть в переменную integer.
- out << integer << ',' << abs(fraction) << endl;
- return out;
- }
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int q;
- Bankomat A;
- cin >> A;
- cout << A.toString() << endl;
- do
- {
- cout << "Выберите действие:" << endl;
- cout << "1. Добавить деньги в банкомат" << endl;
- cout << "2. Снять деньги из банкомата" << endl;
- cout << "0. Выход" << endl;
- cin >> q;
- if (q == 1)
- {
- A.add();
- cout << "В банкомате осталось:" << endl;
- cout << A.toString() << endl;
- }
- else if (q == 2)
- {
- int number;
- cout << "Введите желаемую сумму:" << endl;
- cin >> number;
- A.withdraw(number);
- cout << "В банкомате осталось:" << endl;
- cout << A.toString() << endl;
- }
- } while (q != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement