shushus

Спринт 13. Исключения как способ возврата.

Apr 20th, 2023
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "parser.h"
  5.  
  6. int main() {
  7.     BudgetManager manager;
  8.  
  9.     const int query_count = parser::read_number_on_line(std::cin);
  10.  
  11.     for (int i = 0; i < query_count; ++i) {
  12.         std::string line;
  13.         std::getline(std::cin, line);
  14.         parser::parse_and_process_query(manager, line);
  15.     }
  16. }
  17. -----------------------------------------------------------------------
  18. budget_manager.h
  19. -----------------------------------------------------------------------
  20. #pragma once
  21. #include "date.h"
  22.  
  23. struct Day {
  24.     double earnings_;
  25.     double spending_;
  26. };
  27.  
  28. class BudgetManager {
  29. public:
  30.     inline static const Date START_DATE{2000, 1, 1};
  31.     inline static const Date END_DATE{2100, 1, 1};
  32.  
  33.     BudgetManager();
  34.  
  35.     void earn(Date first, Date last, double income);
  36.     void spend(Date first, Date last, double spendings);
  37.     double compute_income(Date first, Date last);
  38.     void pay_tax(Date first, Date last, double tax_rate);
  39.  
  40. private:
  41.     std::vector<Day> days_;
  42.  
  43. };
  44. -----------------------------------------------------------------------
  45. budget_manager.cpp
  46. -----------------------------------------------------------------------
  47. #include <iostream>
  48. #include "budget_manager.h"
  49.  
  50. BudgetManager::BudgetManager() {
  51.     days_.resize(END_DATE.ComputeDistance(START_DATE, END_DATE));
  52. }
  53.  
  54. void BudgetManager::earn(Date first, Date last, double income) {
  55.  
  56.     double money;
  57.     int from = START_DATE.ComputeDistance(START_DATE, first);
  58.     int to = START_DATE.ComputeDistance(START_DATE, last);
  59.  
  60.     if (first.ComputeDistance(first, last)) {
  61.         money = income/(first.ComputeDistance(first, last) + 1);
  62.  
  63.     } else {
  64.         money = income;
  65.     }
  66.  
  67.     for (int i = from; i <= to; ++i) {
  68.         days_.at(i).earnings_ += money;
  69.     }
  70. }
  71.  
  72. void BudgetManager::spend(Date first, Date last, double spendings) {
  73.     double money;
  74.     int from = START_DATE.ComputeDistance(START_DATE, first);
  75.     int to = START_DATE.ComputeDistance(START_DATE, last);
  76.  
  77.     if (first.ComputeDistance(first, last)) {
  78.         money = spendings/(first.ComputeDistance(first, last) + 1);
  79.  
  80.     } else {
  81.         money = spendings;
  82.     }
  83.  
  84.     for (int i = from; i <= to; ++i) {
  85.         days_.at(i).spending_ += money;
  86.     }
  87. }
  88.  
  89. double BudgetManager::compute_income(Date first, Date last) {
  90.  
  91.     double result = 0.0;
  92.     int from = START_DATE.ComputeDistance(START_DATE, first);
  93.     int to = START_DATE.ComputeDistance(START_DATE, last);
  94.  
  95.     for (int i = from; i <= to; ++i) {
  96.         double pure_income = days_.at(i).earnings_ - days_.at(i).spending_;
  97.         result += pure_income;
  98.     }
  99.  
  100.     std::cout << result << std::endl;
  101.  
  102.     return result;
  103. }
  104.  
  105. void BudgetManager::pay_tax(Date first, Date last, double tax_rate) {
  106.    
  107.     int from = START_DATE.ComputeDistance(START_DATE, first);
  108.     int to = START_DATE.ComputeDistance(START_DATE, last);
  109.     double tax = 1.0 - tax_rate/100.0;
  110.  
  111.     for (int i = from; i <= to; ++i) {
  112.         days_.at(i).earnings_ *= tax;
  113.     }
  114. }
  115. -----------------------------------------------------------------------
  116. date.h
  117. -----------------------------------------------------------------------
  118. #pragma once
  119.  
  120. #include <regex>
  121. #include <string>
  122. #include <string_view>
  123.  
  124. class Date {
  125. public:
  126.     Date() {}
  127.    
  128.     Date(int year, int month, int day) : year_(year)
  129.                                        , month_(month)
  130.                                        , day_(day) {}
  131.  
  132.     static Date FromString(const std::string& str) {
  133.         static const std::regex date_regex(R"/(([0-9]{4})-([0-9]{2})-([0-9]{2}))/");
  134.         std::smatch m;
  135.  
  136.         if (!std::regex_match(str, m, date_regex)) {return {};}
  137.  
  138.         return Date(std::stoi(m[1].str()), std::stoi(m[2].str()), std::stoi(m[3].str()));
  139.     }
  140.  
  141.     static Date FromString(std::string_view str) {return FromString(std::string(str));}
  142.  
  143.     static int ComputeDistance(Date from, Date to) {
  144.         auto date_to_seconds = [](Date date) {
  145.             tm timestamp;
  146.             timestamp.tm_sec = 0;
  147.             timestamp.tm_min = 0;
  148.             timestamp.tm_hour = 0;
  149.             timestamp.tm_mday = date.day_;
  150.             timestamp.tm_mon = date.month_ - 1;
  151.             timestamp.tm_year = date.year_ - 1900;
  152.             timestamp.tm_isdst = 0;
  153.             return mktime(&timestamp);
  154.         };
  155.  
  156.         static constexpr int SECONDS_IN_DAY = 60 * 60 * 24;
  157.         return static_cast<int>((date_to_seconds(to) - date_to_seconds(from)) / SECONDS_IN_DAY);
  158.     }
  159.  
  160. private:
  161.     int year_ = 0;
  162.     int month_ = 0;
  163.     int day_ = 0;
  164. };
  165. -----------------------------------------------------------------------
  166. parser.h
  167. -----------------------------------------------------------------------
  168. #pragma once
  169. #include "budget_manager.h"
  170.  
  171. #include <iostream>
  172.  
  173. namespace parser {
  174.    
  175. std::vector<std::string> split_into_words(const std::string text);
  176. void parse_and_process_query(BudgetManager& budget_manager, std::string line);
  177. int read_number_on_line(std::istream& input);
  178.    
  179. }//end namespace parser
  180. -----------------------------------------------------------------------
  181. parser.cpp
  182. -----------------------------------------------------------------------
  183. #include "parser.h"
  184.  
  185. namespace parser {
  186.  
  187. std::vector<std::string> split_into_words(const std::string text) {
  188.     std::vector<std::string> words;
  189.     std::string word;
  190.    
  191.     for (const char ch : text) {      
  192.         if (ch == ' ') {
  193.            
  194.             if (!word.empty()) {
  195.                 words.push_back(word);
  196.                 word.clear();
  197.             }
  198.         } else {
  199.             word += ch;
  200.         }
  201.     }
  202.    
  203.     if (!word.empty()) {
  204.         words.push_back(word);
  205.     }
  206.  
  207.     return words;
  208. }
  209.  
  210. void parse_and_process_query(BudgetManager& budget_manager, std::string line) {
  211.    
  212.     std::vector<std::string> words = split_into_words(line);
  213.     Date date;
  214.  
  215.     if (words.at(0) == "Earn") {
  216.         budget_manager.earn(date.FromString(words.at(1)),
  217.                             date.FromString(words.at(2)),
  218.                             std::stod(words.at(3)));
  219.    
  220.     } else if (words.at(0) == "Spend") {
  221.         budget_manager.spend(date.FromString(words.at(1)),
  222.                             date.FromString(words.at(2)),
  223.                             std::stod(words.at(3)));
  224.        
  225.     } else if (words.at(0) == "ComputeIncome") {
  226.         budget_manager.compute_income(date.FromString(words.at(1)),
  227.                                       date.FromString(words.at(2)));
  228.        
  229.     } else if (words.at(0) == "PayTax") {
  230.         budget_manager.pay_tax(date.FromString(words.at(1)),
  231.                                date.FromString(words.at(2)),
  232.                                std::stod(words.at(3)));
  233.        
  234.     }
  235. }
  236.  
  237. int read_number_on_line(std::istream& input) {
  238.     std::string line;
  239.     std::getline(input, line);
  240.    
  241.     return std::stoi(line);
  242. }
  243.    
  244. }//end namespace parser
Advertisement
Add Comment
Please, Sign In to add comment