Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include "parser.h"
- int main() {
- BudgetManager manager;
- const int query_count = parser::read_number_on_line(std::cin);
- for (int i = 0; i < query_count; ++i) {
- std::string line;
- std::getline(std::cin, line);
- parser::parse_and_process_query(manager, line);
- }
- }
- -----------------------------------------------------------------------
- budget_manager.h
- -----------------------------------------------------------------------
- #pragma once
- #include "date.h"
- struct Day {
- double earnings_;
- double spending_;
- };
- class BudgetManager {
- public:
- inline static const Date START_DATE{2000, 1, 1};
- inline static const Date END_DATE{2100, 1, 1};
- BudgetManager();
- void earn(Date first, Date last, double income);
- void spend(Date first, Date last, double spendings);
- double compute_income(Date first, Date last);
- void pay_tax(Date first, Date last, double tax_rate);
- private:
- std::vector<Day> days_;
- };
- -----------------------------------------------------------------------
- budget_manager.cpp
- -----------------------------------------------------------------------
- #include <iostream>
- #include "budget_manager.h"
- BudgetManager::BudgetManager() {
- days_.resize(END_DATE.ComputeDistance(START_DATE, END_DATE));
- }
- void BudgetManager::earn(Date first, Date last, double income) {
- double money;
- int from = START_DATE.ComputeDistance(START_DATE, first);
- int to = START_DATE.ComputeDistance(START_DATE, last);
- if (first.ComputeDistance(first, last)) {
- money = income/(first.ComputeDistance(first, last) + 1);
- } else {
- money = income;
- }
- for (int i = from; i <= to; ++i) {
- days_.at(i).earnings_ += money;
- }
- }
- void BudgetManager::spend(Date first, Date last, double spendings) {
- double money;
- int from = START_DATE.ComputeDistance(START_DATE, first);
- int to = START_DATE.ComputeDistance(START_DATE, last);
- if (first.ComputeDistance(first, last)) {
- money = spendings/(first.ComputeDistance(first, last) + 1);
- } else {
- money = spendings;
- }
- for (int i = from; i <= to; ++i) {
- days_.at(i).spending_ += money;
- }
- }
- double BudgetManager::compute_income(Date first, Date last) {
- double result = 0.0;
- int from = START_DATE.ComputeDistance(START_DATE, first);
- int to = START_DATE.ComputeDistance(START_DATE, last);
- for (int i = from; i <= to; ++i) {
- double pure_income = days_.at(i).earnings_ - days_.at(i).spending_;
- result += pure_income;
- }
- std::cout << result << std::endl;
- return result;
- }
- void BudgetManager::pay_tax(Date first, Date last, double tax_rate) {
- int from = START_DATE.ComputeDistance(START_DATE, first);
- int to = START_DATE.ComputeDistance(START_DATE, last);
- double tax = 1.0 - tax_rate/100.0;
- for (int i = from; i <= to; ++i) {
- days_.at(i).earnings_ *= tax;
- }
- }
- -----------------------------------------------------------------------
- date.h
- -----------------------------------------------------------------------
- #pragma once
- #include <regex>
- #include <string>
- #include <string_view>
- class Date {
- public:
- Date() {}
- Date(int year, int month, int day) : year_(year)
- , month_(month)
- , day_(day) {}
- static Date FromString(const std::string& str) {
- static const std::regex date_regex(R"/(([0-9]{4})-([0-9]{2})-([0-9]{2}))/");
- std::smatch m;
- if (!std::regex_match(str, m, date_regex)) {return {};}
- return Date(std::stoi(m[1].str()), std::stoi(m[2].str()), std::stoi(m[3].str()));
- }
- static Date FromString(std::string_view str) {return FromString(std::string(str));}
- static int ComputeDistance(Date from, Date to) {
- auto date_to_seconds = [](Date date) {
- tm timestamp;
- timestamp.tm_sec = 0;
- timestamp.tm_min = 0;
- timestamp.tm_hour = 0;
- timestamp.tm_mday = date.day_;
- timestamp.tm_mon = date.month_ - 1;
- timestamp.tm_year = date.year_ - 1900;
- timestamp.tm_isdst = 0;
- return mktime(×tamp);
- };
- static constexpr int SECONDS_IN_DAY = 60 * 60 * 24;
- return static_cast<int>((date_to_seconds(to) - date_to_seconds(from)) / SECONDS_IN_DAY);
- }
- private:
- int year_ = 0;
- int month_ = 0;
- int day_ = 0;
- };
- -----------------------------------------------------------------------
- parser.h
- -----------------------------------------------------------------------
- #pragma once
- #include "budget_manager.h"
- #include <iostream>
- namespace parser {
- std::vector<std::string> split_into_words(const std::string text);
- void parse_and_process_query(BudgetManager& budget_manager, std::string line);
- int read_number_on_line(std::istream& input);
- }//end namespace parser
- -----------------------------------------------------------------------
- parser.cpp
- -----------------------------------------------------------------------
- #include "parser.h"
- namespace parser {
- std::vector<std::string> split_into_words(const std::string text) {
- std::vector<std::string> words;
- std::string word;
- for (const char ch : text) {
- if (ch == ' ') {
- if (!word.empty()) {
- words.push_back(word);
- word.clear();
- }
- } else {
- word += ch;
- }
- }
- if (!word.empty()) {
- words.push_back(word);
- }
- return words;
- }
- void parse_and_process_query(BudgetManager& budget_manager, std::string line) {
- std::vector<std::string> words = split_into_words(line);
- Date date;
- if (words.at(0) == "Earn") {
- budget_manager.earn(date.FromString(words.at(1)),
- date.FromString(words.at(2)),
- std::stod(words.at(3)));
- } else if (words.at(0) == "Spend") {
- budget_manager.spend(date.FromString(words.at(1)),
- date.FromString(words.at(2)),
- std::stod(words.at(3)));
- } else if (words.at(0) == "ComputeIncome") {
- budget_manager.compute_income(date.FromString(words.at(1)),
- date.FromString(words.at(2)));
- } else if (words.at(0) == "PayTax") {
- budget_manager.pay_tax(date.FromString(words.at(1)),
- date.FromString(words.at(2)),
- std::stod(words.at(3)));
- }
- }
- int read_number_on_line(std::istream& input) {
- std::string line;
- std::getline(input, line);
- return std::stoi(line);
- }
- }//end namespace parser
Advertisement
Add Comment
Please, Sign In to add comment