Advertisement
Guest User

Koko domashno

a guest
Mar 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. //Structs
  9. struct Wallet
  10. {
  11.     char owner[256];
  12.     unsigned id;
  13.     double fiatMoney;
  14. };
  15.  
  16. struct Transaction
  17. {
  18.     long long time;
  19.     unsigned senderId;
  20.     unsigned receiverId;
  21.     double fmiCoins;
  22. };
  23.  
  24. struct Order
  25. {
  26.     enum Type { SELL, BUY } type;
  27.     unsigned walletId;
  28.     double fmiCoins;
  29. };
  30.  
  31. //Enums
  32. enum OrderT {FIRST, LAST};
  33.  
  34. //Globals
  35. Wallet *Wallets;
  36. int walletCount = 0;
  37. const int fiatRate = 375;
  38.  
  39. //Funcs
  40. void loadWallets();
  41. void saveWallets();
  42. void addWallet(Wallet);
  43. void saveTransaction(Transaction);
  44. double getBalance(int);
  45. void saveOrder(Order);
  46. void orderSell(int, double);
  47. void orderBuy(int, double);
  48. int getOrderCount(int);
  49. long long getTransactionTime(int, OrderT);
  50. void showTopTen();
  51.  
  52. int main()
  53. {
  54.     char command[17];
  55.     char oType[5];
  56.     int walletId;
  57.     loadWallets();
  58.  
  59.  
  60.     while (true)
  61.     {
  62.         cin >> command;
  63.         if (strcmp(command, "add-wallet") == 0)
  64.         {
  65.             Wallet tWallet;
  66.             cin >> tWallet.fiatMoney >> tWallet.owner;
  67.             addWallet(tWallet);
  68.             Transaction initialTransaction = { time(0), 4294967295,walletCount - 1, tWallet.fiatMoney / (double)fiatRate };
  69.             saveTransaction(initialTransaction);
  70.             continue;
  71.         }
  72.         if (strcmp(command, "make-order") == 0)
  73.         {
  74.             Order tOrder;
  75.             cin >> oType >> tOrder.fmiCoins >> tOrder.walletId;
  76.             tOrder.type = strcmp(oType, "SELL") ? Order::BUY : Order::SELL;
  77.  
  78.             if (tOrder.type == Order::SELL) // SELL
  79.             {
  80.                 if (tOrder.walletId > walletCount) continue;
  81.                 if (tOrder.fmiCoins > getBalance(tOrder.walletId)) continue;
  82.                 orderSell(tOrder.walletId, tOrder.fmiCoins);
  83.             }
  84.  
  85.             if (tOrder.type == Order::BUY) // BUY
  86.             {
  87.                 if (tOrder.walletId > walletCount) continue;
  88.                 if (tOrder.fmiCoins*fiatRate > Wallets[tOrder.walletId].fiatMoney) continue;
  89.                 orderBuy(tOrder.walletId, tOrder.fmiCoins);
  90.             }
  91.  
  92.             continue;
  93.         }
  94.         if (strcmp(command, "wallet-info") == 0)
  95.         {
  96.             cin >> walletId;
  97.             if (walletId > walletCount) continue;
  98.             cout << "Name: " << Wallets[walletId].owner << endl
  99.                 << "Money: " << Wallets[walletId].fiatMoney << endl
  100.                 << "Coins: " << getBalance(walletId) << endl;
  101.             continue;
  102.         }
  103.         if (strcmp(command, "attract-investors") == 0)
  104.         {
  105.             showTopTen();
  106.             continue;
  107.         }
  108.         if (strcmp(command, "quit") == 0)
  109.         {
  110.             saveWallets();
  111.             break;
  112.         }
  113.     }
  114.  
  115.  
  116.     system("pause");
  117.     return 0;
  118. }
  119.  
  120. void loadWallets()
  121. {
  122.     ifstream infile;
  123.     infile.open("wallets.dat");
  124.  
  125.     Wallet tempWallet;
  126.  
  127.     while (infile >>
  128.         tempWallet.owner >>
  129.         tempWallet.id >>
  130.         tempWallet.fiatMoney)
  131.         addWallet(tempWallet);
  132.  
  133.     infile.close();
  134.     cout << "Wallets loaded:" << walletCount << endl;
  135. }
  136.  
  137. void saveWallets()
  138. {
  139.     if (walletCount == 0) return;
  140.     ofstream outfile;
  141.     outfile.open("wallets.dat", ofstream::out | ofstream::trunc);
  142.  
  143.     for (int i = 0; i < walletCount; i++)
  144.     {
  145.         outfile <<
  146.             Wallets[i].owner << " " <<
  147.             Wallets[i].id << " " <<
  148.             Wallets[i].fiatMoney << endl;
  149.     }
  150.  
  151.     outfile.close();
  152. }
  153.  
  154. void addWallet(Wallet wal)
  155. {
  156.     wal.id = walletCount;
  157.     Wallet *tempWallet = new Wallet[walletCount+1];
  158.     for (int i = 0; i < walletCount; i++)
  159.         *(tempWallet + i) = *(Wallets + i);
  160.  
  161.     *(tempWallet + walletCount) = wal;
  162.     delete[] Wallets;
  163.     Wallets = tempWallet;
  164.  
  165.     walletCount++;
  166. }
  167.  
  168. void saveTransaction(Transaction tran)
  169. {
  170.     ofstream outfile;
  171.     outfile.open("transactions.dat", ofstream::out | ofstream::app);
  172.     outfile <<
  173.         tran.time << " " <<
  174.         tran.senderId << " " <<
  175.         tran.receiverId << " " <<
  176.         tran.fmiCoins << endl;
  177.     outfile.close();
  178. }
  179.  
  180. double getBalance(int id)
  181. {
  182.     double balance = 0;
  183.     ifstream infile;
  184.     infile.open("transactions.dat");
  185.  
  186.     Transaction tempTransaction;
  187.  
  188.     while (infile >>
  189.         tempTransaction.time >>
  190.         tempTransaction.senderId >>
  191.         tempTransaction.receiverId >>
  192.         tempTransaction.fmiCoins)
  193.     {
  194.         if (tempTransaction.senderId == id) balance -= tempTransaction.fmiCoins;
  195.         if (tempTransaction.receiverId == id) balance += tempTransaction.fmiCoins;
  196.     }
  197.  
  198.     infile.close();
  199.     return balance;
  200. }
  201.  
  202. void saveOrder(Order ord)
  203. {
  204.     ofstream outfile;
  205.     outfile.open("orders.dat", ofstream::out | ofstream::app);
  206.     outfile <<
  207.         ord.type << " " <<
  208.         ord.walletId << " " <<
  209.         ord.fmiCoins << endl;
  210.     outfile.close();
  211. }
  212.  
  213. void orderSell(int requestWal, double sum)
  214. {
  215.     int eInt;
  216.     ifstream infile;
  217.     ofstream temp;
  218.  
  219.     infile.open("orders.dat");
  220.     temp.open("temp.dat");
  221.  
  222.     Order tempOrder;
  223.  
  224.     while (infile >>
  225.         eInt >>
  226.         tempOrder.walletId >>
  227.         tempOrder.fmiCoins)
  228.     {
  229.         if (eInt == Order::BUY && sum > 0)
  230.         {
  231.             if (sum >= tempOrder.fmiCoins)
  232.             {
  233.                 sum -= tempOrder.fmiCoins;
  234.                 Transaction tTransaction = { time(0), requestWal, tempOrder.walletId, tempOrder.fmiCoins };
  235.                 saveTransaction(tTransaction);
  236.                 continue;
  237.             }
  238.             else
  239.             {
  240.                 sum -= tempOrder.fmiCoins;
  241.                 Transaction tTransaction = { time(0), requestWal, tempOrder.walletId, sum };
  242.                 saveTransaction(tTransaction);
  243.                 temp << Order::BUY << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
  244.                 continue;
  245.             }
  246.         }
  247.         temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
  248.     }
  249.     if (sum > 0)
  250.     {
  251.         temp << Order::SELL << " " << requestWal << " " << sum << endl;
  252.     }
  253.     infile.close();
  254.     temp.close();
  255.     remove("orders.dat");
  256.     rename("temp.dat", "orders.dat");
  257. }
  258.  
  259. void orderBuy(int requestWal, double sum)
  260. {
  261.     int eInt;
  262.     ifstream infile;
  263.     ofstream temp;
  264.  
  265.     infile.open("orders.dat");
  266.     temp.open("temp.dat");
  267.  
  268.     Order tempOrder;
  269.  
  270.     while (infile >>
  271.         eInt >>
  272.         tempOrder.walletId >>
  273.         tempOrder.fmiCoins)
  274.     {
  275.         if (eInt == Order::SELL && sum > 0)
  276.         {
  277.             if (sum >= tempOrder.fmiCoins)
  278.             {
  279.                 sum -= tempOrder.fmiCoins;
  280.                 Transaction tTransaction = { time(0), tempOrder.walletId, requestWal, tempOrder.fmiCoins };
  281.                 saveTransaction(tTransaction);
  282.                 Wallets[tempOrder.walletId].fiatMoney += (tempOrder.fmiCoins*fiatRate);
  283.                 Wallets[requestWal].fiatMoney -= (tempOrder.fmiCoins*fiatRate);
  284.                 continue;
  285.             }
  286.             else
  287.             {
  288.                 Transaction tTransaction = { time(0), tempOrder.walletId, requestWal, sum };
  289.                 saveTransaction(tTransaction);
  290.                 Wallets[tempOrder.walletId].fiatMoney += (sum*fiatRate);
  291.                 Wallets[requestWal].fiatMoney -= (sum*fiatRate);
  292.                 temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins-sum << endl;
  293.                 sum -= tempOrder.fmiCoins;
  294.                 continue;
  295.             }
  296.         }
  297.         temp << eInt << " " << tempOrder.walletId << " " << tempOrder.fmiCoins << endl;
  298.  
  299.         if (sum > 0)
  300.         {
  301.             temp << Order::BUY << " " << requestWal << " " << sum << endl;
  302.         }
  303.  
  304.     }
  305.     infile.close();
  306.     temp.close();
  307.     remove("orders.dat");
  308.     rename("temp.dat", "orders.dat");
  309. }
  310.  
  311.  
  312. int getOrderCount(int walletId)
  313. {
  314.     int eInt, count = 0;
  315.     ifstream infile;
  316.     infile.open("orders.dat");
  317.     Order tOrder;
  318.  
  319.     while (infile >>
  320.         eInt >>
  321.         tOrder.walletId >>
  322.         tOrder.fmiCoins)
  323.         if (tOrder.walletId == walletId) count++;
  324.     infile.close();
  325.     return count;
  326. }
  327.  
  328. long long getTransactionTime(int walletId, OrderT type)
  329. {
  330.     long long startTime = 0;
  331.     if (type == OrderT::FIRST) startTime = LLONG_MAX;
  332.     ifstream infile;
  333.     infile.open("transactions.dat");
  334.  
  335.     Transaction tempTransaction;
  336.  
  337.     while (infile >>
  338.         tempTransaction.time >>
  339.         tempTransaction.senderId >>
  340.         tempTransaction.receiverId >>
  341.         tempTransaction.fmiCoins)
  342.     {
  343.         if (tempTransaction.senderId == walletId || tempTransaction.receiverId == walletId)
  344.         {
  345.             if (type == OrderT::FIRST)
  346.             {
  347.                 if (tempTransaction.time < startTime) startTime = tempTransaction.time;
  348.             }
  349.             else
  350.             {
  351.                 if (tempTransaction.time > startTime) startTime = tempTransaction.time;
  352.             }
  353.         }
  354.     }
  355.     infile.close();
  356.     return startTime == LLONG_MAX ? 0 : startTime;
  357. }
  358.  
  359. void showTopTen()
  360. {
  361.     int listcount = walletCount < 10 ? walletCount : 10;
  362.     int shown = 0, maxId = -1;
  363.     double max = 0;
  364.  
  365.     while (true)
  366.     {
  367.         for (int i = 0; i < listcount; i++)
  368.         {
  369.             if (getBalance(i) >= max && maxId != i)
  370.             {
  371.                 max = getBalance(i);
  372.                 maxId = i;
  373.             }
  374.         }
  375.         shown++;
  376.         cout << shown << ".\n"
  377.             << "Name: " << Wallets[maxId].owner << endl
  378.             << "Coins: " << getBalance(maxId) << endl
  379.             << "Orders count: " << getOrderCount(maxId) << endl
  380.             << "First transaction: " << getTransactionTime(maxId, OrderT::FIRST) << endl
  381.             << "Last transaction: " << getTransactionTime(maxId, OrderT::LAST) << endl
  382.             << endl;
  383.         if (shown >= listcount) break;
  384.     }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement