Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. // contwork2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <cassert>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10.  
  11. // Account does not manage next, it only stores it
  12. class Account {
  13. public:
  14.     double balance;
  15.     Account* next;
  16.     std::string currency;
  17.  
  18.     virtual ~Account() {}
  19.     Account(double b) : balance(b), next(nullptr) {}
  20.     Account(double b, const std::string& cur) : balance(b), next(nullptr) {
  21.         currency = cur;
  22.     }
  23.     virtual void add_percent() = 0;
  24.  
  25.     virtual Account* copy() = 0;
  26. };
  27.  
  28. class AccountR : public Account {
  29. public:
  30.     AccountR(double b) : Account(b) {}
  31.     AccountR(double b, const std::string& cur) : Account(b, cur) {}
  32.  
  33.     virtual void add_percent() {
  34.         balance *= 1.15;
  35.     }
  36.  
  37.     virtual Account* copy() {
  38.         return new AccountR(balance, currency);
  39.     }
  40. };
  41.  
  42. class AccountD : public Account {
  43. public:
  44.     AccountD(double b) : Account(b) {}
  45.     AccountD(double b, const std::string& cur) : Account(b, cur) {}
  46.  
  47.     virtual void add_percent() {
  48.         balance *= 1.10;
  49.     }
  50.  
  51.     virtual Account* copy() {
  52.         return new AccountD(balance, currency);
  53.     }
  54. };
  55.  
  56. class AccountE : public Account {
  57. public:
  58.     AccountE(double b) : Account(b) {}
  59.     AccountE(double b, const std::string& cur) : Account(b, cur) {}
  60.  
  61.     virtual void add_percent() {
  62.         balance *= 1.08;
  63.     }
  64.  
  65.     virtual Account* copy() {
  66.         return new AccountE(balance, currency);
  67.     }
  68. };
  69.  
  70. class Bank {
  71. public:
  72.     // the last element is the one whose ->next is null
  73.     Account* head;
  74.  
  75.     Bank() : head(nullptr) {}
  76.  
  77.     void add_to_front(Account* what) {
  78.         Account* new_head = what->copy();
  79.         new_head->next = head;
  80.         head = new_head;
  81.     }
  82.  
  83.     void delete_second() {
  84.         assert(head->next != nullptr);
  85.  
  86.         // head is first, head->next is second, head->next->next is third
  87.         // we store third, remove second, and set first->next to third
  88.         Account* third = head->next->next;
  89.         delete head->next;
  90.         head->next = third;
  91.     }
  92.  
  93.     void add_to_back(Account* what) {
  94.         // it is assumed that what != null
  95.         assert(what != nullptr);
  96.         // our list is empty, just add to the front
  97.         if (!head) {
  98.             add_to_front(what);
  99.             return;
  100.         }
  101.         Account* last = head;
  102.  
  103.         while (last->next != nullptr) {
  104.             last = last->next;
  105.         }
  106.         last->next = what->copy();
  107.         last->next->next = nullptr;
  108.     }
  109.  
  110.     void add_100usd_to_back() {
  111.         Account* new_acc = new AccountR(100, "USD");
  112.         add_to_back(new_acc);
  113.         delete new_acc;
  114.     }
  115.  
  116.     void add_1000rub_to_front() {
  117.         Account* new_acc = new AccountE(1000, "RUR");
  118.         add_to_front(new_acc);
  119.         delete new_acc;
  120.     }
  121.  
  122.     // indices start with 0
  123.     void print_eur() {
  124.         Account* cur = head;
  125.         int ind = 0;
  126.         std::cout << "Accounts in euro: " << std::endl;
  127.         while (cur != nullptr) {
  128.             if (cur->currency == "EUR")
  129.                 std::cout << ind << std::endl;
  130.  
  131.             ind++;
  132.             cur = cur->next;
  133.         }
  134.     }
  135.  
  136.     void print_database_to_file(const char* filename) {
  137.         std::ofstream f(filename);
  138.  
  139.         Account* cur = head;
  140.         int ind = 0;
  141.         while (cur != nullptr) {
  142.             f << cur->balance << " " << cur->currency << " " << ind << std::endl;
  143.             ind++;
  144.             cur = cur->next;
  145.         }
  146.     }
  147.  
  148.     void swap_first_and_prelast() {
  149.         assert(head != nullptr);
  150.         assert(head->next != nullptr);
  151.        
  152.         if (head->next->next == nullptr) {
  153.             // we have a list with two elements, do nothing
  154.             return;
  155.         }
  156.         // here we may assume that our list contains no less than three elements
  157.        
  158.         std::vector<Account*> all_elements;
  159.  
  160.         Account* cur = head;
  161.         while (cur != nullptr) {
  162.             all_elements.push_back(cur);
  163.             cur = cur->next;
  164.         }
  165.         int sz = all_elements.size();
  166.  
  167.         Account* old_head_copy = head->copy();
  168.        
  169.         Account* new_head = head->next;
  170.         delete head;
  171.         head = new_head;
  172.  
  173.         Account* old_preend_copy = all_elements[sz - 2]->copy();
  174.         Account* old_end_copy = all_elements[sz - 1]->copy();
  175.         all_elements[sz - 3]->next = nullptr;
  176.         delete all_elements[sz - 1];
  177.         delete all_elements[sz - 2];
  178.        
  179.         add_to_front(old_preend_copy);
  180.         add_to_back(old_head_copy);
  181.         add_to_back(old_end_copy);
  182.  
  183.         delete old_end_copy;
  184.         delete old_preend_copy;
  185.         delete old_head_copy;
  186.     }
  187.  
  188.     ~Bank() {
  189.         if (!head)
  190.             return;
  191.         std::vector<Account*> to_destroy;
  192.         Account* cur = head;
  193.         to_destroy.push_back(head);
  194.         while (cur->next != nullptr) {
  195.             to_destroy.push_back(cur->next);
  196.             cur = cur->next;
  197.         }
  198.  
  199.         for (Account* acc : to_destroy)
  200.             delete acc;
  201.     }
  202. };
  203.  
  204. int _tmain(int argc, _TCHAR* argv[]) {
  205.     Bank my_bank;
  206.     my_bank.add_to_front(new AccountE(1200, "EUR"));
  207.     my_bank.add_to_front(new AccountE(100, "RUB"));
  208.  
  209.     my_bank.add_to_back(new AccountE(1234, "USD"));
  210.     my_bank.add_to_front(new AccountE(123445, "USD"));
  211.     my_bank.add_to_back(new AccountE(1333, "RUB"));
  212.  
  213.  
  214.  
  215.     my_bank.print_database_to_file("s1.txt");
  216.     my_bank.swap_first_and_prelast();
  217.     my_bank.print_database_to_file("s2.txt");
  218.     my_bank.print_eur();
  219.  
  220.  
  221.  
  222.     getchar();
  223.  
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement