Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <stdlib.h>
- #include <iomanip>
- using namespace std;
- struct Phone
- {
- char NameLast[20];
- int Ram;
- int Battery;
- int Cam_res;
- int Price;
- Phone* Next;
- };
- class List
- {
- Phone* Head;
- public:
- List() :Head(NULL) {};
- //(Head = NULL)
- ~List();
- void ReadFromFile();
- void Add();
- void Show();
- void LoadToFile();
- void Search();
- void Sort();
- void Task();
- };
- List::~List()
- {
- while (Head != NULL)
- {
- Phone* temp = Head->Next;
- delete Head;
- Head = temp;
- }
- }
- void List::ReadFromFile()//прочитать файл
- {
- ifstream fin("student.txt");
- Phone* temp;
- if (fin.is_open())
- {
- cout << "File is open" << endl;
- while (!fin.eof())
- {
- temp = new Phone;
- fin >> temp->NameLast >> temp->Ram >> temp->Battery >> temp->Cam_res >> temp->Price;
- temp->Next = Head;
- Head = temp;
- }
- cout << "Data is received" << endl;
- fin.close();
- }
- else cout << "File is not found";
- }
- void List::Show()//показать что в файлы (вывод)
- {
- Phone* temp = Head;
- system("cls");
- cout << setw(20) << "Марка" << setw(20) << "RAM" << setw(20) << "Батарея" << setw(20) << "Камера" << setw(20) << "Цена" << endl;
- cout << "______________________________________________________________________________________________" << endl << endl;
- while (temp != NULL)
- {
- cout << setw(20) << temp->NameLast;
- cout << setw(20) << temp->Ram;
- cout << setw(20) << temp->Battery;
- cout << setw(20) << temp->Cam_res;
- cout << setw(20) << temp->Price << endl;
- temp = temp->Next;
- }
- cout << "_______________________________________________________________________________________________" << endl << endl;
- }
- void List::LoadToFile() {
- char file_name[30];
- system("cls");
- cout << "Input file name: ";
- cin >> file_name;
- ofstream fout(file_name);
- {
- Phone* temp = Head;
- while (temp != NULL)
- {
- fout << setw(20) << temp->NameLast << setw(20) << temp->Ram << setw(10) << temp->Battery << setw(10) << temp->Cam_res << setw(10) << temp->Price << endl;
- temp = temp->Next;
- }
- cout << "File is completed" << endl;
- system("pause");
- fout.close();
- }
- }
- void List::Add()
- {
- Phone st;
- Phone* temp = new Phone;
- temp->Next = Head;
- system("cls");
- cin.ignore();
- cout << "Введите марку: "; cin.getline(st.NameLast, 20);
- cout << "Введите кол-во RAM: "; cin >> st.Ram;
- cin.ignore();
- cout << "Введите кол-во емкости батареи: "; cin >> st.Battery;
- cin.ignore();
- cout << "Введите разшкние камеры: "; cin >> st.Cam_res;
- cin.ignore();
- cout << "Цены: "; cin >> st.Price;
- cin.ignore();
- strcpy(temp->NameLast, st.NameLast);
- temp->Ram = st.Ram;
- temp->Battery = st.Battery;
- temp->Cam_res = st.Cam_res;
- temp->Price = st.Price;
- Head = temp;
- }
- void List::Sort() {
- Phone* left = Head;
- Phone* right = Head->Next;
- Phone* temp = new Phone;
- while (left->Next) {
- while (right) {
- if ((left->Price) < (right->Price)) {
- temp->Price = left->Price;
- left->Price = right->Price;
- right->Price = temp->Price;
- }
- right = right->Next;
- }
- left = left->Next;
- right = left->Next;
- }
- }
- void List::Search() {
- Phone* temp;
- char e[20];
- cout << "Введите название марки: ";
- cin >> e;
- int k = 0;
- temp = Head;
- while (temp->Next)//пока есть значения
- {
- //strcmp тобишь если совпадает (temp->NameLast-наш элемент из списка) с нашим (e) то выдает 0
- if (strcmp(temp->NameLast, e) == 0) // если el - строка, то нужно объявить как char el[20], если символ, то сравнивать можно без strcmp
- {
- cout<<setw(10)<<temp->NameLast<<setw(10)<<temp->Ram<<setw(10)<<temp->Battery<<setw(10)<<temp->Cam_res<<setw(10)<<temp->Price<< "\n";
- k++;
- //break; // чтобы выйти из цикла при нахождении первого элемента
- }
- temp = temp->Next;
- }
- if (k==0)
- cout << "Ничего не найдено :(" << endl;
- }
- void List::Task() {
- Phone* temp;
- temp = Head;
- int R;
- int B;
- int C;
- int k = 0;
- cout << "Введите кол-во RAM: ";
- cin >> R;
- cout << "Введите емкость батареии: ";
- cin >> B;
- cout << "Введите разрешение камеры: ";
- cin >> C;
- while (temp->Next)//пока есть значения
- {
- if (temp->Ram>=R&&temp->Battery>=B&&temp->Cam_res>=C) {
- cout << setw(10) << temp->NameLast << setw(10) << temp->Ram << setw(10) << temp->Battery << setw(10) << temp->Cam_res << setw(10) << temp->Price << "\n";
- k++;
- }
- temp = temp->Next;
- }
- if (k == 0)
- cout << "Ничего не найдено :(" << endl;
- }
- int main() {
- setlocale(0, "");
- cout.setf(ios::left);
- bool flag = true;
- int choice;
- Phone phone;
- List spisok;
- while (flag)
- {
- system("cls");
- cout << " Меню" << endl;
- cout << "_____________________________" << endl;
- cout << "1: Read from the file" << endl;
- cout << "2: Show list" << endl;
- cout << "3: Add telephone" << endl;
- cout << "4: Save to the file" << endl;
- cout << "5: Task" << endl;
- cout << "6: Search" << endl;
- cout << "7: Sort" << endl;
- cout << "8: Exit" << endl;
- cout << "_____________________________" << endl << endl;
- cout << "Make your choice (1-8): ";
- cin >> choice;
- switch (choice)
- {
- case 1: spisok.ReadFromFile();
- system("PAUSE");
- break;
- case 2: spisok.Show();
- system("PAUSE");
- break;
- case 3: spisok.Add();
- break;
- case 4: spisok.LoadToFile();
- break;
- case 5: spisok.Task();
- system("PAUSE");
- break;
- case 6: spisok.Search();
- system("PAUSE");
- break;
- case 7: spisok.Sort();
- system("PAUSE");
- break;
- case 8: flag = false;
- break;
- default: cout << "You are wrong. ";
- }
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement