Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- #include <cassert>
- #include <cctype>
- #include <cmath>
- #include <iostream>
- #include <iomanip>
- #include <sstream>
- using namespace std;
- #endif /* __PROGTEST__ */
- bool equalString(const char* a, const char* b)
- {
- bool res = true;
- for (size_t i = 0; res && (a[i] != '\0' || b[i] != '\0'); ++i)
- {
- res = (a[i] == b[i]);
- }
- return res;
- }
- void copyChar(const char* source, char*& acceptor)
- {
- size_t lenSource = strlen(source);
- delete[] acceptor;
- acceptor = new char[lenSource + 1];
- for (size_t i = 0; i <= lenSource; ++i)
- {
- acceptor[i] = source[i];
- }
- }
- template <class T>
- void copyData(const T* source, T*& acceptor, size_t lenSource)
- {
- delete[] acceptor;
- acceptor = new T[lenSource];
- for (size_t i = 0; i < lenSource; ++i)
- {
- acceptor[i] = source[i];
- }
- }
- template <class T>
- void pushBackHeapArray(T*& heapArr, size_t& size, const T& newElem)
- {
- T* newHeapArr = new T[size + 1];
- for (size_t i = 0; i < size; ++i)
- {
- newHeapArr[i] = heapArr[i];
- }
- newHeapArr[size] = newElem;
- delete[] heapArr;
- heapArr = newHeapArr;
- ++size;
- }
- struct account
- {
- struct trans
- {
- char* credID_;
- int amount_;
- char* sign_;
- trans() : credID_(nullptr), amount_(0), sign_(nullptr) {}
- trans(const char* credID, int amount, const char* sign) : amount_(amount), credID_(nullptr), sign_(nullptr)
- {
- copyChar(credID, credID_);
- copyChar(sign, sign_);
- }
- ~trans()
- {
- clear();
- }
- trans& operator=(const trans& right)
- {
- clear();
- copyChar(right.credID_, credID_);
- copyChar(right.sign_, sign_);
- amount_ = right.amount_;
- return *this;
- }
- void clear()
- {
- delete[] credID_;
- credID_ = nullptr;
- delete[] sign_;
- sign_ = nullptr;
- }
- };
- trans* moves_;
- size_t countofmoves_;
- char* accID_;
- int initialBalance_;
- int balance_;
- account() : moves_(nullptr), countofmoves_(0), accID_(nullptr), initialBalance_(0), balance_(0) {}
- account(const char* accID, int initialBalance) : moves_(nullptr), countofmoves_(0), initialBalance_(initialBalance), balance_(initialBalance), accID_(nullptr)
- {
- copyChar(accID, accID_);
- }
- ~account()
- {
- clear();
- }
- account& operator=(const account& right)
- {
- clear();
- copyChar(right.accID_, accID_);
- copyData(right.moves_, moves_, right.countofmoves_);
- initialBalance_ = right.initialBalance_;
- balance_ = right.balance_;
- countofmoves_ = right.countofmoves_;
- return *this;
- }
- ostream& operator<<(const char* accID)
- {
- }
- friend ostringstream& operator<<(ostringstream& outStr, const account& elem)
- {
- }
- void pushback(const char* credID, int amount, const char* sign)
- {
- pushBackHeapArray(moves_, countofmoves_, trans(credID, amount, sign));
- }
- int Balance() const
- {
- return balance_;
- }
- void clear()
- {
- delete[] accID_;
- accID_ = nullptr;
- delete[] moves_;
- moves_ = nullptr;
- }
- };
- class CBank
- {
- public:
- // default constructor
- CBank() : listofaccs_(nullptr), sizeoflist_(0) {}
- // copy constructor
- CBank(const CBank& tocopy) : listofaccs_(nullptr), sizeoflist_(tocopy.sizeoflist_)
- {
- copyData(tocopy.listofaccs_, listofaccs_, tocopy.sizeoflist_);
- }
- // destructor
- ~CBank()
- {
- clear();
- }
- // operator =
- CBank& operator=(const CBank& right)
- {
- clear();
- sizeoflist_ = right.sizeoflist_;
- copyData(right.listofaccs_, listofaccs_, right.sizeoflist_);
- return *this;
- }
- bool NewAccount(const char* accID, int initialBalance)
- {
- for (size_t i = 0; i < sizeoflist_; ++i)
- {
- if (equalString(listofaccs_[i].accID_, accID))
- {
- return false;
- }
- }
- pushBackHeapArray(listofaccs_, sizeoflist_, account(accID, initialBalance));
- return true;
- }
- bool Transaction(const char* debAccID, const char* credAccID, unsigned int amount, const char* signature)
- {
- int debindex = -1, credindex = -1;
- for (size_t i = 0; i < sizeoflist_; ++i)
- {
- if (equalString(debAccID, listofaccs_[i].accID_))
- {
- debindex = i;
- }
- if (equalString(credAccID, listofaccs_[i].accID_))
- {
- credindex = i;
- }
- }
- if (credindex == debindex || debindex < 0 || credindex < 0)
- {
- return false;
- }
- listofaccs_[debindex].pushback(credAccID, amount, signature);
- listofaccs_[debindex].balance_ -= amount;
- listofaccs_[credindex].pushback(debAccID, -static_cast <int>(amount), signature);
- listofaccs_[credindex].balance_ += amount;
- return true;
- }
- bool TrimAccount(const char* accID)
- {
- int accindex = -1;
- for (size_t i = 0; i < sizeoflist_; ++i)
- {
- if (equalString(accID, listofaccs_[i].accID_))
- {
- accindex = i;
- }
- }
- if (accindex < 0)
- {
- return false;
- }
- delete[] listofaccs_[accindex].moves_;
- listofaccs_[accindex].moves_ = nullptr;
- listofaccs_[accindex].initialBalance_ = listofaccs_[accindex].balance_;
- return true;
- }
- const account& Account(const char* accID)
- {
- int accindex = -1;
- for (size_t i = 0; i < sizeoflist_; ++i)
- {
- if (equalString(accID, listofaccs_[i].accID_))
- {
- accindex = i;
- }
- }
- if (accindex < 0)
- {
- throw invalid_argument("Such id doesn't exist");
- }
- return listofaccs_[accindex];
- }
- private:
- account* listofaccs_;
- size_t sizeoflist_;
- void clear()
- {
- delete[] listofaccs_;
- listofaccs_ = nullptr;
- }
- };
- #ifndef __PROGTEST__
- int main(void)
- {
- ostringstream os;
- char accCpy[100], debCpy[100], credCpy[100], signCpy[100];
- CBank x0;
- assert(x0.NewAccount("123456", 1000));
- assert(x0.NewAccount("987654", -500));
- assert(x0.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
- assert(x0.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
- assert(x0.NewAccount("111111", 5000));
- assert(x0.Transaction("111111", "987654", 290, "Okh6e+8rAiuT5="));
- assert(x0.Account("123456").Balance() == -2190);
- assert(x0.Account("987654").Balance() == 2980);
- assert(x0.Account("111111").Balance() == 4710);
- //os << x0.Account("111111");
- /*os.str("");
- os << x0.Account("123456");
- assert(!strcmp(os.str().c_str(), "123456:\n 1000\n - 300, to: 987654, sign: XAbG5uKz6E=\n - 2890, to: 987654, sign: AbG5uKz6E=\n = -2190\n"));
- os.str("");
- os << x0.Account("987654");
- 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"));
- os.str("");
- os << x0.Account("111111");
- assert(!strcmp(os.str().c_str(), "111111:\n 5000\n - 290, to: 987654, sign: Okh6e+8rAiuT5=\n = 4710\n"));*/
- assert(x0.TrimAccount("987654"));
- /*assert(x0.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
- os.str("");
- os << x0.Account("987654");
- assert(!strcmp(os.str().c_str(), "987654:\n 2980\n + 123, from: 111111, sign: asdf78wrnASDT3W\n = 3103\n"));
- CBank x2;
- strncpy(accCpy, "123456", sizeof(accCpy));
- assert(x2.NewAccount(accCpy, 1000));
- strncpy(accCpy, "987654", sizeof(accCpy));
- assert(x2.NewAccount(accCpy, -500));
- strncpy(debCpy, "123456", sizeof(debCpy));
- strncpy(credCpy, "987654", sizeof(credCpy));
- strncpy(signCpy, "XAbG5uKz6E=", sizeof(signCpy));
- assert(x2.Transaction(debCpy, credCpy, 300, signCpy));
- strncpy(debCpy, "123456", sizeof(debCpy));
- strncpy(credCpy, "987654", sizeof(credCpy));
- strncpy(signCpy, "AbG5uKz6E=", sizeof(signCpy));
- assert(x2.Transaction(debCpy, credCpy, 2890, signCpy));
- strncpy(accCpy, "111111", sizeof(accCpy));
- assert(x2.NewAccount(accCpy, 5000));
- strncpy(debCpy, "111111", sizeof(debCpy));
- strncpy(credCpy, "987654", sizeof(credCpy));
- strncpy(signCpy, "Okh6e+8rAiuT5=", sizeof(signCpy));
- assert(x2.Transaction(debCpy, credCpy, 2890, signCpy));
- assert(x2.Account("123456").Balance() == -2190);
- assert(x2.Account("987654").Balance() == 5580);
- assert(x2.Account("111111").Balance() == 2110);
- os.str("");
- os << x2.Account("123456");
- assert(!strcmp(os.str().c_str(), "123456:\n 1000\n - 300, to: 987654, sign: XAbG5uKz6E=\n - 2890, to: 987654, sign: AbG5uKz6E=\n = -2190\n"));
- os.str("");
- os << x2.Account("987654");
- 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"));
- os.str("");
- os << x2.Account("111111");
- assert(!strcmp(os.str().c_str(), "111111:\n 5000\n - 2890, to: 987654, sign: Okh6e+8rAiuT5=\n = 2110\n"));
- assert(x2.TrimAccount("987654"));
- strncpy(debCpy, "111111", sizeof(debCpy));
- strncpy(credCpy, "987654", sizeof(credCpy));
- strncpy(signCpy, "asdf78wrnASDT3W", sizeof(signCpy));
- assert(x2.Transaction(debCpy, credCpy, 123, signCpy));
- os.str("");
- os << x2.Account("987654");
- assert(!strcmp(os.str().c_str(), "987654:\n 5580\n + 123, from: 111111, sign: asdf78wrnASDT3W\n = 5703\n"));
- CBank x4;
- assert(x4.NewAccount("123456", 1000));
- assert(x4.NewAccount("987654", -500));
- assert(!x4.NewAccount("123456", 3000));
- assert(!x4.Transaction("123456", "666", 100, "123nr6dfqkwbv5"));
- assert(!x4.Transaction("666", "123456", 100, "34dGD74JsdfKGH"));
- assert(!x4.Transaction("123456", "123456", 100, "Juaw7Jasdkjb5"));
- try
- {
- x4.Account("666").Balance();
- assert("Missing exception !!" == NULL);
- }
- catch (...)
- {
- }
- try
- {
- os << x4.Account("666").Balance();
- assert("Missing exception !!" == NULL);
- }
- catch (...)
- {
- }
- assert(!x4.TrimAccount("666"));
- CBank x6;
- assert(x6.NewAccount("123456", 1000));
- assert(x6.NewAccount("987654", -500));
- assert(x6.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
- assert(x6.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
- assert(x6.NewAccount("111111", 5000));
- assert(x6.Transaction("111111", "987654", 2890, "Okh6e+8rAiuT5="));
- CBank x7(x6);
- assert(x6.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
- assert(x7.Transaction("111111", "987654", 789, "SGDFTYE3sdfsd3W"));
- assert(x6.NewAccount("99999999", 7000));
- assert(x6.Transaction("111111", "99999999", 3789, "aher5asdVsAD"));
- assert(x6.TrimAccount("111111"));
- assert(x6.Transaction("123456", "111111", 221, "Q23wr234ER=="));
- os.str("");
- os << x6.Account("111111");
- assert(!strcmp(os.str().c_str(), "111111:\n -1802\n + 221, from: 123456, sign: Q23wr234ER==\n = -1581\n"));
- os.str("");
- os << x6.Account("99999999");
- assert(!strcmp(os.str().c_str(), "99999999:\n 7000\n + 3789, from: 111111, sign: aher5asdVsAD\n = 10789\n"));
- os.str("");
- os << x6.Account("987654");
- 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"));
- os.str("");
- os << x7.Account("111111");
- 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"));
- try
- {
- os << x7.Account("99999999").Balance();
- assert("Missing exception !!" == NULL);
- }
- catch (...)
- {
- }
- os.str("");
- os << x7.Account("987654");
- 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"));
- CBank x8;
- CBank x9;
- assert(x8.NewAccount("123456", 1000));
- assert(x8.NewAccount("987654", -500));
- assert(x8.Transaction("123456", "987654", 300, "XAbG5uKz6E="));
- assert(x8.Transaction("123456", "987654", 2890, "AbG5uKz6E="));
- assert(x8.NewAccount("111111", 5000));
- assert(x8.Transaction("111111", "987654", 2890, "Okh6e+8rAiuT5="));
- x9 = x8;
- assert(x8.Transaction("111111", "987654", 123, "asdf78wrnASDT3W"));
- assert(x9.Transaction("111111", "987654", 789, "SGDFTYE3sdfsd3W"));
- assert(x8.NewAccount("99999999", 7000));
- assert(x8.Transaction("111111", "99999999", 3789, "aher5asdVsAD"));
- assert(x8.TrimAccount("111111"));
- os.str("");
- os << x8.Account("111111");
- assert(!strcmp(os.str().c_str(), "111111:\n -1802\n = -1802\n"));
- os.str("");
- os << x9.Account("111111");
- 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"));*/
- return 0;
- }
- #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement