shushus

Спринт 13. Возврат нескольких значений. Распаковка.

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