gashink_t

лаба 18 контейнеры

May 15th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <list>
  2. #include <string>
  3. #include <iomanip>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. struct toys {
  8.     char name[10];
  9.     int price;
  10. };
  11.  
  12. void create(list<toys>& t);
  13. void print(list<toys>& t);
  14. void search(list<toys>& t);
  15. void expensive_toy(list<toys>& t);
  16.  
  17.  
  18. int main()
  19. {
  20.     int n,c;
  21.     cout << "Enter the number of toys: ";
  22.     cin >> n;
  23.     list<toys> toy(n);
  24.     cout << "Entetr the manage command:\n\t1. Create list;\n\t2. Print list;\n\t3. Find the cost of a toy;\n\t4. Display information about the most expensive toy;\n\t5. The end;\n->";
  25.     cin >> c;
  26.     while (c != 5)
  27.     {
  28.         switch (c) {
  29.         case 1:
  30.             create(toy);
  31.             break;
  32.         case 2:
  33.             print(toy);
  34.             break;
  35.         case 3:
  36.             search(toy);
  37.             break;
  38.         case 4:
  39.             expensive_toy(toy);
  40.             break;
  41.         }
  42.         cout << "->";
  43.         cin >> c;
  44.     }
  45.    
  46.     return 0;
  47. }
  48.  
  49. void create(list<toys>& t)
  50. {
  51.     list<toys>::iterator i;
  52.     for (i = t.begin(); i != t.end(); ++i)
  53.     {
  54.         cin.get();
  55.         cin.clear();
  56.         cout << "Enter the information about toy:\nName: ";
  57.         cin.getline(i->name, 10);
  58.         cout << "Price: ";
  59.         cin >> i->price;
  60.         cout << endl;
  61.     }
  62. }
  63.  
  64. void print(list<toys>& t)
  65. {
  66.     list<toys>::iterator i;
  67.     for (i = t.begin(); i != t.end(); ++i)
  68.     {
  69.         cout << "Name: " << setw(10) << left << i->name << setw(5) << left << " Price: " << i->price << endl;
  70.     }
  71. }
  72.  
  73. void search(list<toys>& t)
  74. {
  75.     char Name[10];
  76.     list<toys>::iterator i;
  77.     cout << "Enter the name of the toy: ";
  78.     cin >> Name;
  79.     for (i = t.begin(); i != t.end(); ++i)
  80.     {
  81.         if (strcmp(i->name, Name) == 0)
  82.         {
  83.             cout << "Price:" << i->price << endl;
  84.             break;
  85.         }
  86.     }
  87.     if (i == t.end()) cout << "There is no such toy!" << endl;
  88. }
  89.  
  90. void expensive_toy(list<toys>& t)
  91. {
  92.     list<toys>::iterator i;
  93.     int max = 0;
  94.     cout << "The most expensive toy: " << endl;
  95.     for (i = t.begin(); i != t.end(); ++i)
  96.     {
  97.         if (i->price > max) max = i->price;
  98.     }
  99.     for (i = t.begin(); i != t.end(); ++i)
  100.     {
  101.         if (i->price == max) cout << "Name: " << setw(10) << left << i->name << setw(5) << left << " Price: " << i->price << endl;
  102.     }
  103. }
Add Comment
Please, Sign In to add comment