Advertisement
plarmi

cppexam_1_3

Jun 30th, 2023 (edited)
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <vector>
  6. #include <string>
  7. #include <ctime>
  8. #include <algorithm>
  9. #include <windows.h>
  10.  
  11. using namespace std;
  12.  
  13. struct Transaction {
  14.     string category;
  15.     double amount;
  16. };
  17.  
  18. struct ExpenseCategory {
  19.     string category;
  20.     double totalAmount;
  21. };
  22.  
  23. bool compareExpenses(const Transaction& a, const Transaction& b) {
  24.     return a.amount > b.amount;
  25. }
  26.  
  27. bool compareExpenseCategories(const ExpenseCategory& a, const ExpenseCategory& b) {
  28.     return a.totalAmount > b.totalAmount;
  29. }
  30.  
  31. class PersonalFinanceManager {
  32. private:
  33.     vector<double> wallets;
  34.     vector<Transaction> transactions;
  35.  
  36. public:
  37.     void addWallet(double initialAmount) {
  38.         wallets.push_back(initialAmount);
  39.     }
  40.  
  41.     void deposit(int walletIndex, double amount) {
  42.         wallets[walletIndex] += amount;
  43.     }
  44.  
  45.     void addExpense(const string& category, double amount) {
  46.         Transaction transaction;
  47.         transaction.category = category;
  48.         transaction.amount = amount;
  49.         transactions.push_back(transaction);
  50.     }
  51.  
  52.     void generateReports() {
  53.         time_t now = time(nullptr);
  54.         tm* currentDate = localtime(&now);
  55.  
  56.         ofstream file;
  57.  
  58.         if (currentDate->tm_mday < 10 && (currentDate->tm_mon + 1) < 10 ) {
  59.             string filename = "reports_" +
  60.                               to_string(currentDate->tm_mday) +
  61.                               to_string(currentDate->tm_mon + 1) +
  62.                               to_string(currentDate->tm_year + 1900) +
  63.                               ".txt";
  64.             file.open(filename);
  65.         }
  66.  
  67.         // Generate daily report
  68.         file << "Отчёт за день (" << currentDate->tm_mday << "." << currentDate->tm_mon + 1 << "." << currentDate->tm_year + 1900 << "):\n";
  69.         double totalExpenses = 0.0;
  70.         for (const auto& transaction : transactions) {
  71.             totalExpenses += transaction.amount;
  72.         }
  73.         file << "Всего затрат: " << totalExpenses << "\n\n";
  74.  
  75.         // Generate weekly report
  76.         file << "Отчёт за неделю:\n";
  77.         double totalExpensesWeek = 0.0;
  78.         for (const auto& transaction : transactions) {
  79.             totalExpensesWeek += transaction.amount;
  80.         }
  81.         file << "Всего затрат: " << totalExpensesWeek << "\n";
  82.  
  83.         // Top 3 expenses of the week
  84.         sort(transactions.begin(), transactions.end(), compareExpenses);
  85.         file << "Топ 3 затрат:\n";
  86.         for (int i = 0; i < 3 && i < transactions.size(); ++i) {
  87.             file << i + 1 << ". Категория: " << transactions[i].category << ", Сумма: " << transactions[i].amount << "\n";
  88.         }
  89.         file << "\n";
  90.  
  91.         // Top 3 expense categories of the week
  92.         vector<ExpenseCategory> expenseCategories;
  93.         for (const auto& transaction : transactions) {
  94.             auto it = find_if(expenseCategories.begin(), expenseCategories.end(), [&](const ExpenseCategory& ec) {
  95.                 return ec.category == transaction.category;
  96.             });
  97.             if (it != expenseCategories.end()) {
  98.                 it->totalAmount += transaction.amount;
  99.             } else {
  100.                 ExpenseCategory ec;
  101.                 ec.category = transaction.category;
  102.                 ec.totalAmount = transaction.amount;
  103.                 expenseCategories.push_back(ec);
  104.             }
  105.         }
  106.         sort(expenseCategories.begin(), expenseCategories.end(), compareExpenseCategories);
  107.         file << "Топ 3 затратных категорий:\n";
  108.         for (int i = 0; i < 3 && i < expenseCategories.size(); ++i) {
  109.             file << i + 1 << ". Категория: " << expenseCategories[i].category << ", Сумма: " << expenseCategories[i].totalAmount << "\n";
  110.         }
  111.         file << "\n";
  112.  
  113.         // Generate monthly report
  114.         file << "Отчёт за месяц (" << currentDate->tm_mon + 1 << "." << currentDate->tm_year + 1900 << "):\n";
  115.         double totalExpensesMonth = 0.0;
  116.         for (const auto& transaction : transactions) {
  117.             totalExpensesMonth += transaction.amount;
  118.         }
  119.         file << "Всего затрат: " << totalExpensesMonth << "\n\n";
  120.  
  121.         // Top 3 expenses of the month
  122.         file << "Топ 3 затратных категорий за месяц:\n";
  123.         sort(transactions.begin(), transactions.end(), compareExpenses);
  124.         for (int i = 0; i < 3 && i < transactions.size(); ++i) {
  125.             file << i + 1 << ". Категория: " << transactions[i].category << ", Сумма: " << transactions[i].amount << "\n";
  126.         }
  127.         file << "\n";
  128.  
  129.         // Top 3 expenses of the month
  130.         file << "Топ 3 затрат за месяц:\n";
  131.         for (int i = 0; i < 3 && i < transactions.size(); ++i) {
  132.             file << i + 1 << ". Категория: " << transactions[i].category << ", Сумма: " << transactions[i].amount << "\n";
  133.         }
  134.         file << "\n";
  135.  
  136.         // Top 3 expense categories of the month
  137.         file << "Топ 3 категорий за месяц:\n";
  138.         sort(expenseCategories.begin(), expenseCategories.end(), compareExpenseCategories);
  139.         for (int i = 0; i < 3 && i < expenseCategories.size(); ++i) {
  140.             file << i + 1 << ". Категория: " << expenseCategories[i].category << ", Сумма: " << expenseCategories[i].totalAmount << "\n";
  141.         }
  142.         file << "\n";
  143.  
  144.         // Close the file
  145.         file.close();
  146.     }
  147. };
  148.  
  149. int main() {
  150.     SetConsoleCP(1251);
  151.     SetConsoleOutputCP(1251);
  152.    
  153.     PersonalFinanceManager manager;
  154.  
  155.     // Add wallets
  156.     manager.addWallet(1000.0);
  157.     manager.addWallet(500.0);
  158.  
  159.     // Deposit into wallets
  160.     manager.deposit(0, 200.0);
  161.     manager.deposit(1, 100.0);
  162.  
  163.     // Add expenses
  164.     manager.addExpense("Еда", 50.0);
  165.     manager.addExpense("Транспорт", 20.0);
  166.     manager.addExpense("Одежда", 100.0);
  167.     manager.addExpense("Аренда", 500.0);
  168.     manager.addExpense("Инструменты", 80.0);
  169.     manager.addExpense("Развлечения", 60.0);
  170.     manager.addExpense("Еда", 30.0);
  171.     manager.addExpense("Одежда", 200.0);
  172.  
  173.     // Generate reports
  174.     manager.generateReports();
  175.  
  176.     return 0;
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement