Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstring>
- #include<fstream>
- #include<ctime>
- using namespace std;
- long long getTimeInSeconds()
- {
- time_t now = time(0);
- return now;
- }
- double getInFMICounts(double fiatMoney)
- {
- return fiatMoney/375;
- }
- struct Transaction {
- long long time;
- unsigned senderId;
- unsigned receiverId;
- double fmiCoins;
- public:
- Transaction()
- {
- this->time = 0;
- this->senderId = 0;
- this->receiverId = 0;
- this->fmiCoins = 0;
- }
- Transaction(unsigned _senderId,unsigned _receiverId,double _fmiCoins)
- {
- this->time = getTimeInSeconds();
- this->senderId = _senderId;
- this->receiverId = _receiverId;
- this->fmiCoins = _fmiCoins;
- }
- void print()const
- {
- cout<<"Transaction:"<<time<<" "<<senderId<<" "<<receiverId<<" "<<fmiCoins<<endl;
- }
- };
- struct Wallet {
- private:
- char owner[256];
- unsigned id;
- double fiatMoney;
- Transaction t;
- public:
- Wallet()
- {
- strcpy(this->owner," ");
- this->id=0;
- this->fiatMoney=0;
- }
- Wallet(const char* _owner ,double _fiatMoney,unsigned int _id)
- {
- strcpy(this->owner,_owner);
- this->id=_id;
- this->fiatMoney=_fiatMoney;
- Transaction t(id,4294967295 ,getInFMICounts(fiatMoney));
- fstream file;
- file.open("transactions.dat",ios::binary | ios::out | ios::app);
- file.seekp(0,ios::beg);
- file.write((const char*)(&t),sizeof(Transaction));
- file.close();
- }
- void print()const
- {
- cout<<this->owner<<" "<<this->id<<" "<<this->fiatMoney<<endl;
- }
- const char* getOwner()const
- {
- return owner;
- }
- unsigned int getId()const
- {
- return id;
- }
- double getFiatMoney()const
- {
- return fiatMoney;
- }
- void setOwner(const char* newOwner)
- {
- strcpy(owner,newOwner);
- }
- void setId(unsigned int newId)
- {
- id=newId;
- }
- void setFiatMoney(double newFiatMoney)
- {
- fiatMoney=newFiatMoney;
- }
- };
- int lastId()
- {
- fstream file;
- file.open("wallet.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- int countOfWalletsInFile = fileSize / sizeof(Wallet);
- file.close();
- if(countOfWalletsInFile<4294967296)
- {
- return countOfWalletsInFile;
- }
- }
- /*bool SearchingForWallet(unsigned int id)
- {
- fstream file;
- file.open("wallet.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfWalletsInFile = fileSize / sizeof(Wallet);
- file.close();
- file.open("wallet.dat", ios::binary | ios::in);
- Wallet wallFromFile;
- Wallet* wallets = new Wallet[countOfWalletsInFile];
- int wallCount = 0;
- while (file.read((char*)&wallFromFile, sizeof(Wallet)))
- {
- wallets[wallCount] = wallFromFile;
- wallCount++;
- }
- file.close();
- for (int i = 0; i < countOfWalletsInFile; i++)
- {
- if(wallets[i].getId()==id)
- {
- return true;
- break;
- }
- else
- {
- return false;
- }
- }
- delete[] wallets;
- }*/
- void walletInfo(unsigned walletID)
- {
- fstream file;
- file.open("wallet.dat", ios::binary | ios::in);
- if (!file)
- {
- cout << "Error\n";
- }
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfWalletsInFile = fileSize / sizeof(Wallet);
- file.close();
- file.open("wallet.dat", ios::binary | ios::in);
- Wallet wallFromFile;
- Wallet* wallets = new Wallet[countOfWalletsInFile];
- int wallCount = 0;
- while (file.read((char*)&wallFromFile, sizeof(Wallet)))
- {
- wallets[wallCount] = wallFromFile;
- wallCount++;
- }
- file.close();
- for (int i = 0; i < countOfWalletsInFile; i++)
- {
- if(wallets[i].getId()==walletID)
- {
- wallets[i].print();
- }
- }
- delete[] wallets;
- }
- class Wallets
- {
- private:
- Wallet* wallets;
- size_t size;
- size_t capacity;
- void copy(const Wallets& other)
- {
- this->capacity = other.capacity;
- this->size = other.size;
- this->wallets = new Wallet[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- this->wallets[i] = other.wallets[i];
- }
- }
- void erase()
- {
- delete [] this->wallets;
- }
- void resize()
- {
- this->capacity *= 2;
- Wallet* newWallet = new Wallet[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- newWallet[i] = wallets[i];
- }
- this->erase();
- wallets = newWallet;
- }
- public:
- Wallets()
- {
- this->capacity = 8;
- this->size = 0;
- this->wallets = new Wallet[this->capacity];
- }
- Wallets(const Wallets& other)
- {
- this->copy(other);
- }
- Wallets& operator=(const Wallets& other)
- {
- if(this != &other)
- {
- this->erase();
- this->copy(other);
- }
- return *this;
- }
- ~Wallets()
- {
- this->erase();
- }
- void addWallet(const Wallet& wallet)
- {
- if(this->size >= this->capacity)
- {
- this->resize();
- }
- this->wallets[this->size++] = wallet;
- }
- void showWallets() const
- {
- for(size_t i = 0; i < this->size; i++)
- {
- this->wallets[i].print();
- }
- }
- void setFiatMoneyinWall(unsigned int id,double money)
- {
- for(int i = 0;i<size;i++)
- {
- if(wallets[i].getId()==id)
- {
- wallets[i].setFiatMoney(money);
- }
- }
- }
- void sortWaallets()
- {
- for(size_t i = 0; i < this->size - 1; i++)
- {
- for(size_t j = 0; j < this->size - 1; j++)
- {
- if(this->wallets[j].getFiatMoney() < this->wallets[j + 1].getFiatMoney())
- {
- swap(this->wallets[j], this->wallets[j+1]);
- }
- }
- }
- }
- void show10()const
- {
- for(size_t i = 0; i < 10; i++)
- {
- this->wallets[i].print();
- }
- }
- /*void WalletInfo(unsigned ID)
- {
- for(size_t i =0;i<this->size;i++)
- {
- if(ID==wallets[i].fiatMoney)
- {
- this->wallets[i].print();
- }
- }
- }*/
- };
- void RichestWallets()//===
- {
- fstream file;
- file.open("wallet.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfWalletsInFile = fileSize / sizeof(Wallet);
- file.close();
- file.open("wallet.dat", ios::binary | ios::in);
- Wallet wallFromFile;
- Wallet* wallets = new Wallet[countOfWalletsInFile];
- int wallCount = 0;
- while (file.read((char*)&wallFromFile, sizeof(Wallet)))
- {
- wallets[wallCount] = wallFromFile;
- wallCount++;
- }
- file.close();
- Wallets w;
- for(size_t i = 0 ; i < countOfWalletsInFile;i++)
- {
- w.addWallet(wallets[i]);
- }
- w.sortWaallets();
- if(countOfWalletsInFile>=10)
- {
- w.show10();
- }
- else
- {
- w.showWallets();
- }
- delete[] wallets;
- }
- bool enoughMoney(unsigned int id,double coins)
- {
- fstream file;
- file.open("wallet.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfWalletsInFile = fileSize / sizeof(Wallet);
- file.close();
- file.open("wallet.dat", ios::binary | ios::in);
- Wallet wallFromFile;
- Wallet* wallets = new Wallet[countOfWalletsInFile];
- int wallCount = 0;
- while (file.read((char*)&wallFromFile, sizeof(Wallet)))
- {
- wallets[wallCount] = wallFromFile;
- wallCount++;
- }
- file.close();
- for(size_t i = 0 ; i < countOfWalletsInFile;i++)
- {
- if(wallets[i].getId()==id)
- {
- if(wallets[i].getFiatMoney()>=coins*375)
- {
- return true;
- }
- else return false;
- }
- }
- delete[] wallets;
- }
- struct Order {
- enum Type { SELL, BUY } type;
- unsigned walletId;
- double fmiCoins;
- Type t;
- Order()
- {
- this->walletId=0;
- this->fmiCoins=0;
- }
- Order(Type _t,unsigned _walletId,double _fmiCoins)
- {
- this->t=_t;
- this->walletId=_walletId;
- this->fmiCoins=_fmiCoins;
- }
- unsigned int getWalletId()const
- {
- return walletId;
- }
- double getfmiCoins()
- {
- return fmiCoins;
- }
- Type getType()
- {
- return t;
- }
- void setWalletId(unsigned NEWwalletId)
- {
- this->walletId=NEWwalletId;
- }
- void setFMICoins(double NEWfmiCoins)
- {
- this->fmiCoins=NEWfmiCoins;
- }
- void print()const
- {
- if(t==Type(0)){
- cout<<"SELL Wallet ID:"<<walletId<<" Fmi coins:"<<fmiCoins<<endl;
- }
- else if(t==Type(1))
- {
- cout<<"BUY Wallet ID:"<<walletId<<" Fmi coins:"<<fmiCoins<<endl;
- }
- }
- };
- class Orders
- {
- private:
- Order* orders;
- size_t size;
- size_t capacity;
- void copy(const Orders& other)
- {
- this->capacity = other.capacity;
- this->size = other.size;
- this->orders = new Order[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- this->orders[i] = other.orders[i];
- }
- }
- void erase()
- {
- delete [] this->orders;
- }
- void resize()
- {
- this->capacity *= 2;
- Order* newOrder = new Order[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- newOrder[i] = orders[i];
- }
- this->erase();
- orders = newOrder;
- }
- public:
- Orders()
- {
- this->capacity = 8;
- this->size = 0;
- this->orders = new Order[this->capacity];
- }
- Orders(const Orders& other)
- {
- this->copy(other);
- }
- Orders& operator=(const Orders& other)
- {
- if(this != &other)
- {
- this->erase();
- this->copy(other);
- }
- return *this;
- }
- ~Orders()
- {
- this->erase();
- }
- void addOrder(const Order& order)
- {
- // unsigned int ID = order.getWalletId();
- //if(SearchingForWallet(ID))
- //{
- if(this->size >= this->capacity)
- {
- this->resize();
- }
- this->orders[this->size++] = order;
- //}
- }
- void showOrders() const
- {
- for(size_t i = 0; i < this->size; i++)
- {
- this->orders[i].print();
- }
- }
- /*Orders& orderTran(Order& selling,Order& buying)
- {
- double moneyS = selling.getfmiCoins();
- double moneyB = buying.getfmiCoins();
- if(moneyS < moneyB && moneyS != 0)
- {
- moneyB = moneyB - moneyS;
- moneyS = 0;
- }
- else if(moneyB==moneyS)
- {
- moneyS=0;
- moneyB=0;
- }
- else if(moneyB < moneyS && moneyB != 0)
- {
- moneyS=moneyS - moneyB;
- moneyB = 0;
- }
- selling.setFMICoins(moneyS);
- selling.setFMICoins(moneyB);
- }*/
- size_t getSize()const
- {
- return this->size;
- }
- double* getFmiCoins()
- {
- double* coins = new double[this->capacity];
- for(size_t i = 0 ; i < this->size;i++)
- {
- coins[i]=orders[i].getfmiCoins();
- }
- return coins;
- }
- unsigned getWalletID(size_t index)
- {
- return orders[index].getWalletId();
- }
- void setFMICOINS(double* arr)
- {
- for(int i = 0 ; i < size;i++)
- {
- orders[i].setFMICoins(arr[i]);
- }
- }
- void WriteInFile()//===
- {
- for(size_t i = 0 ; i < size;i++)
- {
- if(orders[i].getfmiCoins()!=0)
- {
- Order orderInFile;
- orderInFile = orders[i];
- fstream file;
- file.open("order.dat",ios::binary | ios::out | ios::app);
- file.seekp(0,ios::beg);
- file.write((const char*)(&orderInFile),sizeof(Order));
- file.close();
- }
- }
- }
- void OrdersInFile()
- {
- fstream file;
- file.open("order.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfOrdersInFile = fileSize / sizeof(Order);
- file.close();
- file.open("order.dat", ios::binary | ios::in);
- Order orderFromFile;
- Order* FileOrders = new Order[countOfOrdersInFile];
- int orderCount = 0;
- while (file.read((char*)&orderFromFile, sizeof(Order)))
- {
- FileOrders[orderCount] = orderFromFile;
- orderCount++;
- }
- file.close();
- for (int i = 0; i < countOfOrdersInFile; i++)
- {
- if(FileOrders[i].getType()==Order::Type(0))
- {
- if(FileOrders[i].getfmiCoins()!=0)
- {
- if(this->size >= this->capacity)
- {
- this->resize();
- }
- this->orders[this->size++] = FileOrders[i];
- }
- }
- }
- delete[] FileOrders;
- }
- void BuyInFile()
- {
- fstream file;
- file.open("order.dat", ios::binary | ios::in);
- file.seekp(0, ios::end);
- streampos fileSize = file.tellp();
- file.seekp(0, ios::beg);
- streampos countOfOrdersInFile = fileSize / sizeof(Order);
- file.close();
- file.open("order.dat", ios::binary | ios::in);
- Order orderFromFile;
- Order* FileOrders = new Order[countOfOrdersInFile];
- int orderCount = 0;
- while (file.read((char*)&orderFromFile, sizeof(Order)))
- {
- FileOrders[orderCount] = orderFromFile;
- orderCount++;
- }
- file.close();
- for (int i = 0; i < countOfOrdersInFile; i++)
- {
- if(FileOrders[i].getType()==Order::Type(1))
- {
- if(FileOrders[i].fmiCoins!=0)
- {
- if(this->size >= this->capacity)
- {
- this->resize();
- }
- this->orders[this->size++] = FileOrders[i];
- }
- }
- }
- delete[] FileOrders;
- }
- };
- class Transactions
- {
- private:
- Transaction* transactions;
- size_t size;
- size_t capacity;
- void copy(const Transactions& other)
- {
- this->capacity = other.capacity;
- this->size = other.size;
- this->transactions = new Transaction[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- this->transactions[i] = other.transactions[i];
- }
- }
- void erase()
- {
- delete [] this->transactions;
- }
- void resize()
- {
- this->capacity *= 2;
- Transaction* newTransaction = new Transaction[this->capacity];
- for(size_t i = 0; i < this->size; i++)
- {
- newTransaction[i] = transactions[i];
- }
- this->erase();
- transactions = newTransaction;
- }
- public:
- Transactions()
- {
- this->capacity = 8;
- this->size = 0;
- this->transactions = new Transaction[this->capacity];
- }
- Transactions(const Transactions& other)
- {
- this->copy(other);
- }
- Transactions& operator=(const Transactions& other)
- {
- if(this != &other)
- {
- this->erase();
- this->copy(other);
- }
- return *this;
- }
- ~Transactions()
- {
- this->erase();
- }
- void addTransaction(const Transaction& transaction)
- {
- if(this->size >= this->capacity)
- {
- this->resize();
- }
- this->transactions[this->size++] = transaction;
- }
- void showTransactions() const
- {
- for(size_t i = 0; i < this->size; i++)
- {
- this->transactions[i].print();
- }
- }
- };
- void TransactionOrders( Orders& selling, Orders& buying)
- {
- size_t size1= selling.getSize();
- size_t size2=buying.getSize();
- double *arr;
- double *arr2;
- Transactions ts;
- arr=selling.getFmiCoins();
- arr2=buying.getFmiCoins();
- for(size_t i = 0 ; i < size1;i++)
- {
- for(size_t j = 0 ; j <size2;j++)
- {
- if(arr[i]<arr2[j] && arr[i]!=0)
- {
- Transaction t(selling.getWalletID(i),buying.getWalletID(j),arr[i]);
- ts.addTransaction(t);
- arr2[j]=arr2[j]-arr[i];
- arr[i]=0;
- }
- else if(arr[i]==arr2[j])
- {
- Transaction t(selling.getWalletID(i),buying.getWalletID(j),arr[i]);
- ts.addTransaction(t);
- arr[i]=arr2[j]=0;
- }
- else if(arr[i]>arr2[j] && arr2[j]!=0)
- {
- Transaction t(selling.getWalletID(i),buying.getWalletID(j),arr2[j]);
- ts.addTransaction(t);
- arr[i]=arr[i]-arr2[j];
- arr2[j]=0;
- }
- }
- }
- selling.setFMICOINS(arr);
- buying.setFMICOINS(arr2);
- selling.WriteInFile();//==
- buying.WriteInFile();//==
- ts.showTransactions();
- fstream tfile;
- tfile.open("transactions.dat",ios::binary | ios::out | ios::app);
- tfile.seekp(0,ios::beg);
- tfile.write((const char*)(&ts),sizeof(ts));
- tfile.close();
- }
- int main()
- {
- char command[128];
- double money;
- char name[256];
- char s[4];
- unsigned walletId;
- unsigned senderId;
- unsigned receiverId;
- double fmiCoinsT;
- enum Type { SELL, BUY } type;
- double fmiCoins;
- int id=1+lastId();
- Wallets ws;
- Orders selling;
- Orders buying;
- selling.OrdersInFile();
- buying.BuyInFile();
- remove("order.dat");
- cin>>command;
- while(strcmp(command,"quit")!=0)
- {
- if(strcmp(command,"add-wallet")==0)
- {
- cin>>money>>name;
- Wallet w(name,money,id);
- fstream file;
- file.open("wallet.dat",ios::binary | ios::out | ios::app);
- if(!file)
- {
- cout<<"Error"<<endl;
- }
- file.seekp(0,ios::beg);
- file.write((const char*)(&w),sizeof(w));
- file.close();
- ws.addWallet(w);
- id++;
- }
- if(strcmp(command,"make-order")==0)
- {
- cin>>s>>walletId>>fmiCoins;
- if(strcmp(s,"SELL")==0)
- {
- //if(enoughMoney(walletId,fmiCoins))
- //{
- Order o(Order::Type(0),walletId,fmiCoins);
- selling.addOrder(o);
- //}
- }
- else if(strcmp(s,"BUY")==0)
- {
- Order o(Order::Type(1),walletId,fmiCoins);
- buying.addOrder(o);
- }
- else
- {
- cout<<"Invalid argument for Type"<<endl;
- }
- }
- if(strcmp(command,"transfer")==0)
- {
- cin>>senderId>>receiverId>>fmiCoinsT;
- Transaction t(senderId,receiverId,fmiCoinsT);
- }
- if(strcmp(command,"attract-investors")==0)
- {
- RichestWallets();
- }
- if(strcmp(command,"wallet-info")==0)
- {
- cin>>walletId;
- walletInfo(walletId);
- }
- cin>>command;
- }
- selling.showOrders();
- buying.showOrders();
- TransactionOrders(selling,buying);
- cout<<"after transactions:"<<endl;
- selling.showOrders();
- buying.showOrders();
- ws.showWallets();
- /* ws.showWallets();
- if(SearchingForWallet(17))
- {
- cout<<"Yes"<<endl;
- }
- else
- {
- cout<<"false"<<endl;
- }
- cout<<lastId();
- */
- /*fstream file;
- file.open("wallet.dat",ios::binary | ios::out);
- if(!file)
- {
- cout<<"Error"<<endl;
- }
- file.write((const char*)(&w),sizeof(w));
- file.close();
- fstream file2;
- file2.open("transactions.dat",ios::binary | ios::out);
- if(!file2)
- {
- cout<<"Error"<<endl;
- }
- file2.write((const char*)(&tr),sizeof(tr));
- file2.close();
- /* fstream file3;
- file3.open("orders.dat",ios::binary | ios::out);
- if(!file3)
- {
- cout<<"Error"<<endl;
- }
- file3.write((const char*)(&o),sizeof(o));
- file3.close();
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement