Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- struct present_s {
- std::string m_Name;
- int m_iDay;
- int m_iMonth;
- int m_iYear;
- int m_iSells;
- int m_iCost;
- };
- std::vector<struct present_s> g_Presents;
- void add_present_string(const char* pStr)
- {
- struct present_s present;
- char szName[64] = {0};
- sscanf(pStr,"%s %d %d %d %d %d",szName,&present.m_iDay,
- &present.m_iMonth,&present.m_iYear,
- &present.m_iSells,&present.m_iCost);
- present.m_Name = std::string(szName);
- g_Presents.push_back(present);
- }
- void add_present(FILE* in = stdin)
- {
- char szBuf[256] = {0};
- std::cout << "Input in format: Name Day Month Year Sells Cost" << std::endl;
- fflush(in);
- fgets(szBuf,256,in);
- size_t uLen = strlen(szBuf);
- if(szBuf[uLen-1] == '\n')
- szBuf[uLen-1] = '\0';
- add_present_string(szBuf);
- }
- void save_presents(FILE* out)
- {
- for(auto it = g_Presents.begin(); it != g_Presents.end(); ++it)
- {
- fprintf(out,"%s %d %d %d %d %d\n",
- it->m_Name.c_str(),
- it->m_iDay,it->m_iMonth,it->m_iYear,
- it->m_iSells,it->m_iCost);
- }
- }
- void show_all(int day = 0)
- {
- std::cout << std::endl << std::endl;
- std::vector<struct present_s>::iterator it;
- std::cout << std::endl << "Name\tDay\tMonth\tYear\tSells\tCost" << std::endl;
- for(auto it = g_Presents.begin(); it != g_Presents.end(); ++it)
- {
- if(day == 0 || day == it->m_iDay)
- {
- std::cout << it->m_Name
- << '\t' << it->m_iDay
- << '\t' << it->m_iMonth
- << '\t' << it->m_iYear
- << '\t' << it->m_iSells
- << '\t' << it->m_iCost
- << std::endl;
- }
- }
- }
- static bool _present_sort_name_op(struct present_s& one,struct present_s& two)
- {
- return one.m_Name < two.m_Name;
- }
- void sort_by_name()
- {
- std::sort(g_Presents.begin(),g_Presents.end(),_present_sort_name_op);
- }
- static bool _present_sort_cost_op(struct present_s& one,struct present_s& two)
- {
- return one.m_iCost < two.m_iCost;
- }
- void sort_by_cost()
- {
- std::sort(g_Presents.begin(),g_Presents.end(),_present_sort_cost_op);
- }
- int main()
- {
- while(1)
- {
- int choice,day;
- std::string filename;
- FILE* f;
- std::cout
- << "1. Add present" << std::endl
- << "2. Show all presents" << std::endl
- << "3. Sort by name" << std::endl
- << "4. Sort by cost" << std::endl
- << "5. Show all in day" << std::endl
- << "6. Add from file" << std::endl
- << "7. Save to file" << std::endl
- << "8. Exit" << std::endl;
- std::cout.flush();
- std::cin >> choice;
- fflush(stdin);
- switch(choice)
- {
- case 1: add_present(); break;
- case 2: show_all(); break;
- case 3: sort_by_name(); show_all(); break;
- case 4: sort_by_cost(); show_all(); break;
- case 5:
- std::cout << "Day: ";
- std::cin >> day;
- show_all(day);
- break;
- case 6:
- std::cout << "Enter filename: " << std::endl;
- std::getline(std::cin,filename);
- f = fopen(filename.c_str(),"r");
- if(f) add_present(f);
- else
- {
- std::cout << "Wrong file!" << std::endl;
- break;
- }
- fclose(f);
- break;
- case 7:
- std::cout << "Enter filename: " << std::endl;
- std::getline(std::cin,filename);
- f = fopen(filename.c_str(),"w");
- if(f) save_presents(f);
- else
- {
- std::cout << "Wrong file!" << std::endl;
- break;
- }
- fclose(f);
- break;
- case 8: return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment