Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<ctime>
- #include<vector>
- #include<sys/time.h>
- #include <chrono>
- #include <algorithm>
- #include <math.h>
- //#include "date.h"
- using namespace std;
- // TODO zmienilem na long longi poniewaz double sa dobre do 15 znakow i kazdy ma ll xd
- using B = unsigned long long;
- class Operation {
- B money;
- tm date;
- chrono::milliseconds time; // moze mienic nazwe na milisecond
- //long int time;
- //char* format = (char*)malloc(10);
- public:
- Operation(B cash) : money(cash) {
- chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
- chrono::system_clock::duration tp = now.time_since_epoch();
- time = chrono::duration_cast<chrono::milliseconds>(tp);
- time_t tt = chrono::system_clock::to_time_t(now);
- date = *localtime(&tt);
- /*gettimeofday(&date, NULL);
- time = date.tv_sec * 1000 + date.tv_usec / 1000;
- struct tm *ptm = localtime(&date.tv_sec);
- strftime(format, sizeof(format), "%Y-%m", ptm);
- strftime(format + 7, sizeof(format), "-%d", ptm);*/
- }
- ~Operation() {
- //TODO cos program mi krzyczy
- //delete format;
- }
- string getDate() const {
- return to_string(date.tm_year + 1900) + "-" + to_string(date.tm_mon + 1) + "-" + to_string(date.tm_mday);
- }
- /*
- void printData() {
- for (int i = 0; i < 10; i++)
- cout << format[i];
- cout << endl;
- }*/
- string getStringOfUnits() const {
- return to_string(money/100000000) + "," + to_string(money%100000000);
- }
- B getUnits() const {
- //TODO teraz trzeba to troche zmienic bo dla nas 1 B = 10^8 B xdd
- return money;
- }
- bool operator==(const Operation &other) {
- return time == other.time;
- }
- bool operator<(const Operation &other) {
- return time < other.time;
- }
- bool operator>(const Operation &other) {
- return time > other.time;
- }
- bool operator!=(const Operation &other) {
- return time != other.time;
- }
- bool operator>=(const Operation &other) {
- return time >= other.time;
- }
- bool operator<=(const Operation &other) {
- return time <= other.time;
- }
- friend ostream& operator<<(ostream&, const Operation&);
- };
- ostream& operator<<(ostream &os, const Operation & op) {
- return os << "Wallet balance is " << op.getStringOfUnits() << " b after opereation made at day " << op.getDate() <<endl;
- //TODO
- }
- // template<class T>
- class Wallet {
- private:
- public:
- vector<Operation> history;
- mutable B productedMoney = 0;
- const B maxProductedMoney = 2100000000000000;
- int findFloatingPoint (string s) {
- bool afterPoint = false;
- int result = s.size(); // wartość zwracana, gdy nie ma przecinka.
- for (size_t i = 0; i < s.size(); i++) {
- if (s[i] != '0' && s[i] != '1' && s[i] != '.' )
- throw "string does not represent a binary number";
- if (s[i] == '.') {
- if (afterPoint || i == 0 || i == s.size() - 1)
- throw "string does not represent a binary number";
- afterPoint = true;
- result = i;
- }
- }
- return result;
- }
- double stringToBinary (string s) {
- double result = 0;
- double multiplier = 1;
- size_t floatingPoint = findFloatingPoint(s);
- for (size_t i = floatingPoint - 1; ; i--) {
- if (s[i] == '1')
- result += multiplier;
- multiplier *= 2;
- if (i == 0)
- break;
- }
- multiplier = 0.5;
- for (size_t i = floatingPoint + 1; i < s.size(); i++) {
- if (s[i] == '1')
- result += multiplier;
- multiplier /= 2;
- }
- return result;
- }
- Wallet (string s, int x);
- Wallet(B n = 0) {
- //TODO teraz trzeba dodawaÄ wszedzie razy 100 000 000
- //TODO ERROR JEZELI PRZEKRACZA MAKSYMALNA LICZBE
- n *= 100000000;
- n = tryCreateB(n);
- Operation op = Operation(n);
- history.push_back(op);
- productedMoney += n;
- }
- Wallet (string s) {
- B n = (B) (stod(s) * 100000000);
- n = tryCreateB(n);
- Operation op = Operation(n);
- history.push_back(op);
- productedMoney += n;
- }
- static Wallet fromBinary(string s);
- Wallet(Wallet &&w2) {
- history = move(w2.history);
- Operation op = Operation(history.back().getUnits());
- history.push_back(op);
- }
- Wallet(Wallet &w2) = delete;
- Wallet(Wallet &&w1, Wallet &&w2) {
- B units1 = w1.history.back().getUnits();
- B units2 = w2.history.back().getUnits();
- size_t sizeW1 = w1.history.size();
- size_t sizeW2 = w2.history.size();
- history = move(w1.history);
- for (size_t i = 0; i < sizeW2; i++) {
- history.push_back(move(w2.history[i]));
- }
- //TODO sprawdzic?
- // inplace_merge(history.begin(), history.begin() + sizeW1, history.end());
- Operation op = Operation(units1 + units2);
- history.push_back(op);
- }
- Wallet &operator=(Wallet &&w) {
- if (this == &w)
- return *this;
- history = move(w.history);
- Operation op = Operation(history.back().getUnits());
- history.push_back(op);
- return *this;
- }
- Wallet &operator=(Wallet &w) = delete;
- Wallet &operator*=(int n) {
- //TODO wyjatek i co z typem
- B moneyInWallet = history.back().getUnits();
- B createdMoney = tryCreateB(moneyInWallet, n - 1);
- Operation op = Operation(moneyInWallet + createdMoney);
- productedMoney += createdMoney;
- history.push_back(op);
- return *this;
- }
- B tryCreateB(B create, int multiply = 1) {
- if (multiply == 0)
- return 0;
- if (multiply < 0 || log10(multiply) + log10(create) >= log10(maxProductedMoney) ||
- ((B) multiply) * create > maxProductedMoney - productedMoney) {
- throw "B limit exceeded.";
- return 0;
- } else {
- return multiply * create;
- }
- }
- // Wallet operator*(int n) {
- // Wallet result = *this;
- // result *= n;
- // return result;
- // }
- };
- Wallet::Wallet(string s, int x) {
- B n = (B) (stringToBinary(s) * 100000000);
- n = tryCreateB(n);
- Operation op = Operation(n);
- history.push_back(op);
- productedMoney += n;
- }
- Wallet Wallet::fromBinary(string s) {
- return Wallet(s, 0);
- }
- int main() {
- Operation op = Operation(2019222222837261);
- // cout << op.money << endl;
- // op.printData();
- cout << op;
- Wallet w = Wallet("563.27");
- cout << w.history[0].getUnits() << endl;
- Wallet v = Wallet::fromBinary("100.0100");
- cout << v.history[0].getUnits() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement