Advertisement
Guest User

Untitled

a guest
May 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. struct Phone
  11. {
  12.     char NameLast[20];
  13.     int Ram;
  14.     int Battery;
  15.     int Cam_res;
  16.     int Price;
  17.     Phone* Next;
  18. };
  19.  
  20. class List
  21. {
  22.     Phone* Head;
  23. public:
  24.     List() :Head(NULL) {};
  25.     //(Head = NULL)
  26.     ~List();
  27.     void ReadFromFile();
  28.     void Add();
  29.     void Show();
  30.     void LoadToFile();
  31.     void Search();
  32.     void Sort();
  33.     void Task();
  34. };
  35. List::~List()
  36. {
  37.     while (Head != NULL)
  38.     {
  39.         Phone* temp = Head->Next;
  40.         delete Head;
  41.         Head = temp;
  42.     }
  43. }
  44. void List::ReadFromFile()//прочитать файл
  45. {
  46.     ifstream fin("student.txt");
  47.     Phone* temp;
  48.     if (fin.is_open())
  49.     {
  50.         cout << "File is open" << endl;
  51.         while (!fin.eof())
  52.         {
  53.             temp = new Phone;
  54.             fin >> temp->NameLast >> temp->Ram >> temp->Battery >> temp->Cam_res >> temp->Price;
  55.             temp->Next = Head;
  56.             Head = temp;
  57.         }
  58.         cout << "Data is received" << endl;
  59.         fin.close();
  60.     }
  61.     else cout << "File is not found";
  62. }
  63. void List::Show()//показать что в файлы (вывод)
  64. {
  65.     Phone* temp = Head;
  66.     system("cls");
  67.     cout << setw(20) << "Марка" << setw(20) << "RAM" << setw(20) << "Батарея" << setw(20) << "Камера" << setw(20) << "Цена" << endl;
  68.     cout << "______________________________________________________________________________________________" << endl << endl;
  69.     while (temp != NULL)
  70.     {
  71.         cout << setw(20) << temp->NameLast;
  72.         cout << setw(20) << temp->Ram;
  73.         cout << setw(20) << temp->Battery;
  74.         cout << setw(20) << temp->Cam_res;
  75.         cout << setw(20) << temp->Price << endl;
  76.         temp = temp->Next;
  77.     }
  78.     cout << "_______________________________________________________________________________________________" << endl << endl;
  79. }
  80. void List::LoadToFile() {
  81.     char file_name[30];
  82.     system("cls");
  83.     cout << "Input file name:   ";
  84.     cin >> file_name;
  85.     ofstream fout(file_name);
  86.     {
  87.         Phone* temp = Head;
  88.         while (temp != NULL)
  89.         {
  90.             fout << setw(20) << temp->NameLast << setw(20) << temp->Ram << setw(10) << temp->Battery << setw(10) << temp->Cam_res << setw(10) << temp->Price << endl;
  91.             temp = temp->Next;
  92.         }
  93.         cout << "File is completed" << endl;
  94.         system("pause");
  95.         fout.close();
  96.     }
  97.  
  98. }
  99. void List::Add()
  100. {
  101.     Phone st;
  102.     Phone* temp = new Phone;
  103.     temp->Next = Head;
  104.     system("cls");
  105.     cin.ignore();
  106.     cout << "Введите марку: "; cin.getline(st.NameLast, 20);
  107.     cout << "Введите кол-во RAM: "; cin >> st.Ram;
  108.     cin.ignore();
  109.     cout << "Введите кол-во емкости батареи: "; cin >> st.Battery;
  110.     cin.ignore();
  111.     cout << "Введите разшкние камеры: "; cin >> st.Cam_res;
  112.     cin.ignore();
  113.     cout << "Цены: "; cin >> st.Price;
  114.     cin.ignore();
  115.  
  116.     strcpy(temp->NameLast, st.NameLast);
  117.     temp->Ram = st.Ram;
  118.     temp->Battery = st.Battery;
  119.     temp->Cam_res = st.Cam_res;
  120.     temp->Price = st.Price;
  121.     Head = temp;
  122. }
  123.  
  124. void List::Sort() {
  125.  
  126.     Phone* left = Head;
  127.     Phone* right = Head->Next;
  128.     Phone* temp = new Phone;
  129.     while (left->Next) {
  130.         while (right) {
  131.             if ((left->Price) < (right->Price)) {
  132.                 temp->Price = left->Price;
  133.                 left->Price = right->Price;
  134.                 right->Price = temp->Price;
  135.             }
  136.             right = right->Next;
  137.         }
  138.         left = left->Next;
  139.         right = left->Next;
  140.     }
  141. }
  142.  
  143. void List::Search() {
  144.     Phone* temp;
  145.     char e[20];
  146.     cout << "Введите название марки: ";
  147.     cin >> e;
  148.     int k = 0;
  149.     temp = Head;
  150.     while (temp->Next)//пока есть значения
  151.     {
  152.         //strcmp тобишь если совпадает (temp->NameLast-наш элемент из списка) с нашим (e) то выдает 0
  153.         if (strcmp(temp->NameLast, e) == 0)   //  если el - строка, то нужно объявить как char el[20], если символ, то сравнивать можно без strcmp
  154.         {
  155.             cout<<setw(10)<<temp->NameLast<<setw(10)<<temp->Ram<<setw(10)<<temp->Battery<<setw(10)<<temp->Cam_res<<setw(10)<<temp->Price<< "\n";
  156.             k++;
  157.             //break; // чтобы выйти из цикла при нахождении первого элемента
  158.         }
  159.         temp = temp->Next;
  160.     }
  161.     if (k==0)
  162.         cout << "Ничего не найдено :(" << endl;
  163. }
  164. void List::Task() {
  165.     Phone* temp;
  166.     temp = Head;
  167.     int R;
  168.     int B;
  169.     int C;
  170.     int k = 0;
  171.     cout << "Введите кол-во RAM: ";
  172.     cin >> R;
  173.     cout << "Введите емкость батареии: ";
  174.     cin >> B;
  175.     cout << "Введите разрешение камеры: ";
  176.     cin >> C;
  177.     while (temp->Next)//пока есть значения
  178.     {
  179.         if (temp->Ram>=R&&temp->Battery>=B&&temp->Cam_res>=C) {
  180.             cout << setw(10) << temp->NameLast << setw(10) << temp->Ram << setw(10) << temp->Battery << setw(10) << temp->Cam_res << setw(10) << temp->Price << "\n";
  181.             k++;
  182.         }
  183.         temp = temp->Next;
  184.     }
  185.     if (k == 0)
  186.         cout << "Ничего не найдено :(" << endl;
  187. }
  188.  
  189.  
  190. int main() {
  191.     setlocale(0, "");
  192.     cout.setf(ios::left);
  193.     bool flag = true;
  194.     int choice;
  195.     Phone phone;
  196.     List spisok;
  197.     while (flag)
  198.     {
  199.         system("cls");
  200.         cout << "   Меню" << endl;
  201.         cout << "_____________________________" << endl;
  202.         cout << "1: Read from the file" << endl;
  203.         cout << "2: Show list" << endl;
  204.         cout << "3: Add telephone" << endl;
  205.         cout << "4: Save to the file" << endl;
  206.         cout << "5: Task" << endl;
  207.         cout << "6: Search" << endl;
  208.         cout << "7: Sort" << endl;
  209.         cout << "8: Exit" << endl;
  210.         cout << "_____________________________" << endl << endl;
  211.         cout << "Make your choice (1-8): ";
  212.         cin >> choice;
  213.         switch (choice)
  214.         {
  215.         case 1: spisok.ReadFromFile();
  216.             system("PAUSE");
  217.             break;
  218.         case 2: spisok.Show();
  219.             system("PAUSE");
  220.             break;
  221.         case 3: spisok.Add();
  222.             break;
  223.         case 4: spisok.LoadToFile();
  224.             break;
  225.         case 5: spisok.Task();
  226.             system("PAUSE");
  227.             break;
  228.         case 6: spisok.Search();
  229.             system("PAUSE");
  230.             break;
  231.         case 7: spisok.Sort();
  232.             system("PAUSE");
  233.             break;
  234.         case 8: flag = false;
  235.             break;
  236.         default: cout << "You are  wrong.  ";
  237.         }
  238.     }
  239.     system("PAUSE");
  240.     return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement