Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <cassert>
- #include <vector>
- #include <map>
- #include <fstream>
- #include <algorithm>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- struct Date {
- int year, month, day = 0;
- };
- struct Currency {
- double dollar, euro = 0;
- bool first = 0;
- };
- bool operator < (Date lhs, Date rhs) {
- if (lhs.year < rhs.year) {
- return true;
- } else if (lhs.year == rhs.year and lhs.month < rhs.month) {
- return true;
- } else if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day < rhs.day) {
- return true;
- } else {
- return false;
- }
- }
- bool operator == (Date lhs, Date rhs) {
- if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day == rhs.day) {
- return true;
- }
- return false;
- }
- bool operator > (Date lhs, Date rhs) {
- if (lhs.year > rhs.year) {
- return true;
- } else if (lhs.year == rhs.year and lhs.month > rhs.month) {
- return true;
- } else if (lhs.year == rhs.year and lhs.month == rhs.month and lhs.day > rhs.day) {
- return true;
- } else {
- return false;
- }
- }
- class Currencies {
- public:
- void SaveFile(string filename) {
- ofstream out;
- out.open(filename);
- for (auto item : db) {
- out.precision(2);
- out << item.first.day << "." << item.first.month << "." << item.first.year;
- out << ' ';
- out << item.second.dollar;
- out << ' ';
- out << item.second.euro;
- out << endl;
- }
- }
- void NewEntry(Date date, Currency currency) {
- if (db.find(date) != db.end()) {
- cout << "Entry already exists, rewriting" << endl;
- }
- if (db.empty()) {
- currency.first = 1;
- }
- db[date] = currency;
- cout << "Done!" << endl;
- cout << date.year << " " << date.month << " " << date.day << " " << currency.dollar << " " << currency.euro << " " << endl;
- }
- void LoadFile(string filename, Currencies db) {
- ifstream in;
- Date date;
- Currency currency;
- string s;
- while (!in.eof()) {
- in.open(filename);
- getline(in, s, '.');
- date.year = stoi(s);
- getline(in, s, '.');
- date.month = stoi(s);
- getline(in, s, ' ');
- date.day = stoi(s);
- getline(in, s, ' ');
- currency.dollar = stoi(s);
- getline(in, s, ' ');
- currency.euro = stoi(s);
- }
- db.NewEntry(date, currency);
- }
- void ShowInfo(Date rhs, Date lhs) {
- double tempe, tempd = 0;
- for (auto item : db) {
- if (item.first == lhs or item.second.first == 1) {
- cout.precision(2);
- cout << item.first.day << "." << item.first.month << "." << item.first.year;
- cout << setw(5);
- cout << item.second.dollar;
- cout << setw(5);
- cout << item.second.euro;
- cout << endl;
- continue;
- } else if (item.first < rhs and item.first > lhs) {
- cout.precision(2);
- cout << item.first.day << "." << item.first.month << "." << item.first.year;
- int i = item.second.dollar - tempd;
- if (i > 0) {
- cout << setw(3);
- cout << '+' << i;
- } else if (i < 0) {
- cout << setw(3);
- cout << i;
- } else {
- cout << setw(5);
- cout << i;
- }
- i = item.second.euro - tempe;
- if (i > 0) {
- cout << setw(3);
- cout << '+' << i;
- } else if (i < 0) {
- cout << setw(3);
- cout << i;
- } else {
- cout << setw(5);
- cout << i;
- }
- cout << endl;
- } else if (item.first == rhs) {
- cout.precision(2);
- cout << item.first.day << "." << item.first.month << "." << item.first.year;
- cout << setw(5);
- cout << item.second.dollar;
- cout << setw(5);
- cout << item.second.euro;
- cout << endl;
- }
- tempe = item.second.euro;
- tempd = item.second.dollar;
- }
- }
- void ShowMinMax(Date rhs, Date lhs) {
- vector<double> d, e;
- if (!db.empty()) {
- for (auto item : db) {
- if ((item.first > lhs and item.first < rhs) or item.first == rhs or item.first == lhs) {
- d.push_back(item.second.dollar);
- e.push_back(item.second.euro);
- }
- }
- cout << "Min dollar value is " << *min_element(d.begin(), d.end()) << endl;
- cout << "Max dollar value is " << *max_element(d.begin(), d.end()) << endl;
- cout << "Min euro value is " << *min_element(e.begin(), e.end()) << endl;
- cout << "Max euro value is " << *max_element(e.begin(), e.end()) << endl;
- } else {
- cout << "Database is empty!" << endl;
- }
- }
- private:
- map<Date, Currency> db;
- };
- bool is_number(const string& s)
- {
- return !s.empty() && find_if(s.begin(),
- s.end(), [](char c) { return !isdigit(c); }) == s.end();
- }
- Currency CurrencyParser() {
- Currency currency;
- string s;
- for (;;) {
- cout << "Waiting for dollar input" << endl;
- cin >> s;
- if (!is_number(s)) {
- cout << "Wrong dollar format" << endl;
- continue;
- }
- if (currency.dollar < 0) {
- cout << "Currency value can't be negative" << endl;
- continue;
- }
- currency.dollar = atof(s.c_str());
- break;
- }
- for (;;) {
- cout << "Waiting for euro input";
- cin >> s;
- if (!is_number(s)) {
- cout << "Wrong euro format" << endl;
- continue;
- }
- if (currency.euro < 0) {
- cout << "Currency value can't be negative" << endl;
- continue;
- }
- currency.euro = atof(s.c_str());
- break;
- }
- return currency;
- }
- Date DateParser() {
- Date date;
- vector<int> months {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- string s;
- for (;;) {
- cout << "Waiting for year input" << endl;
- cin >> s;
- if (!is_number(s)) {
- cout << "Wrong year format" << endl;
- continue;
- } else if (stoi(s) < 0) {
- cout << "Year can't be negative" << endl;
- continue;
- }
- date.year = stoi(s);
- break;
- }
- for (;;) {
- cout << "Waiting for month input" << endl;
- cin >> s;
- if (!is_number(s)) {
- cout << "Wrong month format" << endl;
- continue;
- } else if (stoi(s) < 1 or stoi(s) > 12) {
- cout << "Wrong month, use 1-12" << endl;
- continue;
- }
- date.month = stoi(s);
- break;
- }
- for (;;) {
- cout << "Waiting for day input" << endl;
- cin >> s;
- if (!is_number(s)) {
- cout << "Wrong day format" << endl;
- continue;
- } else if (stoi(s) < 0) {
- cout << "Day can't be negative" << endl;
- continue;
- } else if (stoi(s) > months[date.month]) {
- cout << "Wrong amount of days, max is " << months[date.month] << endl;
- continue;
- }
- date.day = stoi(s);
- break;
- }
- return date;
- }
- void ShowMenu() {
- cout << "System menu" << endl;
- cout << "1. Load file" << endl;
- cout << "2. New entry" << endl;
- cout << "3. Show info" << endl;
- cout << "4. Show min and max values" << endl;
- cout << "5. Save file" << endl;
- cout << "6. Exit" << endl;
- }
- void MenuInput(Currencies db) {
- int input = 0;
- string s;
- do {
- ShowMenu();
- cin >> input;
- switch (input) {
- case 1:
- cout << "Waiting for filename input: ";
- cin >> s;
- db.LoadFile(s, db);
- break;
- case 2:
- db.NewEntry(DateParser(), CurrencyParser());
- break;
- case 3:
- db.ShowInfo(DateParser(), DateParser());
- break;
- case 4:
- db.ShowMinMax(DateParser(), DateParser());
- break;
- case 5:
- cout << "Waiting for filename input: ";
- cin >> s;
- db.SaveFile(s);
- break;
- case 6:
- exit(1);
- break;
- }
- } while (input != 6);
- }
- int main() {
- Currencies db;
- MenuInput(db);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement