Advertisement
kutuzzzov

Урок 2 Зови по имени 2 ч

Mar 1st, 2023
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. // не меняйте файлы xml.h и xml.cpp
  7. #include "xml.h"
  8.  
  9. using namespace std;
  10.  
  11. struct Spending {
  12.     string category;
  13.     int amount;
  14. };
  15.  
  16. int CalculateTotalSpendings(const vector<Spending>& spendings) {
  17.     int result = 0;
  18.     for (const Spending& s : spendings) {
  19.         result += s.amount;
  20.     }
  21.     return result;
  22. }
  23.  
  24. string FindMostExpensiveCategory(const vector<Spending>& spendings) {
  25.     auto compare_by_amount = [](const Spending& lhs, const Spending& rhs) {
  26.         return lhs.amount < rhs.amount;
  27.     };
  28.     return max_element(begin(spendings), end(spendings), compare_by_amount)->category;
  29. }
  30.  
  31. vector<Spending> LoadFromXml(istream& input) {
  32.     // место для вашей реализации
  33.     // пример корректного XML-документа в условии
  34.     std::vector<Spending> result;
  35.     auto knot = Load(input).GetRoot().Children();
  36.     for (const auto& data : knot) {
  37.         int amount = data.AttributeValue<int>("amount");
  38.         std::string category = data.AttributeValue<std::string>("category");
  39.         result.push_back({ category, amount });
  40.     }
  41.     return result;
  42. }
  43.  
  44. int main() {
  45.     const vector<Spending> spendings = LoadFromXml(cin);
  46.     cout << "Total "sv << CalculateTotalSpendings(spendings) << '\n';
  47.     cout << "Most expensive is "sv << FindMostExpensiveCategory(spendings) << '\n';
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement