Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // contwork2.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <vector>
- #include <cassert>
- #include <iostream>
- #include <fstream>
- #include <string>
- // Account does not manage next, it only stores it
- class Account {
- public:
- double balance;
- Account* next;
- std::string currency;
- virtual ~Account() {}
- Account(double b) : balance(b), next(nullptr) {}
- Account(double b, const std::string& cur) : balance(b), next(nullptr) {
- currency = cur;
- }
- virtual void add_percent() = 0;
- virtual Account* copy() = 0;
- };
- class AccountR : public Account {
- public:
- AccountR(double b) : Account(b) {}
- AccountR(double b, const std::string& cur) : Account(b, cur) {}
- virtual void add_percent() {
- balance *= 1.15;
- }
- virtual Account* copy() {
- return new AccountR(balance, currency);
- }
- };
- class AccountD : public Account {
- public:
- AccountD(double b) : Account(b) {}
- AccountD(double b, const std::string& cur) : Account(b, cur) {}
- virtual void add_percent() {
- balance *= 1.10;
- }
- virtual Account* copy() {
- return new AccountD(balance, currency);
- }
- };
- class AccountE : public Account {
- public:
- AccountE(double b) : Account(b) {}
- AccountE(double b, const std::string& cur) : Account(b, cur) {}
- virtual void add_percent() {
- balance *= 1.08;
- }
- virtual Account* copy() {
- return new AccountE(balance, currency);
- }
- };
- class Bank {
- public:
- // the last element is the one whose ->next is null
- Account* head;
- Bank() : head(nullptr) {}
- void add_to_front(Account* what) {
- Account* new_head = what->copy();
- new_head->next = head;
- head = new_head;
- }
- void delete_second() {
- assert(head->next != nullptr);
- // head is first, head->next is second, head->next->next is third
- // we store third, remove second, and set first->next to third
- Account* third = head->next->next;
- delete head->next;
- head->next = third;
- }
- void add_to_back(Account* what) {
- // it is assumed that what != null
- assert(what != nullptr);
- // our list is empty, just add to the front
- if (!head) {
- add_to_front(what);
- return;
- }
- Account* last = head;
- while (last->next != nullptr) {
- last = last->next;
- }
- last->next = what->copy();
- last->next->next = nullptr;
- }
- void add_100usd_to_back() {
- Account* new_acc = new AccountR(100, "USD");
- add_to_back(new_acc);
- delete new_acc;
- }
- void add_1000rub_to_front() {
- Account* new_acc = new AccountE(1000, "RUR");
- add_to_front(new_acc);
- delete new_acc;
- }
- // indices start with 0
- void print_eur() {
- Account* cur = head;
- int ind = 0;
- std::cout << "Accounts in euro: " << std::endl;
- while (cur != nullptr) {
- if (cur->currency == "EUR")
- std::cout << ind << std::endl;
- ind++;
- cur = cur->next;
- }
- }
- void print_database_to_file(const char* filename) {
- std::ofstream f(filename);
- Account* cur = head;
- int ind = 0;
- while (cur != nullptr) {
- f << cur->balance << " " << cur->currency << " " << ind << std::endl;
- ind++;
- cur = cur->next;
- }
- }
- void swap_first_and_prelast() {
- assert(head != nullptr);
- assert(head->next != nullptr);
- if (head->next->next == nullptr) {
- // we have a list with two elements, do nothing
- return;
- }
- // here we may assume that our list contains no less than three elements
- std::vector<Account*> all_elements;
- Account* cur = head;
- while (cur != nullptr) {
- all_elements.push_back(cur);
- cur = cur->next;
- }
- int sz = all_elements.size();
- Account* old_head_copy = head->copy();
- Account* new_head = head->next;
- delete head;
- head = new_head;
- Account* old_preend_copy = all_elements[sz - 2]->copy();
- Account* old_end_copy = all_elements[sz - 1]->copy();
- all_elements[sz - 3]->next = nullptr;
- delete all_elements[sz - 1];
- delete all_elements[sz - 2];
- add_to_front(old_preend_copy);
- add_to_back(old_head_copy);
- add_to_back(old_end_copy);
- delete old_end_copy;
- delete old_preend_copy;
- delete old_head_copy;
- }
- ~Bank() {
- if (!head)
- return;
- std::vector<Account*> to_destroy;
- Account* cur = head;
- to_destroy.push_back(head);
- while (cur->next != nullptr) {
- to_destroy.push_back(cur->next);
- cur = cur->next;
- }
- for (Account* acc : to_destroy)
- delete acc;
- }
- };
- int _tmain(int argc, _TCHAR* argv[]) {
- Bank my_bank;
- my_bank.add_to_front(new AccountE(1200, "EUR"));
- my_bank.add_to_front(new AccountE(100, "RUB"));
- my_bank.add_to_back(new AccountE(1234, "USD"));
- my_bank.add_to_front(new AccountE(123445, "USD"));
- my_bank.add_to_back(new AccountE(1333, "RUB"));
- my_bank.print_database_to_file("s1.txt");
- my_bank.swap_first_and_prelast();
- my_bank.print_database_to_file("s2.txt");
- my_bank.print_eur();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement