Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.25 KB | None | 0 0
  1. #ifndef __PROGTEST__
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <cassert>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <sstream>
  11. using namespace std;
  12. #endif /* __PROGTEST__ */
  13.  
  14. bool equalString(const char* a, const char* b)
  15. {
  16.     bool res = true;
  17.     for (size_t i = 0; res && (a[i] != '\0' || b[i] != '\0'); ++i)
  18.     {
  19.         res = (a[i] == b[i]);
  20.     }
  21.     return res;
  22. }
  23.  
  24. void copyChar(const char* source, char*& acceptor)
  25. {
  26.     size_t lenSource = strlen(source);
  27.     delete[] acceptor;
  28.     acceptor = new char[lenSource + 1];
  29.     for (size_t i = 0; i <= lenSource; ++i)
  30.     {
  31.         acceptor[i] = source[i];
  32.     }
  33. }
  34.  
  35. template <class T>
  36. void copyData(const T* source, T*& acceptor, size_t lenSource)
  37. {
  38.     delete[] acceptor;
  39.     acceptor = new T[lenSource];
  40.     for (size_t i = 0; i < lenSource; ++i)
  41.     {
  42.         acceptor[i] = source[i];
  43.     }
  44. }
  45.  
  46. template <class T>
  47. void pushBackHeapArray(T*& heapArr, size_t& size, const T& newElem)
  48. {
  49.     T* newHeapArr = new T[size + 1];
  50.     for (size_t i = 0; i < size; ++i)
  51.     {
  52.         newHeapArr[i] = heapArr[i];
  53.     }
  54.     newHeapArr[size] = newElem;
  55.     delete[] heapArr;
  56.     heapArr = newHeapArr;
  57.     ++size;
  58. }
  59.  
  60. struct account
  61. {
  62.     struct trans
  63.     {
  64.         char* credID_;
  65.         int amount_;
  66.         char* sign_;
  67.  
  68.         trans() : credID_(nullptr), amount_(0), sign_(nullptr) {}
  69.        
  70.         trans(const char* credID, int amount, const char* sign) : amount_(amount), credID_(nullptr), sign_(nullptr)
  71.         {
  72.             copyChar(credID, credID_);
  73.             copyChar(sign, sign_);
  74.         }
  75.  
  76.         ~trans()
  77.         {
  78.             clear();
  79.         }
  80.  
  81.         trans& operator=(const trans& right)
  82.         {
  83.             clear();
  84.  
  85.             copyChar(right.credID_, credID_);
  86.             copyChar(right.sign_, sign_);
  87.  
  88.             amount_ = right.amount_;
  89.  
  90.             return *this;
  91.         }
  92.  
  93.         void clear()
  94.         {
  95.             delete[] credID_;
  96.             credID_ = nullptr;
  97.             delete[] sign_;
  98.             sign_ = nullptr;
  99.         }
  100.     };
  101.  
  102.     trans* moves_;
  103.     size_t countofmoves_;
  104.     char* accID_;
  105.     int initialBalance_;
  106.     int balance_;
  107.  
  108.     account() : moves_(nullptr), countofmoves_(0), accID_(nullptr), initialBalance_(0), balance_(0) {}
  109.  
  110.     account(const char* accID, int initialBalance) : moves_(nullptr), countofmoves_(0), initialBalance_(initialBalance), balance_(initialBalance), accID_(nullptr)
  111.     {
  112.         copyChar(accID, accID_);
  113.     }
  114.  
  115.     ~account()
  116.     {
  117.         clear();
  118.     }
  119.  
  120.     account& operator=(const account& right)
  121.     {
  122.         clear();
  123.  
  124.         copyChar(right.accID_, accID_);
  125.         copyData(right.moves_, moves_, right.countofmoves_);
  126.  
  127.         initialBalance_ = right.initialBalance_;
  128.         balance_ = right.balance_;
  129.         countofmoves_ = right.countofmoves_;
  130.  
  131.         return *this;
  132.     }
  133.  
  134.     ostream& operator<<(const char* accID)
  135.     {
  136.  
  137.     }
  138.  
  139.     friend ostringstream& operator<<(ostringstream& outStr, const account& elem)
  140.     {
  141.  
  142.     }
  143.  
  144.     void pushback(const char* credID, int amount, const char* sign)
  145.     {
  146.         pushBackHeapArray(moves_, countofmoves_, trans(credID, amount, sign));
  147.     }
  148.  
  149.     int Balance() const
  150.     {
  151.         return balance_;
  152.     }
  153.  
  154.     void clear()
  155.     {
  156.         delete[] accID_;
  157.         accID_ = nullptr;
  158.         delete[] moves_;
  159.         moves_ = nullptr;
  160.     }
  161. };
  162.  
  163. class CBank
  164. {
  165. public:
  166.  
  167.     // default constructor
  168.     CBank() : listofaccs_(nullptr), sizeoflist_(0) {}
  169.  
  170.     // copy constructor
  171.     CBank(const CBank& tocopy) : listofaccs_(nullptr), sizeoflist_(tocopy.sizeoflist_)
  172.     {
  173.         copyData(tocopy.listofaccs_, listofaccs_, tocopy.sizeoflist_);
  174.     }
  175.  
  176.     // destructor
  177.     ~CBank()
  178.     {
  179.         clear();
  180.     }
  181.  
  182.     // operator =
  183.     CBank& operator=(const CBank& right)
  184.     {
  185.         clear();
  186.         sizeoflist_ = right.sizeoflist_;
  187.         copyData(right.listofaccs_, listofaccs_, right.sizeoflist_);
  188.  
  189.         return *this;
  190.     }
  191.  
  192.     bool NewAccount(const char* accID, int initialBalance)
  193.     {
  194.         for (size_t i = 0; i < sizeoflist_; ++i)
  195.         {
  196.             if (equalString(listofaccs_[i].accID_, accID))
  197.             {
  198.                 return false;
  199.             }
  200.         }
  201.  
  202.         pushBackHeapArray(listofaccs_, sizeoflist_, account(accID, initialBalance));
  203.  
  204.         return true;
  205.     }
  206.  
  207.     bool Transaction(const char* debAccID, const char* credAccID, unsigned int amount, const char* signature)
  208.     {
  209.         int debindex = -1, credindex = -1;
  210.         for (size_t i = 0; i < sizeoflist_; ++i)
  211.         {
  212.             if (equalString(debAccID, listofaccs_[i].accID_))
  213.             {
  214.                 debindex = i;
  215.             }
  216.             if (equalString(credAccID, listofaccs_[i].accID_))
  217.             {
  218.                 credindex = i;
  219.             }
  220.         }
  221.  
  222.         if (credindex == debindex || debindex < 0 || credindex < 0)
  223.         {
  224.             return false;
  225.         }
  226.  
  227.         listofaccs_[debindex].pushback(credAccID, amount, signature);
  228.         listofaccs_[debindex].balance_ -= amount;
  229.         listofaccs_[credindex].pushback(debAccID, -static_cast <int>(amount), signature);
  230.         listofaccs_[credindex].balance_ += amount;
  231.         return true;
  232.     }
  233.  
  234.     bool TrimAccount(const char* accID)
  235.     {
  236.         int accindex = -1;
  237.         for (size_t i = 0; i < sizeoflist_; ++i)
  238.         {
  239.             if (equalString(accID, listofaccs_[i].accID_))
  240.             {
  241.                 accindex = i;
  242.             }
  243.         }
  244.         if (accindex < 0)
  245.         {
  246.             return false;
  247.         }
  248.         delete[] listofaccs_[accindex].moves_;
  249.         listofaccs_[accindex].moves_ = nullptr;
  250.         listofaccs_[accindex].initialBalance_ = listofaccs_[accindex].balance_;
  251.         return true;
  252.     }
  253.  
  254.     const account& Account(const char* accID)
  255.     {
  256.         int accindex = -1;
  257.         for (size_t i = 0; i < sizeoflist_; ++i)
  258.         {
  259.             if (equalString(accID, listofaccs_[i].accID_))
  260.             {
  261.                 accindex = i;
  262.             }
  263.         }
  264.         if (accindex < 0)
  265.         {
  266.             throw invalid_argument("Such id doesn't exist");
  267.         }
  268.         return listofaccs_[accindex];
  269.     }
  270.  
  271. private:
  272.  
  273.     account* listofaccs_;
  274.     size_t sizeoflist_;
  275.  
  276.     void clear()
  277.     {
  278.         delete[] listofaccs_;
  279.         listofaccs_ = nullptr;
  280.     }
  281. };
  282.  
  283. #ifndef __PROGTEST__
  284. int main(void)
  285. {
  286.     ostringstream os;
  287.     char accCpy[100], debCpy[100], credCpy[100], signCpy[100];
  288.     CBank x0;
  289.     assert(x0.NewAccount("123456", 1000));
  290.     assert(x0.NewAccount("987654", -500));
  291.     assert(x0.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
  292.     assert(x0.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
  293.     assert(x0.NewAccount("111111", 5000));
  294.     assert(x0.Transaction("111111", "987654", 290, "Okh6e+8rAiuT5="));
  295.     assert(x0.Account("123456").Balance() == -2190);
  296.     assert(x0.Account("987654").Balance() == 2980);
  297.     assert(x0.Account("111111").Balance() == 4710);
  298.     //os << x0.Account("111111");
  299.     /*os.str("");
  300.     os << x0.Account("123456");
  301.     assert(!strcmp(os.str().c_str(), "123456:\n   1000\n - 300, to: 987654, sign: XAbG5uKz6E=\n - 2890, to: 987654, sign: AbG5uKz6E=\n = -2190\n"));
  302.     os.str("");
  303.     os << x0.Account("987654");
  304.     assert(!strcmp(os.str().c_str(), "987654:\n   -500\n + 300, from: 123456, sign: XAbG5uKz6E=\n + 2890, from: 123456, sign: AbG5uKz6E=\n + 290, from: 111111, sign: Okh6e+8rAiuT5=\n = 2980\n"));
  305.     os.str("");
  306.     os << x0.Account("111111");
  307.     assert(!strcmp(os.str().c_str(), "111111:\n   5000\n - 290, to: 987654, sign: Okh6e+8rAiuT5=\n = 4710\n"));*/
  308.     assert(x0.TrimAccount("987654"));
  309.     /*assert(x0.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
  310.     os.str("");
  311.     os << x0.Account("987654");
  312.     assert(!strcmp(os.str().c_str(), "987654:\n   2980\n + 123, from: 111111, sign: asdf78wrnASDT3W\n = 3103\n"));
  313.  
  314.     CBank x2;
  315.     strncpy(accCpy, "123456", sizeof(accCpy));
  316.     assert(x2.NewAccount(accCpy, 1000));
  317.     strncpy(accCpy, "987654", sizeof(accCpy));
  318.     assert(x2.NewAccount(accCpy, -500));
  319.     strncpy(debCpy, "123456", sizeof(debCpy));
  320.     strncpy(credCpy, "987654", sizeof(credCpy));
  321.     strncpy(signCpy, "XAbG5uKz6E=", sizeof(signCpy));
  322.     assert(x2.Transaction(debCpy, credCpy, 300, signCpy));
  323.     strncpy(debCpy, "123456", sizeof(debCpy));
  324.     strncpy(credCpy, "987654", sizeof(credCpy));
  325.     strncpy(signCpy, "AbG5uKz6E=", sizeof(signCpy));
  326.     assert(x2.Transaction(debCpy, credCpy, 2890, signCpy));
  327.     strncpy(accCpy, "111111", sizeof(accCpy));
  328.     assert(x2.NewAccount(accCpy, 5000));
  329.     strncpy(debCpy, "111111", sizeof(debCpy));
  330.     strncpy(credCpy, "987654", sizeof(credCpy));
  331.     strncpy(signCpy, "Okh6e+8rAiuT5=", sizeof(signCpy));
  332.     assert(x2.Transaction(debCpy, credCpy, 2890, signCpy));
  333.     assert(x2.Account("123456").Balance() == -2190);
  334.     assert(x2.Account("987654").Balance() == 5580);
  335.     assert(x2.Account("111111").Balance() == 2110);
  336.     os.str("");
  337.     os << x2.Account("123456");
  338.     assert(!strcmp(os.str().c_str(), "123456:\n   1000\n - 300, to: 987654, sign: XAbG5uKz6E=\n - 2890, to: 987654, sign: AbG5uKz6E=\n = -2190\n"));
  339.     os.str("");
  340.     os << x2.Account("987654");
  341.     assert(!strcmp(os.str().c_str(), "987654:\n   -500\n + 300, from: 123456, sign: XAbG5uKz6E=\n + 2890, from: 123456, sign: AbG5uKz6E=\n + 2890, from: 111111, sign: Okh6e+8rAiuT5=\n = 5580\n"));
  342.     os.str("");
  343.     os << x2.Account("111111");
  344.     assert(!strcmp(os.str().c_str(), "111111:\n   5000\n - 2890, to: 987654, sign: Okh6e+8rAiuT5=\n = 2110\n"));
  345.     assert(x2.TrimAccount("987654"));
  346.     strncpy(debCpy, "111111", sizeof(debCpy));
  347.     strncpy(credCpy, "987654", sizeof(credCpy));
  348.     strncpy(signCpy, "asdf78wrnASDT3W", sizeof(signCpy));
  349.     assert(x2.Transaction(debCpy, credCpy, 123, signCpy));
  350.     os.str("");
  351.     os << x2.Account("987654");
  352.     assert(!strcmp(os.str().c_str(), "987654:\n   5580\n + 123, from: 111111, sign: asdf78wrnASDT3W\n = 5703\n"));
  353.  
  354.     CBank x4;
  355.     assert(x4.NewAccount("123456", 1000));
  356.     assert(x4.NewAccount("987654", -500));
  357.     assert(!x4.NewAccount("123456", 3000));
  358.     assert(!x4.Transaction("123456", "666", 100, "123nr6dfqkwbv5"));
  359.     assert(!x4.Transaction("666", "123456", 100, "34dGD74JsdfKGH"));
  360.     assert(!x4.Transaction("123456", "123456", 100, "Juaw7Jasdkjb5"));
  361.     try
  362.     {
  363.         x4.Account("666").Balance();
  364.         assert("Missing exception !!" == NULL);
  365.     }
  366.     catch (...)
  367.     {
  368.     }
  369.     try
  370.     {
  371.         os << x4.Account("666").Balance();
  372.         assert("Missing exception !!" == NULL);
  373.     }
  374.     catch (...)
  375.     {
  376.     }
  377.     assert(!x4.TrimAccount("666"));
  378.  
  379.     CBank x6;
  380.     assert(x6.NewAccount("123456", 1000));
  381.     assert(x6.NewAccount("987654", -500));
  382.     assert(x6.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
  383.     assert(x6.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
  384.     assert(x6.NewAccount("111111", 5000));
  385.     assert(x6.Transaction("111111", "987654", 2890, "Okh6e+8rAiuT5="));
  386.     CBank x7(x6);
  387.     assert(x6.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
  388.     assert(x7.Transaction("111111", "987654", 789, "SGDFTYE3sdfsd3W"));
  389.     assert(x6.NewAccount("99999999", 7000));
  390.     assert(x6.Transaction("111111", "99999999", 3789, "aher5asdVsAD"));
  391.     assert(x6.TrimAccount("111111"));
  392.     assert(x6.Transaction("123456", "111111", 221, "Q23wr234ER=="));
  393.     os.str("");
  394.     os << x6.Account("111111");
  395.     assert(!strcmp(os.str().c_str(), "111111:\n   -1802\n + 221, from: 123456, sign: Q23wr234ER==\n = -1581\n"));
  396.     os.str("");
  397.     os << x6.Account("99999999");
  398.     assert(!strcmp(os.str().c_str(), "99999999:\n   7000\n + 3789, from: 111111, sign: aher5asdVsAD\n = 10789\n"));
  399.     os.str("");
  400.     os << x6.Account("987654");
  401.     assert(!strcmp(os.str().c_str(), "987654:\n   -500\n + 300, from: 123456, sign: XAbG5uKz6E=\n + 2890, from: 123456, sign: AbG5uKz6E=\n + 2890, from: 111111, sign: Okh6e+8rAiuT5=\n + 123, from: 111111, sign: asdf78wrnASDT3W\n = 5703\n"));
  402.     os.str("");
  403.     os << x7.Account("111111");
  404.     assert(!strcmp(os.str().c_str(), "111111:\n   5000\n - 2890, to: 987654, sign: Okh6e+8rAiuT5=\n - 789, to: 987654, sign: SGDFTYE3sdfsd3W\n = 1321\n"));
  405.     try
  406.     {
  407.         os << x7.Account("99999999").Balance();
  408.         assert("Missing exception !!" == NULL);
  409.     }
  410.     catch (...)
  411.     {
  412.     }
  413.     os.str("");
  414.     os << x7.Account("987654");
  415.     assert(!strcmp(os.str().c_str(), "987654:\n   -500\n + 300, from: 123456, sign: XAbG5uKz6E=\n + 2890, from: 123456, sign: AbG5uKz6E=\n + 2890, from: 111111, sign: Okh6e+8rAiuT5=\n + 789, from: 111111, sign: SGDFTYE3sdfsd3W\n = 6369\n"));
  416.  
  417.     CBank x8;
  418.     CBank x9;
  419.     assert(x8.NewAccount("123456", 1000));
  420.     assert(x8.NewAccount("987654", -500));
  421.     assert(x8.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
  422.     assert(x8.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
  423.     assert(x8.NewAccount("111111", 5000));
  424.     assert(x8.Transaction("111111", "987654", 2890, "Okh6e+8rAiuT5="));
  425.     x9 = x8;
  426.     assert(x8.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
  427.     assert(x9.Transaction("111111", "987654", 789, "SGDFTYE3sdfsd3W"));
  428.     assert(x8.NewAccount("99999999", 7000));
  429.     assert(x8.Transaction("111111", "99999999", 3789, "aher5asdVsAD"));
  430.     assert(x8.TrimAccount("111111"));
  431.     os.str("");
  432.     os << x8.Account("111111");
  433.     assert(!strcmp(os.str().c_str(), "111111:\n   -1802\n = -1802\n"));
  434.     os.str("");
  435.     os << x9.Account("111111");
  436.     assert(!strcmp(os.str().c_str(), "111111:\n   5000\n - 2890, to: 987654, sign: Okh6e+8rAiuT5=\n - 789, to: 987654, sign: SGDFTYE3sdfsd3W\n = 1321\n"));*/
  437.  
  438.     return 0;
  439. }
  440. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement