Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <time.h>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <iterator>
- #include <cstring>
- using namespace std;
- int number_lines = 0;
- struct autosalon
- {
- char mark[50];
- char firm[50];
- char date_manuf[50];
- int mileage;
- int price;
- };
- void read_file(autosalon *autos, string name_file, int flag)
- {
- ifstream fin(name_file);
- if (!fin.is_open()) cout<<"файл не открыт";
- else
- {
- if (name_file == "autosalon.txt")
- {
- number_lines = 0;
- while (!fin.eof())
- {
- fin >> autos[number_lines].mark >> autos[number_lines].firm >> autos[number_lines].date_manuf >> autos[number_lines].mileage >> autos[number_lines].price;
- if (flag==1)
- {
- cout << autos[number_lines].mark << " " << autos[number_lines].firm << " " << autos[number_lines].date_manuf << " " << autos[number_lines].mileage << " " << autos[number_lines].price;
- cout<< "\n";
- }
- number_lines++;
- }
- }
- }
- }
- void write_file(string name_file, string input_string)
- {
- ofstream fout(name_file, ios::app);
- fout << endl;
- fout << input_string;
- }
- void filter_by_price(autosalon *autos, string name_file, int filter)
- {
- read_file(autos, name_file, 0);
- for (int i=0; i<number_lines; i++)
- {
- if (autos[i].price <= filter)
- {
- cout << autos[i].mark << " " << autos[i].date_manuf << " " << autos[i].price;
- cout<< "\n";
- }
- }
- }
- int remove_line(const char *filename, size_t index)
- {
- vector<string> vec;
- ifstream file(filename);
- if (file.is_open())
- {
- string str;
- while (getline(file, str))
- vec.push_back(str);
- file.close();
- if (vec.size() < index)
- return -1;
- vec.erase(vec.begin() + index);
- ofstream outfile(filename);
- if (outfile.is_open())
- {
- copy(vec.begin(), vec.end(),
- ostream_iterator<string>(outfile, "\n"));
- outfile.close();
- return 1;
- }
- return -2;
- }
- return -3;
- }
- void remove_by_firm(autosalon *autos, string name_file, string firm)
- {
- read_file(autos, name_file, 0);
- int x[number_lines];
- int j = 0;
- for (int i=0; i<number_lines; i++)
- {
- if (autos[i].firm == firm)
- {
- x[j] = i;
- j++;
- }
- }
- for (int i=0; i<j; i++)
- {
- remove_line("autosalon.txt", x[i]);
- for (int g=i+1; g<j; g++) x[g]--;
- }
- }
- void rewrite_file(string name_file, string input_string)
- {
- ofstream refout(name_file);
- refout << "";
- refout.close();
- ofstream rfout(name_file, ios::app);
- rfout << input_string;
- }
- void new_file(autosalon *autos, string name_file, string new_name_file)
- {
- read_file(autos, name_file, 0);
- ofstream refout(new_name_file);
- refout << "";
- refout.close();
- ofstream anfout(new_name_file, ios::app);
- for (int i=0; i<number_lines; i++)
- {
- anfout << autos[i].mark << " " << autos[i].mileage << " " << autos[i].price;
- anfout << endl;
- cout << autos[i].mark << " " << autos[i].mileage << " " << autos[i].price;
- cout << endl;
- }
- }
- void set_mark_price(autosalon *autos, string name_file, string old_mark, char new_mark[50], int new_price)
- {
- read_file(autos, name_file, 0);
- for (int i=0; i<number_lines; i++)
- {
- if (autos[i].mark == old_mark)
- {
- strcpy(autos[i].mark, new_mark);
- autos[i].price = new_price;
- }
- }
- ofstream refout(name_file);
- refout << "";
- refout.close();
- ofstream anfout(name_file, ios::app);
- for (int i=0; i<number_lines; i++)
- {
- anfout << autos[i].mark << " " << autos[i].firm << " " << autos[i].date_manuf << " " << autos[i].mileage << " " << autos[i].price;
- anfout << endl;
- }
- }
- void menu(autosalon *autos)
- {
- cout << "Автосалон \n";
- cout << "1. Меню изменения файла базы данных." << endl;
- cout << "2. Выдать на экран содержимое файла полной базы данных." << endl;
- cout << "3. Выдать на экран все автомобили, с ценой не больше заданной с указанием даты выпуска." << endl;
- cout << "4. Сформировать файл: марка автомобиля пробег цена" << endl;
- cout << "5. Выдать на экран содержимое файла: марка автомобиля пробег цена." << endl;
- int count;
- cin >> count;
- char choise;
- if (count == 1)
- {
- cout << "Нажмите 1 чтобы добавить запись в файл" << endl;
- cout << "Нажмите 2 чтобы затереть файл и писать с начала" << endl;
- cout << "Нажмите 3 чтобы для заданной марки автомобиля изменить марка цену." << endl;
- cout << "Нажмите 4 чтобы Удалить все записи указанной фирмы изготовителя." << endl;
- cin >> choise;
- if (choise == '1')
- {
- cout << "Введите строку формата: марка фирма дата_выпуска пробег цена " << endl;
- string mark, firm, date, mileage, price;
- cin >> mark;
- cin >> firm;
- cin >> date;
- cin >> mileage;
- cin >> price;
- string new_string;
- new_string = new_string + mark + " " + firm + " " + date + " " + mileage + " " + price;
- write_file("autosalon.txt", new_string);
- cout << endl;
- cout << "Чтобы показать Меню нажмите m." << endl;
- cout << "Чтобы показать измененный файл нажмите r." << endl;
- cin >> choise;
- if (choise == 'm') menu(autos);
- if (choise == 'r')
- {
- read_file(autos, "autosalon.txt", 1);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- }
- if (choise == '2')
- {
- cout << "Введите строку формата: марка фирма дата_выпуска пробег цена " << endl;
- string mark, firm, date, mileage, price;
- cin >> mark;
- cin >> firm;
- cin >> date;
- cin >> mileage;
- cin >> price;
- string new_string;
- new_string = new_string + mark + " " + firm + " " + date + " " + mileage + " " + price;
- rewrite_file("autosalon.txt", new_string);
- cout << endl;
- cout << "Чтобы показать Меню нажмите m." << endl;
- cout << "Чтобы показать измененный файл нажмите r." << endl;
- cin >> choise;
- if (choise == 'm') menu(autos);
- if (choise == 'r')
- {
- read_file(autos, "autosalon.txt", 1);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- }
- if (choise == '3')
- {
- char mark[50];
- cout << "Введите марку автомобиля" << endl;
- cin >> mark;
- char mark1[50];
- cout << "Введите новую марку автомобиля" << endl;
- cin >> mark1;
- int price;
- cout << "Введите новую цену автомобиля" << endl;
- cin >> price;
- set_mark_price(autos, "autosalon.txt", mark, mark1, price);
- cout << endl;
- cout << "Чтобы показать Меню нажмите m." << endl;
- cout << "Чтобы показать измененный файл нажмите r." << endl;
- cin >> choise;
- if (choise == 'm') menu(autos);
- if (choise == 'r')
- {
- read_file(autos, "autosalon.txt", 1);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- }
- if (choise == '4')
- {
- cout << "Введите фирму" << endl;
- char firm[50];
- cin >> firm;
- remove_by_firm(autos, "autosalon.txt", firm);
- cout << endl;
- cout << "Чтобы показать Меню нажмите m." << endl;
- cout << "Чтобы показать измененный файл нажмите r." << endl;
- cin >> choise;
- if (choise == 'm') menu(autos);
- if (choise == 'r')
- {
- read_file(autos, "autosalon.txt", 1);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- }
- }
- if (count == 2)
- {
- read_file(autos, "autosalon.txt", 1);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- if (count == 3)
- {
- int price;
- cout << "Введите цену для сортировки" << endl;
- cin >> price;
- filter_by_price(autos, "autosalon.txt", price);
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- if ((count == 4) or (count == 5))
- {
- new_file(autos, "autosalon.txt", "autosalon2.txt");
- cout << "Показать меню? y/n " << endl;
- cin >> choise;
- if (choise == 'y')
- {
- cout<<endl<<endl;
- menu(autos);
- }
- else
- {
- cout << "End programm";
- }
- }
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- autosalon *autos = new autosalon[5000];
- menu(autos);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement