Quick actions

cmd+k|ctrl+k

Navigation

Languages

LangProgLaba2_Lexa

Snippet info

Language

Cpp

Visibility

public

Author

homiecage

Created

2017-03-20T09:05:17Z

Updated

2017-03-20T09:05:17Z

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>

#define N 25
using namespace std;

struct Data {
	int time;
	int day, month, year;
};

struct person {
	char name[N];
	int count;
	Data pereg[N];
};

struct table {
	person tbl[N];
};

void chtenie(table &first, int n) {							//Считывание данных из файла
	ifstream file("C:\\Users\\User\\Desktop\\table.txt");
	for (int i = 0; i < n; i++) {
		file >> first.tbl[i].name;
		file >> first.tbl[i].count;
		for (int j = 0; first.tbl[i].count > j; j++) {
			file >> first.tbl[i].pereg[j].time;
			file >> first.tbl[i].pereg[j].day;
			file >> first.tbl[i].pereg[j].month;
			file >> first.tbl[i].pereg[j].year;
		}
	}
	file.close();
}			

int kolstr() {											//Количество строк
	FILE *f;
	f = fopen("C:\\Users\\User\\Desktop\\table.txt", "r");
	int x = 1;
	char c;
	while ((c = fgetc(f)) != EOF) {
		if (c == '\n') x++;
	}
	fclose(f);
	return x;
}


void zapis(table &first, int n) {											//Запись данных в файл
	ofstream file("C:\\Users\\User\\Desktop\\table.txt");	
	for (int i = 0; i<n; i++) {
		file << first.tbl[i].name << " ";
		file << first.tbl[i].count << " ";
		for (int j = 0; first.tbl[i].count > j; j++) {
			file << first.tbl[i].pereg[j].time << " ";
			file << first.tbl[i].pereg[j].day << " ";
			file << first.tbl[i].pereg[j].month << " ";
			file << first.tbl[i].pereg[j].year << " ";
		}
		file << endl;
	}
	file.close();
}

int vvod(table &first, int n) {							//Ввод с клавиатуры
	int k;
	cout << "Введите количество записей" << endl;
	cin >> k;
	for (int i = n; i < k+n; i++) {
		cout << "Введите фамилию" << endl;
		cin >> first.tbl[i].name;
		cout << "Введите число переговоров" << endl;
		cin >> first.tbl[i].count;
		cout << "Введите продолжительность и дату для каждого разговора" << endl;
		for (int j = 0; first.tbl[i].count > j; j++) {
			cout << "Продолжительнось" << endl;
			cin >> first.tbl[i].pereg[j].time;
			cout << "День" << endl;
			cin >> first.tbl[i].pereg[j].day;
			cout << "Месяц" << endl;
			cin >> first.tbl[i].pereg[j].month;
			cout << "Год" << endl;
			cin >> first.tbl[i].pereg[j].year;
		}
	}
        return n+k;
 }

void vivod(table &first, int n) {				//Вывод на экран
	for (int i = 0; i < n; i++) {
		cout << first.tbl[i].name << " " << first.tbl[i].count << endl;
		for (int j = 0; j < first.tbl[i].count; j++) {
			cout << "\t" << j + 1 << ") " << first.tbl[i].pereg[j].time << " " << first.tbl[i].pereg[j].day << "." << first.tbl[i].pereg[j].month << "." << first.tbl[i].pereg[j].year << endl;
		}
	}
}

void sortfam(table &first, int n) {						//Сортировка по фамилии
	for (int i = 1; i < n; i++) {
		if (strcmp(first.tbl[i].name, first.tbl[i-1].name) < 0) {
			swap(first.tbl[i - 1], first.tbl[i]);
			i--;
		}
	}
}

void sortpereg(table &first, int n) {
	for (int i = 1; i < n; i++) {						//Сортировка по количеству переговоров
		if (first.tbl[i].count < first.tbl[i - 1].count) {
			swap(first.tbl[i - 1], first.tbl[i]);
			i--;
		}
	}
}

void sorttime(table &first, int n) {
	for (int i = 0; i < n; i++) {						//Сортировка по длительности переговоров
		for (int j = 1; j < first.tbl[i].count; j++) {
			if (first.tbl[i].pereg[j].time < first.tbl[i].pereg[j - 1].time) {
				swap(first.tbl[i].pereg[j - 1], first.tbl[i].pereg[j]);
				i--;
			}
		}
	}
}

void sortdate(table &first, int n) {
	for (int i = 0; i < n; i++) {						//сортировка по дате
		for (int j = 1; j < first.tbl[i].count; j++) {
			if (first.tbl[i].pereg[j].year == first.tbl[i].pereg[j - 1].year) {
				if (first.tbl[i].pereg[j].month == first.tbl[i].pereg[j - 1].month) {
					if (first.tbl[i].pereg[j].day < first.tbl[i].pereg[j - 1].day) {
						swap(first.tbl[i].pereg[j - 1], first.tbl[i].pereg[j]);
						j--;
					}
				}
				else {
					if (first.tbl[i].pereg[j].month < first.tbl[i].pereg[j - 1].month) {
						swap(first.tbl[i].pereg[j - 1], first.tbl[i].pereg[j]);
						j--;
					}

				}
			}
			else {
				if (first.tbl[i].pereg[j].year < first.tbl[i].pereg[j - 1].year) {
					swap(first.tbl[i].pereg[j - 1], first.tbl[i].pereg[j]);
					j--;
				}
			}
		}
	}
}

void poiskfam(table &first, int n) {									//Поиск по фамилии

	int i, j, maxs=0, s=0, minr=1000, r=0;
	int otv;
	char B[25];
	cin >> B;
	for (i = 0; i < n; i++) {
		for (j = 0; B[j]!=NULL && r==0; j++) {
			if (first.tbl[i].name[j] == B[j]) {
				s++;
			}
			else {
				r = r + abs(first.tbl[i].name[j] - B[j]);
			}
		}
		if (s > maxs) {
			otv = i;
			maxs = s;
			minr = r;
		}
		if (s == maxs) {
			if (r < minr) {
				otv = i;
				minr = r;
			}
		}
		s = 0;
		r = 0;
	}
	cout << first.tbl[otv].name << " " << first.tbl[otv].count << endl;
	for (int j = 0; j < first.tbl[otv].count; j++) {
		cout << "\t" << j + 1 << ") " << first.tbl[otv].pereg[j].time << " " << first.tbl[otv].pereg[j].day << "." << first.tbl[otv].pereg[j].month << "." << first.tbl[otv].pereg[j].year << endl;
	}
}

void poiskpereg(table &first, int n) {
	int a;							//Поиск по количеству переговоров
	int min = 100;
	int otv;
	cout << "Количество:" << endl;
	cin >> a;
	int i;
	for (i = 0; i < n; i++) {
		if (min > abs(a - first.tbl[i].count)) {
			min = abs(a - first.tbl[i].count);
			otv = i;
		}
	}
	cout << first.tbl[otv].name << " " << first.tbl[otv].count << endl;
	for (int j = 0; j < first.tbl[otv].count; j++) {
		cout << "\t" << j + 1 << ") " << first.tbl[otv].pereg[j].time << " " << first.tbl[otv].pereg[j].day << "." << first.tbl[otv].pereg[j].month << "." << first.tbl[otv].pereg[j].year << endl;
	}

}

void poisktime(table &first, int n) {
	int b;										//Поиск по длительности
	int i, j;
	int min = 40;
	int otvi;
	int otvj;
	cout << "Длительность:" << endl;
	cin >> b;
	for (i = 0; i < n; i++) {
		for (j = 0; j < first.tbl[i].count; j++) {
			if (min > abs(b - first.tbl[i].pereg[j].time)) {
				min = abs(b - first.tbl[i].pereg[j].time);
				otvi = i;
				otvj = j;
			}
		}
	}
	cout << first.tbl[otvi].name << " " << first.tbl[otvi].count << endl;
	cout << "\t" << otvj + 1 << ") " << first.tbl[otvi].pereg[otvj].time << " " << first.tbl[otvi].pereg[otvj].day << "." << first.tbl[otvi].pereg[otvj].month << "." << first.tbl[otvi].pereg[otvj].year << endl;
}

void poiskdate(table &first, int n) {					//Поиск даты
	int i, j, d, m, y, min = 10000;
	int otvi;
	int otvj;
	cout << "День" << endl;
	cin >> d;
	cout << "Месяц" << endl;
	cin >> m;
	cout << "Год" << endl;
	cin >> y;
	for (i = 0; i < n; i++) {
		for (j = 0; j < first.tbl[i].count; j++) {
			if (min > abs((first.tbl[i].pereg[j].year * 365 + first.tbl[i].pereg[j].month * 30 + first.tbl[i].pereg[j].day) - (y * 365 + m * 30 + d))) {
				min = abs((first.tbl[i].pereg[j].year * 365 + first.tbl[i].pereg[j].month * 30 + first.tbl[i].pereg[j].day) - (y * 365 + m * 30 + d));
				otvi = i;
				otvj = j;
			}
		}
	}
	cout << first.tbl[otvi].name << " " << first.tbl[otvi].count << endl;
	cout << "\t" << otvj + 1 << ") " << first.tbl[otvi].pereg[otvj].time << " " << first.tbl[otvi].pereg[otvj].day << "." << first.tbl[otvi].pereg[otvj].month << "." << first.tbl[otvi].pereg[otvj].year << endl;

}

int udal(table &first, int n) {
	int nom;									//Удаление записи
	cout << "Введите номер строки, которую хотите удалить" << endl;
	cin >> nom;
	for (int i = nom-1; i < n-1; i++) {
		first.tbl[i] = first.tbl[i + 1];
	}
        return n--;
}

void izmen(table &first) {
	int x, y, z;						//Изменение записи
	cout << "Введите номер строки, которую хотите изменить" << endl;
	cin >> x;
	x--;
	cout << "Что хотите изменить? 1 - Фамилию, 2 - количество переговоров, 3 - продолжительность, 4 - день, 5 - месяц, 6 - год"<< endl;
	cin >> y;
	if (y == 1) {
		cout << "Введите фамилию" << endl;
		cin >> first.tbl[x].name;
	}
	if (y == 2) {
		cout << "Введите число переговоров" << endl;
		cin >> first.tbl[x].count;
	}
	if (y > 2) {
		cout << "Введите номер переговора, который хотите изменить" << endl;
		cin >> z;
		z--;
		if (y == 3) {
			cout << "Введите продолжительнось" << endl;
			cin >> first.tbl[x].pereg[z].time;
		}
		if (y == 4) {
			cout << "Введите день" << endl;
			cin >> first.tbl[x].pereg[z].day;
		}
		if (y == 5) {
			cout << "Введите месяц" << endl;
			cin >> first.tbl[x].pereg[z].month;
		}
		if (y == 6) {
			cout << "Введите год" << endl;
			cin >> first.tbl[x].pereg[z].year;
		}
	}
}

void formula(table &first, int n) {							//Длительность самого долгого переговора
	int s=0;
	int otvi;
	int otvj;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < first.tbl[i].count; j++) {
			if (first.tbl[i].pereg[j].time > s) {
				s = first.tbl[i].pereg[j].time;
				otvi = i;
				otvj = j;
			}
		}
	}
	cout << first.tbl[otvi].name << " " << first.tbl[otvi].count << endl;
	cout << "\t" << otvj + 1 << ") " << first.tbl[otvi].pereg[otvj].time << " " << first.tbl[otvi].pereg[otvj].day << "." << first.tbl[otvi].pereg[otvj].month << "." << first.tbl[otvi].pereg[otvj].year << endl;
}

int main() {
	setlocale(LC_ALL, "rus");
	table first;
	int n;
	n = kolstr();
        point:;
	int menu;
	cout << "Меню: " << endl;
	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 >> menu;
		switch (menu) {
		case 0: {
			break;
		}
		case 1: {
			chtenie(first, n);
			goto point;
		}
		case 2: {
			zapis(first, n);
			goto point;
		}
		case 3: {
			n=vvod(first, n);
			goto point;
		}
		case 4: {
			vivod(first, n);
			goto point;
		}
		case 5: {
			int menu2;
			point2:;
			cout << "1. По фамилии" << endl;
			cout << "2. По количеству переговоров" << endl;
			cout << "3. По длительности переговора" << endl;
			cout << "4. По дате" << endl;
			cout << "0. выход в предыдущее меню" << endl;
			cin >> menu2;

			switch (menu2) {
			case 0: {
				goto point;
			}
			case 1: {
				poiskfam(first, n);
				goto point2;
			}
			case 2: {
				poiskpereg(first, n);
				goto point2;
			}
			case 3: {
				poisktime(first, n);
				goto point2;
			}
			case 4: {
				poiskdate(first, n);
				goto point2;
			}
			}
		}
		case 6: {
			n=udal(first, n);
			goto point;
		}
		case 7: {
			izmen(first);
			goto point;
		}
		case 8: {
			int menu3;
			point3:;
			cout << "1. По фамилии" << endl;
			cout << "2. По количеству переговоров" << endl;
			cout << "3. По длительности переговора" << endl;
			cout << "4. По дате" << endl;
			cout << "0. выход в предыдущее меню" << endl;
			cin >> menu3;

			switch (menu3) {
			case 0: {
				goto point;
			}
			case 1: {
				sortfam(first, n);
				goto point3;
			}
			case 2: {
				sortpereg(first, n);
				goto point3;
			}
			case 3: {
				sorttime(first, n);
				goto point3;
			}
			case 4: {
				sortdate(first, n);
				goto point3;
			}
			}
		}
		case 9: {
			formula(first, n);
			goto point;
		}
		}


	
}
INFO