Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <time.h>
- using namespace std;
- //Structs
- struct Wallet
- {
- char owner[256];
- unsigned id;
- double fiatMoney;
- };
- struct Transaction
- {
- long long time;
- unsigned senderId;
- unsigned receiverId;
- double fmiCoins;
- };
- struct Order
- {
- enum Type { SELL, BUY } type;
- unsigned walletId;
- double fmiCoins;
- };
- //Enums
- enum OrderT {FIRST, LAST};
- //Globals
- Wallet *Wallets;
- int walletCount = 0;
- const int fiatRate = 375;
- //Funcs
- void loadWallets();
- void saveWallets();
- void addWallet(Wallet);
- void saveTransaction(Transaction);
- double getBalance(int);
- void saveOrder(Order);
- void orderSell(int, double);
- void orderBuy(int, double);
- int getOrderCount(int);
- long long getTransactionTime(int, OrderT);
- void showTopTen();
- int main()
- {
- char command[17];
- char oType[5];
- int walletId;
- loadWallets();
- while (true)
- {
- cin >> command;
- if (strcmp(command, "add-wallet") == 0)
- {
- Wallet tWallet;
- cin >> tWallet.fiatMoney >> tWallet.owner;
- addWallet(tWallet);
- Transaction initialTransaction = { time(0), 4294967295,walletCount - 1, tWallet.fiatMoney / (double)fiatRate };
- saveTransaction(initialTransaction);
- continue;
- }
- if (strcmp(command, "make-order") == 0)
- {
- Order tOrder;
- cin >> oType >> tOrder.fmiCoins >> tOrder.walletId;
- tOrder.type = strcmp(oType, "SELL") ? Order::BUY : Order::SELL;
- if (tOrder.type == Order::SELL) // SELL
- {
- if (tOrder.walletId > walletCount) continue;
- if (tOrder.fmiCoins > getBalance(tOrder.walletId)) continue;
- orderSell(tOrder.walletId, tOrder.fmiCoins);
- }
- if (tOrder.type == Order::BUY) // BUY
- {
- if (tOrder.walletId > walletCount) continue;
- if (tOrder.fmiCoins*fiatRate > Wallets[tOrder.walletId].fiatMoney) continue;
- orderBuy(tOrder.walletId, tOrder.fmiCoins);
- }
- continue;
- }
- if (strcmp(command, "wallet-info") == 0)
- {
- cin >> walletId;
- if (walletId > walletCount) continue;
- cout << "Name: " << Wallets[walletId].owner << endl
- << "Money: " << Wallets[walletId].fiatMoney << endl
- << "Coins: " << getBalance(walletId) << endl;
- continue;
- }
- if (strcmp(command, "attract-investors") == 0)
- {
- showTopTen();
- continue;
- }
- if (strcmp(command, "quit") == 0)
- {
- saveWallets();
- break;
- }
- }
- system("pause");
- return 0;
- }
- void loadWallets()
- {
- ifstream infile;
- infile.open("wallets.dat");
- Wallet tempWallet;
- while (infile >>
- tempWallet.owner >>
- tempWallet.id >>
- tempWallet.fiatMoney)
- addWallet(tempWallet);
- infile.close();
- cout << "Wallets loaded:" << walletCount << endl;
- }
- void saveWallets()
- {
- if (walletCount == 0) return;
- ofstream outfile;
- outfile.open("wallets.dat", ofstream::out | ofstream::trunc);
- for (int i = 0; i < walletCount; i++)
- {
- outfile <<
- Wallets[i].owner << " " <<
- Wallets[i].id << " " <<
- Wallets[i].fiatMoney << endl;
- }
- outfile.close();
- }
- void addWallet(Wallet wal)
- {
- wal.id = walletCount;
- Wallet *tempWallet = new Wallet[walletCount+1];
- for (int i = 0; i < walletCount; i++)
- *(tempWallet + i) = *(Wallets + i);
- *(tempWallet + walletCount) = wal;
- delete[] Wallets;
- Wallets = tempWallet;
- walletCount++;
- }
- void saveTransaction(Transaction tran)
- {
- ofstream outfile;
- outfile.open("transactions.dat", ofstream::out | ofstream::app);
- outfile <<
- tran.time << " " <<
- tran.senderId << " " <<
- tran.receiverId << " " <<
- tran.fmiCoins << endl;
- outfile.close();
- }
- double getBalance(int id)
- {
- double balance = 0;
- ifstream infile;
- infile.open("transactions.dat");
- Transaction tempTransaction;
- while (infile >>
- tempTransaction.time >>
- tempTransaction.senderId >>
- tempTransaction.receiverId >>
- tempTransaction.fmiCoins)
- {
- if (tempTransaction.senderId == id) balance -= tempTransaction.fmiCoins;
- if (tempTransaction.receiverId == id) balance += tempTransaction.fmiCoins;
- }
- infile.close();
- return balance;
- }
- void saveOrder(Order ord)
- {
- ofstream outfile;
- outfile.open("orders.dat", ofstream::out | ofstream::app);
- outfile <<
- ord.type << " " <<
- ord.walletId << " " <<
- ord.fmiCoins << endl;
- outfile.close();
- }
- void orderSell(int requestWal, double sum)
- {
- int eInt;
- ifstream infile;
- ofstream temp;
- infile.open("orders.dat");
- temp.open("temp.dat");
- Order tempOrder;
- while (infile >>
- eInt >>
- tempOrder.walletId >>
- tempOrder.fmiCoins)
- {
- if (eInt == Order::BUY && sum > 0)
- {
- if (sum >= tempOrder.fmiCoins)
- {
- sum -= tempOrder.fmiCoins;
- Transaction tTransaction = { time(0), requestWal, tempOrder.walletId, tempOrder.fmiCoins };
- saveTransaction(tTransaction);
- continue;
- }
- else
- {
- sum -= tempOrder.fmiCoins;
- Transaction tTransaction = { time(0), requestWal, tempOrder.walletId, sum };
- saveTransaction(tTransaction);
- temp << Order::BUY << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
- continue;
- }
- }
- temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
- }
- if (sum > 0)
- {
- temp << Order::SELL << " " << requestWal << " " << sum << endl;
- }
- infile.close();
- temp.close();
- remove("orders.dat");
- rename("temp.dat", "orders.dat");
- }
- void orderBuy(int requestWal, double sum)
- {
- int eInt;
- ifstream infile;
- ofstream temp;
- infile.open("orders.dat");
- temp.open("temp.dat");
- Order tempOrder;
- while (infile >>
- eInt >>
- tempOrder.walletId >>
- tempOrder.fmiCoins)
- {
- if (eInt == Order::SELL && sum > 0)
- {
- if (sum >= tempOrder.fmiCoins)
- {
- sum -= tempOrder.fmiCoins;
- Transaction tTransaction = { time(0), tempOrder.walletId, requestWal, tempOrder.fmiCoins };
- saveTransaction(tTransaction);
- Wallets[tempOrder.walletId].fiatMoney += (tempOrder.fmiCoins*fiatRate);
- Wallets[requestWal].fiatMoney -= (tempOrder.fmiCoins*fiatRate);
- continue;
- }
- else
- {
- Transaction tTransaction = { time(0), tempOrder.walletId, requestWal, sum };
- saveTransaction(tTransaction);
- Wallets[tempOrder.walletId].fiatMoney += (sum*fiatRate);
- Wallets[requestWal].fiatMoney -= (sum*fiatRate);
- temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins-sum << endl;
- sum -= tempOrder.fmiCoins;
- continue;
- }
- }
- temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
- if (sum > 0)
- {
- temp << Order::BUY << " " << requestWal << " " << sum << endl;
- }
- }
- infile.close();
- temp.close();
- remove("orders.dat");
- rename("temp.dat", "orders.dat");
- }
- int getOrderCount(int walletId)
- {
- int eInt, count = 0;
- ifstream infile;
- infile.open("orders.dat");
- Order tOrder;
- while (infile >>
- eInt >>
- tOrder.walletId >>
- tOrder.fmiCoins)
- if (tOrder.walletId == walletId) count++;
- infile.close();
- return count;
- }
- long long getTransactionTime(int walletId, OrderT type)
- {
- long long startTime = 0;
- if (type == OrderT::FIRST) startTime = LLONG_MAX;
- ifstream infile;
- infile.open("transactions.dat");
- Transaction tempTransaction;
- while (infile >>
- tempTransaction.time >>
- tempTransaction.senderId >>
- tempTransaction.receiverId >>
- tempTransaction.fmiCoins)
- {
- if (tempTransaction.senderId == walletId || tempTransaction.receiverId == walletId)
- {
- if (type == OrderT::FIRST)
- {
- if (tempTransaction.time < startTime) startTime = tempTransaction.time;
- }
- else
- {
- if (tempTransaction.time > startTime) startTime = tempTransaction.time;
- }
- }
- }
- infile.close();
- return startTime == LLONG_MAX ? 0 : startTime;
- }
- void showTopTen()
- {
- int listcount = walletCount < 10 ? walletCount : 10;
- int shown = 0, maxId = -1;
- double max = 0;
- while (true)
- {
- for (int i = 0; i < listcount; i++)
- {
- if (getBalance(i) >= max && maxId != i)
- {
- max = getBalance(i);
- maxId = i;
- }
- }
- shown++;
- cout << shown << ".\n"
- << "Name: " << Wallets[maxId].owner << endl
- << "Coins: " << getBalance(maxId) << endl
- << "Orders count: " << getOrderCount(maxId) << endl
- << "First transaction: " << getTransactionTime(maxId, OrderT::FIRST) << endl
- << "Last transaction: " << getTransactionTime(maxId, OrderT::LAST) << endl
- << endl;
- if (shown >= listcount) break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement