LangProgLaba2_N14

Run Settings
LanguageC++
Language Version
Run Command
//14. Структуры.Название кинофильма, сеанс, стоимость билета, количество зрителей. //#define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include <iostream> #include <iomanip> #include <fstream> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> using namespace std; #define N 24 struct ttime { int hour; int min; }; struct Film { char name[N]; ttime session; int price; int viewers; }; struct table { Film Lines[N]; int LinesCount; }; void write_data(table &t) //Ввод данных { int k; cout << "Введите количество записей"; cin >> k; if ((k > 0) && (k < 1001)) { for (int i = t.LinesCount; i < t.LinesCount + k; i++) { cout << "Введите название кинофильма "; cin >> t.Lines[i].name; cout << "Введите время сеанса, сначала часы, затем минуты "; cin >> t.Lines[i].session.hour >> t.Lines[i].session.min; cout << "Введите стоимость "; cin >> t.Lines[i].price; cout << "Введите количество зрителей "; cin >> t.Lines[i].viewers; } t.LinesCount = t.LinesCount + k; } else { cout << "Table overflow!!! " << endl; cout << "You can write number of lines between 1 and 1000 " << endl; } } void write_to_file(table &t) { ofstream file("table.txt"); for (int i = 0; i < t.LinesCount; i++) { file << t.Lines[i].name << " "; file << t.Lines[i].session.hour << " "; file << t.Lines[i].session.min << " "; file << t.Lines[i].price << " "; file << t.Lines[i].viewers << " "; file << endl; } file.close(); } int viewers_of_string(char *fileName) { char *str = new char[1000]; int l = 0; ifstream stroki(fileName); while (!stroki.eof()) { stroki.getline(str, 1000, '\n'); l++; } stroki.close(); delete str; return l - 1; } void load(table &t) { ifstream f("table.txt"); if (f.is_open()) { int currentLine = 0; while (!f.eof()) { f >> t.Lines[currentLine].name; f >> t.Lines[currentLine].session.hour; f >> t.Lines[currentLine].session.min; f >> t.Lines[currentLine].price; f >> t.Lines[currentLine].viewers; currentLine++; } t.LinesCount = currentLine - 1; f.close(); } else { cout << "File is not found" << endl; } } void show(table &t) { //Вывод на экран for (int i = 0; i < t.LinesCount; i++) { cout << t.Lines[i].name << "\t "; cout << t.Lines[i].session.hour << ":" << t.Lines[i].session.min << "\t "; cout << t.Lines[i].price << "\t "; cout << t.Lines[i].viewers; cout << endl; } } void sortname(table &t) { for (int k = 0; k < t.LinesCount - 1; k++) { for (int i = 0; i < t.LinesCount - 1; i++) { for (int j = 0; t.Lines[i].name[j] != '\0'; j++) { if (t.Lines[i].name[j] > t.Lines[i + 1].name[j]) { swap(t.Lines[i], t.Lines[i + 1]); break; } else { break; } } } } } void sortprice(table &t) { for (int k = 0; k < t.LinesCount - 1; k++) { for (int i = 0; i < t.LinesCount - 1; i++) { if (t.Lines[i].price > t.Lines[i + 1].price) { swap(t.Lines[i], t.Lines[i + 1]); } } } } void sortsession(table &t) { for (int k = 0; k < t.LinesCount - 1; k++) { for (int i = 0; i < t.LinesCount - 1; i++) { if (t.Lines[i].session.hour > t.Lines[i + 1].session.hour) { swap(t.Lines[i], t.Lines[i + 1]); } else if ((t.Lines[i].session.hour == t.Lines[i + 1].session.hour) && (t.Lines[i].session.min > t.Lines[i + 1].session.min)) { swap(t.Lines[i], t.Lines[i + 1]); } } } } void sortviewers(table &t) { for (int k = 0; k < t.LinesCount - 1; k++) { for (int i = 0; i < t.LinesCount - 1; i++) { if (t.Lines[i].viewers > t.Lines[i + 1].viewers) { swap(t.Lines[i], t.Lines[i + 1]); } } } } void delete_line(table &t) { int line; int choose; cout << "Хотите удалить строку?" << endl; cout << "1) ДА" << endl; cout << "2) НЕТ" << endl; cin >> choose; switch (choose) { case 1: { cout << "Какую строку удалить?" << endl; cin >> line; int i; for (i = line - 1; i < t.LinesCount - 1; i++) { t.Lines[i] = t.Lines[i + 1]; } //delete (t.Lines[i+1]); t.LinesCount--; } case 2: { goto labelend; } } labelend:; } void findName(table &t) { int i, j, max = 0, min = INT_MAX, k = 0, p = 0; int kk[100]; char B[25]; cin >> B; for (i = 0; i < t.LinesCount; i++) { for (j = 0; B[j] != NULL; j++) { k = k*10 + abs(t.Lines[i].name[j] - B[j]); } kk[i]=k; k = 0; } kk[i] = -1; for (int s=0; kk[s]!=-1; s++) { if (kk[s]<min) min=kk[s]; } for (i = 0; i < t.LinesCount; i++) { if (kk[i]==min) { cout << t.Lines[i].name << " " << t.Lines[i].session.hour << ":" << t.Lines[i].session.min << " " << t.Lines[i].price << " " << t.Lines[i].viewers << endl; } } } void findSession(table &t) //Поиск по сеансу { int h, m; int l = 0; cout << "Введите время сеанса: часы, затем минуты" << endl; cin >> h; cin >> m; int sum = h*60 + m; for (int j = sum, g = sum; (j >= 0) || (g <= 1440); j--, g++) { for (int i = 0; i < t.LinesCount; i++) { if (((t.Lines[i].session.hour*60 + t.Lines[i].session.min - j) == 0) || ((t.Lines[i].session.hour*60 + t.Lines[i].session.min - g) == 0)) { cout << t.Lines[i].name << " " << t.Lines[i].session.hour << ":" << t.Lines[i].session.min << " " << t.Lines[i].price << " " << t.Lines[i].viewers << endl; l = 1; } } if (l == 1) { return; } } } void findPrice(table &t) //Поиск по цене { int g; cout << "Введите цену" << endl; cin >> g; for (int j = g, k = g; (j >= 0) || (k <= 500); j--, k++) { for (int i = 0; i < t.LinesCount; i++) { if (((t.Lines[i].price - j) == 0) || ((t.Lines[i].price - k) == 0)) { cout << t.Lines[i].name << " " << t.Lines[i].session.hour << ":" << t.Lines[i].session.min << " " << t.Lines[i].price << " " << t.Lines[i].viewers << endl; return; } } } } void findCountViewers(table &t) { int g; cout << "Введите количество зрителей" << endl; cin >> g; for (int j = g, k = g; (j > 0)||(k < 1000); j--, k++) { for (int i = 0; i < t.LinesCount; i++) { if (((t.Lines[i].viewers - j) == 0)||((t.Lines[i].viewers - k) == 0)) { cout << t.Lines[i].name << " " << t.Lines[i].session.hour << ":" << t.Lines[i].session.min << " " << t.Lines[i].price << " " << t.Lines[i].viewers << endl; return; } } } } void mostProfitableSession(table &t) { int maxProfit = 0; int res; for (int i = 0; i < t.LinesCount; i++) { for (int j = 0; j < t.LinesCount; j++) { if (t.Lines[i].price * t.Lines[i].viewers > maxProfit) { maxProfit = t.Lines[i].price * t.Lines[i].viewers; res = i; } } } cout << t.Lines[res].name << " " << t.Lines[res].session.hour << ":" << t.Lines[res].session.min << " " << t.Lines[res].price << " " << t.Lines[res].viewers << endl; } void edit(table &t) { int x, y; cout << "Введите номер записи, которую хотите изменить" << endl; cin >> x; x--; point4:; cout << "Что надо изменить?" << endl << "1) Название фильма " << endl << " 2) Время сеанса " << endl << " 3) Цена " << endl << " 4) Количество зрителей " << endl << " 0) Выйти" << endl; cin >> y; switch (y) { case 1: { cout << "Введите название фильма" << endl; cin >> t.Lines[x].name; goto point4; } case 2: { cout << "Введите время сеанса" << endl; cin >> t.Lines[x].session.hour; cin >> t.Lines[x].session.min; goto point4; } case 3: { cout << "Введите цену" << endl; cin >> t.Lines[x].price; goto point4; } case 4: { cout << "Введите количество зрителей" << endl; cin >> t.Lines[x].viewers; goto point4; } default: { break; } } } int main() { setlocale(LC_ALL, "Russian"); table tablica; tablica.LinesCount = 0; int choose; int n = 0; while (true) { label: cout << "1)Загрузить таблицу из файла" << endl; cout << "2)Ввести данные" << endl; cout << "3)Сохранить таблицу в файл" << endl; cout << "4)Просмотр таблицы" << endl; cout << "5)Сортировка" << endl; cout << "6)Поиск в таблице элемента с заданным значением поля или с наиболее близким к нему по значению" << endl; cout << "7)Удаление записи" << endl; cout << "8)Изменение (редактирование) записи" << endl; cout << "9)Наиболее кассовый сеанс" << endl; cout << "0)Выход" << endl; cin >> choose; switch (choose) { case 0: {goto labelend; } case 1: {load(tablica); goto label; } case 2: {write_data(tablica); goto label; } case 3: {write_to_file(tablica); goto label; } case 4: {show(tablica); goto label; } case 5: {goto label5; } case 6: {goto label6; } case 7: {delete_line(tablica); goto label; } case 8: {edit(tablica); goto label; } case 9: {mostProfitableSession(tablica); goto label; } while (true) { label5: int menu; cout << "0)Выход" << endl; cout << "1)Сортировка по имени" << endl; cout << "2)Сортировка по сеансу" << endl; cout << "3)Сортировка по стоимости" << endl; cout << "4)Сортировка по количеству зрителей" << endl; cin >> menu; switch (menu) { case 0: {goto label; } case 1: {sortname(tablica); goto label5; } case 2: {sortsession(tablica); goto label5; } case 3: {sortprice(tablica); goto label5; } case 4: {sortviewers(tablica); goto label5; } } } while (true) { label6: char menu; cout << "0)Выход" << endl; cout << "1)Поиск по названию" << endl; cout << "2)Поиск по сеансу" << endl; cout << "3)Поиск по стоимости" << endl; cout << "4)Поиск по количеству зрителей" << endl; cin >> menu; switch (menu) { case '0': {goto label; } case '1': {findName(tablica); goto label6; } case '2': {findSession(tablica); goto label6; } case '3': {findPrice(tablica); goto label6; } case '4': {findCountViewers(tablica); goto label6; } } } } } labelend: return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines