Guest User

Untitled

a guest
Dec 4th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct present_s {
  10.     std::string m_Name;
  11.     int m_iDay;
  12.     int m_iMonth;
  13.     int m_iYear;
  14.     int m_iSells;
  15.     int m_iCost;
  16. };
  17.  
  18. std::vector<struct present_s> g_Presents;
  19.  
  20. void add_present_string(const char* pStr)
  21. {
  22.     struct present_s present;
  23.     char szName[64] = {0};
  24.     sscanf(pStr,"%s %d %d %d %d %d",szName,&present.m_iDay,
  25.         &present.m_iMonth,&present.m_iYear,
  26.         &present.m_iSells,&present.m_iCost);
  27.     present.m_Name = std::string(szName);
  28.     g_Presents.push_back(present);
  29. }
  30.  
  31. void add_present(FILE* in = stdin)
  32. {
  33.     char szBuf[256] = {0};
  34.     std::cout << "Input in format: Name Day Month Year Sells Cost" << std::endl;
  35.     fflush(in);
  36.     fgets(szBuf,256,in);
  37.     size_t uLen = strlen(szBuf);
  38.     if(szBuf[uLen-1] == '\n')
  39.         szBuf[uLen-1] = '\0';
  40.     add_present_string(szBuf);
  41. }
  42.  
  43. void save_presents(FILE* out)
  44. {
  45.     for(auto it = g_Presents.begin(); it != g_Presents.end(); ++it)
  46.     {
  47.         fprintf(out,"%s %d %d %d %d %d\n",
  48.             it->m_Name.c_str(),
  49.             it->m_iDay,it->m_iMonth,it->m_iYear,
  50.             it->m_iSells,it->m_iCost);
  51.     }
  52. }
  53.  
  54. void show_all(int day = 0)
  55. {
  56.     std::cout << std::endl << std::endl;
  57.     std::vector<struct present_s>::iterator it;
  58.     std::cout << std::endl << "Name\tDay\tMonth\tYear\tSells\tCost" << std::endl;
  59.     for(auto it = g_Presents.begin(); it != g_Presents.end(); ++it)
  60.     {
  61.         if(day == 0 || day == it->m_iDay)
  62.         {
  63.             std::cout << it->m_Name
  64.                 << '\t' << it->m_iDay
  65.                 << '\t' << it->m_iMonth
  66.                 << '\t' << it->m_iYear
  67.                 << '\t' << it->m_iSells
  68.                 << '\t' << it->m_iCost
  69.                 << std::endl;
  70.         }
  71.     }
  72. }
  73.  
  74. static bool _present_sort_name_op(struct present_s& one,struct present_s& two)
  75. {
  76.     return one.m_Name < two.m_Name;
  77. }
  78.  
  79. void sort_by_name()
  80. {
  81.     std::sort(g_Presents.begin(),g_Presents.end(),_present_sort_name_op);
  82. }
  83.  
  84. static bool _present_sort_cost_op(struct present_s& one,struct present_s& two)
  85. {
  86.     return one.m_iCost < two.m_iCost;
  87. }
  88.  
  89. void sort_by_cost()
  90. {
  91.     std::sort(g_Presents.begin(),g_Presents.end(),_present_sort_cost_op);
  92. }
  93.  
  94. int main()
  95. {  
  96.     while(1)
  97.     {
  98.         int choice,day;
  99.         std::string filename;
  100.         FILE* f;
  101.         std::cout
  102.             << "1. Add present" << std::endl
  103.             << "2. Show all presents" << std::endl
  104.             << "3. Sort by name" << std::endl
  105.             << "4. Sort by cost" << std::endl
  106.             << "5. Show all in day" << std::endl
  107.             << "6. Add from file" << std::endl
  108.             << "7. Save to file" << std::endl
  109.             << "8. Exit" << std::endl;
  110.         std::cout.flush();
  111.         std::cin >> choice;
  112.         fflush(stdin);
  113.         switch(choice)
  114.         {
  115.             case 1: add_present(); break;
  116.             case 2: show_all(); break;
  117.             case 3: sort_by_name(); show_all(); break;
  118.             case 4: sort_by_cost(); show_all(); break;
  119.             case 5:
  120.                 std::cout << "Day: ";
  121.                 std::cin >> day;
  122.                 show_all(day);
  123.                 break;
  124.             case 6:
  125.                 std::cout << "Enter filename: " << std::endl;
  126.                 std::getline(std::cin,filename);
  127.                
  128.                 f = fopen(filename.c_str(),"r");
  129.                 if(f) add_present(f);
  130.                 else
  131.                 {
  132.                     std::cout << "Wrong file!" << std::endl;
  133.                     break;
  134.                 }
  135.                 fclose(f);
  136.                 break;
  137.             case 7:
  138.                 std::cout << "Enter filename: " << std::endl;
  139.                 std::getline(std::cin,filename);
  140.                
  141.                 f = fopen(filename.c_str(),"w");
  142.                 if(f) save_presents(f);
  143.                 else
  144.                 {
  145.                     std::cout << "Wrong file!" << std::endl;
  146.                     break;
  147.                 }
  148.                 fclose(f);
  149.                 break;
  150.             case 8: return 0;
  151.         }
  152.     }
  153.    
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment