Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. #include<vector>
  4. #include<sys/time.h>
  5. #include <chrono>
  6. #include <algorithm>
  7. #include <math.h>
  8. //#include "date.h"
  9.  
  10. using namespace std;
  11.  
  12. // TODO zmienilem na long longi poniewaz double sa dobre do 15 znakow i kazdy ma ll xd
  13. using B = unsigned long long;
  14.  
  15. class Operation {
  16.     B money;
  17.     tm date;
  18.     chrono::milliseconds time; // moze mienic nazwe na milisecond
  19.     //long int time;
  20.     //char* format = (char*)malloc(10);
  21. public:
  22.     Operation(B cash) : money(cash) {
  23.         chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
  24.         chrono::system_clock::duration tp = now.time_since_epoch();
  25.  
  26.         time = chrono::duration_cast<chrono::milliseconds>(tp);
  27.  
  28.         time_t tt = chrono::system_clock::to_time_t(now);
  29.         date = *localtime(&tt);
  30.         /*gettimeofday(&date, NULL);
  31.         time = date.tv_sec * 1000 + date.tv_usec / 1000;
  32.         struct tm *ptm = localtime(&date.tv_sec);
  33.         strftime(format, sizeof(format), "%Y-%m", ptm);
  34.         strftime(format + 7, sizeof(format), "-%d", ptm);*/
  35.     }
  36.     ~Operation() {
  37.         //TODO cos program mi krzyczy
  38.         //delete format;
  39.     }
  40.  
  41.     string getDate() const {
  42.         return to_string(date.tm_year + 1900) + "-" + to_string(date.tm_mon + 1) + "-" + to_string(date.tm_mday);
  43.     }
  44.     /*
  45.     void printData() {
  46.         for (int i = 0; i < 10; i++)
  47.             cout << format[i];
  48.         cout << endl;
  49.     }*/
  50.  
  51.     string getStringOfUnits() const {
  52.         return to_string(money/100000000) + "," + to_string(money%100000000);
  53.     }
  54.  
  55.     B getUnits() const {
  56.         //TODO teraz trzeba to troche zmienic bo dla nas 1 B = 10^8 B xdd
  57.         return money;
  58.     }
  59.  
  60.     bool operator==(const Operation &other) {
  61.         return time == other.time;
  62.     }
  63.  
  64.     bool operator<(const Operation &other) {
  65.         return time < other.time;
  66.     }
  67.  
  68.     bool operator>(const Operation &other) {
  69.         return time > other.time;
  70.     }
  71.  
  72.     bool operator!=(const Operation &other) {
  73.         return time != other.time;
  74.     }
  75.  
  76.     bool operator>=(const Operation &other) {
  77.         return time >= other.time;
  78.     }
  79.  
  80.     bool operator<=(const Operation &other) {
  81.         return time <= other.time;
  82.     }
  83.  
  84.     friend ostream& operator<<(ostream&, const Operation&);
  85.    
  86. };
  87.  
  88. ostream& operator<<(ostream &os, const Operation & op) {
  89.     return os << "Wallet balance is " << op.getStringOfUnits() <<  " b after opereation made at day " << op.getDate() <<endl;
  90.     //TODO
  91. }
  92.  
  93.  
  94. // template<class T>
  95. class Wallet {
  96. private:
  97. public:
  98.     vector<Operation> history;
  99.     mutable B productedMoney = 0;
  100.     const B maxProductedMoney = 2100000000000000;
  101.  
  102.     int findFloatingPoint (string s) {
  103.         bool afterPoint = false;
  104.         int result = s.size(); // wartość zwracana, gdy nie ma przecinka.
  105.  
  106.         for (size_t i = 0; i < s.size(); i++) {
  107.             if (s[i] != '0' && s[i] != '1' && s[i] != '.' )
  108.                 throw "string does not represent a binary number";
  109.  
  110.             if (s[i] == '.') {
  111.  
  112.                 if (afterPoint || i == 0 || i == s.size() - 1)
  113.                     throw "string does not represent a binary number";
  114.  
  115.                 afterPoint = true;
  116.                 result = i;
  117.             }
  118.         }
  119.  
  120.         return result;
  121.  
  122.     }
  123.  
  124.     double stringToBinary (string s) {
  125.         double result = 0;
  126.         double multiplier = 1;
  127.         size_t floatingPoint = findFloatingPoint(s);
  128.  
  129.         for (size_t i = floatingPoint - 1; ; i--) {
  130.             if (s[i] == '1')
  131.                 result += multiplier;
  132.  
  133.             multiplier *= 2;
  134.            
  135.             if (i == 0)
  136.                 break;
  137.         }
  138.  
  139.         multiplier = 0.5;
  140.  
  141.         for (size_t i = floatingPoint + 1; i < s.size(); i++) {
  142.             if (s[i] == '1')
  143.                 result += multiplier;
  144.  
  145.             multiplier /= 2;
  146.         }
  147.  
  148.         return result;
  149.     }
  150.  
  151.     Wallet (string s, int x);
  152.  
  153.     Wallet(B n = 0) {
  154.         //TODO teraz trzeba dodawać wszedzie razy 100 000 000
  155.         //TODO ERROR JEZELI PRZEKRACZA MAKSYMALNA LICZBE
  156.         n *= 100000000;
  157.         n = tryCreateB(n);
  158.         Operation op = Operation(n);
  159.         history.push_back(op);
  160.         productedMoney += n;
  161.     }
  162.  
  163.     Wallet (string s) {
  164.         B n = (B) (stod(s) * 100000000);
  165.         n = tryCreateB(n);
  166.         Operation op = Operation(n);
  167.         history.push_back(op);
  168.         productedMoney += n;
  169.     }
  170.    
  171.     static Wallet fromBinary(string s);
  172.  
  173.     Wallet(Wallet &&w2) {
  174.         history = move(w2.history);
  175.         Operation op = Operation(history.back().getUnits());
  176.         history.push_back(op);
  177.     }
  178.  
  179.     Wallet(Wallet &w2) = delete;
  180.  
  181.     Wallet(Wallet &&w1, Wallet &&w2) {
  182.         B units1 = w1.history.back().getUnits();
  183.         B units2 = w2.history.back().getUnits();
  184.         size_t sizeW1 = w1.history.size();
  185.         size_t sizeW2 = w2.history.size();
  186.  
  187.         history = move(w1.history);
  188.         for (size_t i = 0; i < sizeW2; i++) {
  189.             history.push_back(move(w2.history[i]));
  190.         }
  191.  
  192.         //TODO sprawdzic?
  193.         // inplace_merge(history.begin(), history.begin() + sizeW1, history.end());
  194.         Operation op = Operation(units1 + units2);
  195.  
  196.         history.push_back(op);
  197.     }
  198.  
  199.     Wallet &operator=(Wallet &&w) {
  200.         if (this == &w)
  201.             return *this;
  202.         history = move(w.history);
  203.         Operation op = Operation(history.back().getUnits());
  204.         history.push_back(op);
  205.         return *this;
  206.     }
  207.  
  208.     Wallet &operator=(Wallet &w) = delete;
  209.  
  210.     Wallet &operator*=(int n) {
  211.         //TODO wyjatek i co z typem
  212.         B moneyInWallet = history.back().getUnits();
  213.         B createdMoney = tryCreateB(moneyInWallet, n - 1);
  214.  
  215.         Operation op = Operation(moneyInWallet  + createdMoney);
  216.         productedMoney += createdMoney;
  217.         history.push_back(op);
  218.         return *this;
  219.     }
  220.  
  221.     B tryCreateB(B create, int multiply = 1) {
  222.         if (multiply == 0)
  223.             return 0;
  224.         if (multiply < 0 || log10(multiply) + log10(create) >= log10(maxProductedMoney) ||
  225.             ((B) multiply) * create > maxProductedMoney - productedMoney) {
  226.             throw "B limit exceeded.";
  227.             return 0;
  228.         } else {
  229.             return multiply * create;
  230.         }
  231.     }
  232.  
  233.     // Wallet operator*(int n) {
  234.     //  Wallet result = *this;
  235.     //  result *= n;
  236.     //  return result;
  237.     // }
  238.  
  239. };
  240.  
  241. Wallet::Wallet(string s, int x) {
  242.     B n = (B) (stringToBinary(s) * 100000000);
  243.     n = tryCreateB(n);
  244.     Operation op = Operation(n);
  245.     history.push_back(op);
  246.     productedMoney += n;
  247. }
  248.  
  249. Wallet Wallet::fromBinary(string s) {
  250.     return Wallet(s, 0);
  251. }
  252.  
  253.  
  254. int main() {
  255.     Operation op = Operation(2019222222837261);
  256.     // cout << op.money << endl;
  257.     // op.printData();
  258.     cout << op;
  259.     Wallet w = Wallet("563.27");
  260.     cout << w.history[0].getUnits() << endl;
  261.  
  262.     Wallet v = Wallet::fromBinary("100.0100");
  263.     cout << v.history[0].getUnits() << endl;   
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement