Advertisement
Guest User

Untitled

a guest
May 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <cassert>
  4. #include <vector>
  5. #include <map>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. struct Date {
  14.     int year, month, day = 0;
  15. };
  16.  
  17. struct Currency {
  18.     double dollar, euro = 0;
  19.     bool first = 0;
  20. };
  21.  
  22.  
  23. bool operator < (Date lhs, Date rhs) {
  24.     if (lhs.year < rhs.year) {
  25.         return true;
  26.     } else if (lhs.year == rhs.year and lhs.month < rhs.month) {
  27.         return true;
  28.     } else if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day < rhs.day) {
  29.         return true;
  30.     } else {
  31.         return false;
  32.     }
  33. }
  34.  
  35. bool operator == (Date lhs, Date rhs) {
  36.     if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day == rhs.day) {
  37.         return true;
  38.     }
  39.     return false;
  40. }
  41.  
  42. bool operator > (Date lhs, Date rhs) {
  43.     if (lhs.year > rhs.year) {
  44.         return true;
  45.     } else if (lhs.year == rhs.year and lhs.month > rhs.month) {
  46.         return true;
  47.     } else if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day > rhs.day) {
  48.         return true;
  49.     } else {
  50.         return false;
  51.     }
  52. }
  53.  
  54. class Currencies {
  55. public:
  56.  
  57.     void SaveFile(string filename) {
  58.         ofstream out;
  59.         out.open(filename);
  60.         for (auto item : db) {
  61.             out.precision(2);
  62.             out << item.first.day << "." << item.first.month << "." << item.first.year;
  63.             out << ' ';
  64.             out << item.second.dollar;
  65.             out << ' ';
  66.             out << item.second.euro;
  67.             out << endl;
  68.         }
  69.     }
  70.  
  71.     void NewEntry(Date date, Currency currency) {
  72.         if (db.find(date) != db.end()) {
  73.             cout << "Entry already exists, rewriting" << endl;
  74.         }
  75.         if (db.empty()) {
  76.             currency.first = 1;
  77.         }
  78.         db[date] = currency;
  79.         cout << "Done!" << endl;
  80.         cout << date.year << " " << date.month << " " << date.day << " " << currency.dollar  << " " << currency.euro << " " << endl;
  81.     }
  82.  
  83.     void LoadFile(string filename, Currencies db) {
  84.         ifstream in;
  85.         Date date;
  86.         Currency currency;
  87.         string s;
  88.         while (!in.eof()) {
  89.             in.open(filename);
  90.             getline(in, s, '.');
  91.             date.year = stoi(s);
  92.             getline(in, s, '.');
  93.             date.month = stoi(s);
  94.             getline(in, s, ' ');
  95.             date.day = stoi(s);
  96.             getline(in, s, ' ');
  97.             currency.dollar = stoi(s);
  98.             getline(in, s, ' ');
  99.             currency.euro = stoi(s);
  100.         }
  101.         db.NewEntry(date, currency);
  102.     }
  103.  
  104.     void ShowInfo(Date rhs, Date lhs) {
  105.         double tempe, tempd = 0;
  106.         for (auto item : db) {
  107.             if (item.first == lhs or item.second.first == 1) {
  108.                 cout.precision(2);
  109.                 cout << item.first.day << "." << item.first.month << "." << item.first.year;
  110.                 cout << setw(5);
  111.                 cout << item.second.dollar;
  112.                 cout << setw(5);
  113.                 cout << item.second.euro;
  114.                 cout << endl;
  115.                 continue;
  116.             } else if (item.first < rhs and item.first > lhs) {
  117.                 cout.precision(2);
  118.                 cout << item.first.day << "." << item.first.month << "." << item.first.year;
  119.                 int i = item.second.dollar - tempd;
  120.                 if (i > 0) {
  121.                     cout << setw(3);
  122.                     cout << '+' << i;
  123.                 } else if (i < 0) {
  124.                     cout << setw(3);
  125.                     cout << i;
  126.                 } else {
  127.                     cout << setw(5);
  128.                     cout << i;
  129.                 }
  130.                 i = item.second.euro - tempe;
  131.                 if (i > 0) {
  132.                     cout << setw(3);
  133.                     cout << '+' << i;
  134.                 } else if (i < 0) {
  135.                     cout << setw(3);
  136.                     cout << i;
  137.                 } else {
  138.                     cout << setw(5);
  139.                     cout << i;
  140.                 }
  141.                 cout << endl;
  142.             } else if (item.first == rhs) {
  143.                 cout.precision(2);
  144.                 cout << item.first.day << "." << item.first.month << "." << item.first.year;
  145.                 cout << setw(5);
  146.                 cout << item.second.dollar;
  147.                 cout << setw(5);
  148.                 cout << item.second.euro;
  149.                 cout << endl;
  150.             }
  151.             tempe = item.second.euro;
  152.             tempd = item.second.dollar;
  153.         }
  154.     }
  155.  
  156.     void ShowMinMax(Date rhs, Date lhs) {
  157.         vector<double> d, e;
  158.         if (!db.empty()) {
  159.             for (auto item : db) {
  160.                         if ((item.first > lhs and item.first < rhs) or item.first == rhs or item.first == lhs) {
  161.                             d.push_back(item.second.dollar);
  162.                             e.push_back(item.second.euro);
  163.                         }
  164.                     }
  165.                     cout << "Min dollar value is " << *min_element(d.begin(), d.end()) << endl;
  166.                     cout << "Max dollar value is " << *max_element(d.begin(), d.end()) << endl;
  167.                     cout << "Min euro value is " << *min_element(e.begin(), e.end()) << endl;
  168.                     cout << "Max euro value is " << *max_element(e.begin(), e.end()) << endl;
  169.         } else {
  170.             cout << "Database is empty!" << endl;
  171.         }
  172.     }
  173. private:
  174.     map<Date, Currency> db;
  175. };
  176.  
  177. bool is_number(const string& s)
  178. {
  179.     return !s.empty() && find_if(s.begin(),
  180.         s.end(), [](char c) { return !isdigit(c); }) == s.end();
  181. }
  182.  
  183. Currency CurrencyParser() {
  184.     Currency currency;
  185.     string s;
  186.     for (;;) {
  187.         cout << "Waiting for dollar input" << endl;
  188.         cin >> s;
  189.         if (!is_number(s)) {
  190.             cout << "Wrong dollar format" << endl;
  191.             continue;
  192.         }
  193.         if (currency.dollar < 0) {
  194.             cout << "Currency value can't be negative" << endl;
  195.             continue;
  196.         }
  197.         currency.dollar = atof(s.c_str());
  198.         break;
  199.     }
  200.     for (;;) {
  201.         cout << "Waiting for euro input";
  202.         cin >> s;
  203.         if (!is_number(s)) {
  204.             cout << "Wrong euro format" << endl;
  205.             continue;
  206.         }
  207.         if (currency.euro < 0) {
  208.             cout << "Currency value can't be negative" << endl;
  209.             continue;
  210.         }
  211.         currency.euro = atof(s.c_str());
  212.         break;
  213.     }
  214.     return currency;
  215. }
  216.  
  217. Date DateParser() {
  218.     Date date;
  219.     vector<int> months {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  220.     string s;
  221.     for (;;) {
  222.         cout << "Waiting for year input" << endl;
  223.         cin >> s;
  224.         if (!is_number(s)) {
  225.             cout << "Wrong year format" << endl;
  226.             continue;
  227.         } else if (stoi(s) < 0) {
  228.             cout << "Year can't be negative" << endl;
  229.             continue;
  230.         }
  231.         date.year = stoi(s);
  232.         break;
  233.     }
  234.     for (;;) {
  235.         cout << "Waiting for month input" << endl;
  236.         cin >> s;
  237.         if (!is_number(s)) {
  238.             cout << "Wrong month format" << endl;
  239.             continue;
  240.         } else if (stoi(s) < 1 or stoi(s) > 12) {
  241.             cout << "Wrong month, use 1-12" << endl;
  242.             continue;
  243.         }
  244.         date.month = stoi(s);
  245.         break;
  246.     }
  247.     for (;;) {
  248.         cout << "Waiting for day input" << endl;
  249.         cin >> s;
  250.         if (!is_number(s)) {
  251.             cout << "Wrong day format" << endl;
  252.             continue;
  253.         } else if (stoi(s) < 0) {
  254.             cout << "Day can't be negative" << endl;
  255.             continue;
  256.         } else if (stoi(s) > months[date.month]) {
  257.             cout << "Wrong amount of days, max is " << months[date.month] << endl;
  258.             continue;
  259.         }
  260.         date.day = stoi(s);
  261.         break;
  262.     }
  263.     return date;
  264. }
  265.  
  266. void ShowMenu() {
  267.     cout << "System menu" << endl;
  268.     cout << "1. Load file" << endl;
  269.     cout << "2. New entry" << endl;
  270.     cout << "3. Show info" << endl;
  271.     cout << "4. Show min and max values" << endl;
  272.     cout << "5. Save file" << endl;
  273.     cout << "6. Exit" << endl;
  274. }
  275.  
  276. void MenuInput(Currencies db) {
  277.     int input = 0;
  278.     string s;
  279.     do {
  280.         ShowMenu();
  281.         cin >> input;
  282.         switch (input) {
  283.         case 1:
  284.             cout << "Waiting for filename input: ";
  285.             cin >> s;
  286.             db.LoadFile(s, db);
  287.             break;
  288.         case 2:
  289.             db.NewEntry(DateParser(), CurrencyParser());
  290.             break;
  291.         case 3:
  292.             db.ShowInfo(DateParser(), DateParser());
  293.             break;
  294.         case 4:
  295.             db.ShowMinMax(DateParser(), DateParser());
  296.             break;
  297.         case 5:
  298.             cout << "Waiting for filename input: ";
  299.             cin >> s;
  300.             db.SaveFile(s);
  301.             break;
  302.         case 6:
  303.             exit(1);
  304.             break;
  305.         }
  306.     } while (input != 6);
  307.  
  308. }
  309.  
  310. int main() {
  311.     Currencies db;
  312.     MenuInput(db);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement