#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include /*формат файла 11 Арефин A 200 Асепков A 200 Барановский A 300 Демидовец B 500 Елькин B 700 Заяц A 500 Иванникова D 400 Иванов A 400 Калитько C 200 Карась B 400 Кирлица A 450 */ /* employee struct start */ typedef struct { char surname[20]; char department[2]; int salary; } employee; employee* getEmployeeList(char* filePath) { FILE* file; file = fopen(filePath, "r"); int size; fscanf(file, "%d", &size); employee* list = (employee*)malloc(size * sizeof(employee)); for (int i = 0; i < size; i++) { fscanf(file, "%s", &list[i].surname); fscanf(file, "%s", &list[i].department); fscanf(file, "%d", &list[i].salary); } fclose(file); return list; } void addEmployee(int size, employee* list) { size++; list = (employee*)realloc(list, size * sizeof(employee)); printf("\nВведите фамилию работника: "); scanf("%s", &list[size - 1].surname); printf("\nВведите отдел работника: "); scanf("%s", &list[size - 1].department); printf("\nВведите зарплату работника: "); scanf("%d", &list[size - 1].salary); } void changeEmployee(employee* list) { int employeeToChange; printf("\nВыберете необходимого для изменения работника по номеру из списка: "); scanf("%d", &employeeToChange); printf("Введите новую фамилию работника: "); scanf("%s", &list[employeeToChange - 1].surname); printf("Введите новую отдел работника: "); scanf("%s", &list[employeeToChange - 1].department); printf("Введите новую зарплату работника: "); scanf("%d", &list[employeeToChange - 1].salary); } void deleteEmployee(int size, employee* list) { int employeeToDelete; printf("\nВыберете необходимого для удаления работника по номеру из списка: "); scanf("%d", &employeeToDelete); for (int i = employeeToDelete - 1; i < size; i++) memmove(&list[i], &list[i + 1], 26); //26 - размер одной записи типа employee size--; list = (employee*)realloc(list, size * sizeof(employee)); } void printEmployeeList(int size, employee* list) { printf("\n\tКоличество работников: %d\n", size); printf("\tВывод списка всех работников:\n"); for (int i = 0; i < size; i++) { printf("%d. %s из отдела %s - зарплата равна %d $\n", i + 1, list[i].surname, list[i].department, list[i].salary); } } int calculateAllPayments(int size, employee* list) { int sum = 0; for (int i = 0; i < size; i++) { if (strcmp(list[i].department, "A") == 0) sum += list[i].salary; } return sum; } float calculateAvarageSalary(int size, int paymentsSum, employee* list) { int numOfDepartmentEmployees = 0; for (int i = 0; i < size; i++) { if (strcmp(list[i].department, "A") == 0) numOfDepartmentEmployees++; } float averageSalary = 0; averageSalary = paymentsSum / numOfDepartmentEmployees; return averageSalary; } void printPaymentsAndSalary(int paymentsSum, float averageSalary) { printf("Общая сумма выплат отдела А: %d\nСредняя заработная плата сотрудника отдела А: %.2f$", paymentsSum, averageSalary); } /* employee struct end */ /* file funcs/procs start */ char* getPathToFile() { static char filePath[20]; printf("\nВведите путь к файлу: "); gets_s(filePath, 20); return filePath; } int getSize(char filePath[20]) { FILE* file; file = fopen(filePath, "r"); int temp; fscanf(file, "%d", &temp); fclose(file); return temp; } void endWork(char* filePath, int size, employee* list) { FILE* file = fopen(filePath, "w"); fprintf(file, "%d\n", size); for (int i = 0; i < size; i++) { fprintf(file, "%s ", &list[i].surname); fprintf(file, "%s ", &list[i].department); fprintf(file, "%d\n", list[i].salary); } fclose(file); printf("\n\t\t\t\t\t\tРабота с файлом успешно закончена.\n"); } /* file funcs/procs end */ /* print procs/funcs start */ void printTaskInfo() { printf("Данная программа получает файл со сведениями о месячной заработной плате сотрудников отдела.\n"); printf("Затем вычисляет общую сумму выплат за месяц по отделу А,а также среднемесячный заработок сотрудникам этого отдела.\n"); } int getChoice() { int choice; printf("\nВыберете опцию меню: "); scanf_s("%d", &choice); return choice; } void printMenuOptions() { printf("\t\t\t\t\t\tМеню работы с файлом предлагает следующие опции: \n"); printf("\t\t\t\t\t\t\t 1. Добавить запись о работник\n"); printf("\t\t\t\t\t\t\t 2. Изменить запись о работнике\n"); printf("\t\t\t\t\t\t\t 3. Удалить запись о работнике\n"); printf("\t\t\t\t\t\t\t 4. Вывести список работников\n"); printf("\t\t\t\t\t\t\t 5. Посчитать общие выплаты и среднюю з/п.\n"); printf("\t\t\t\t\t\t\t 6. Закончить работу с файлом\n"); } void printErrMessage() { printf("Введено неверное значение! Выберете одну из существующих опций.\n"); } /* print procs/funcs end */ int main() { setlocale(LC_ALL, "Rus"); printTaskInfo(); char* filePath = getPathToFile(); int size = getSize(filePath); employee* list = getEmployeeList(filePath); printMenuOptions(); int choice = 0; while (choice != 6) { choice = getChoice(); switch (choice) { case 1: //добавить запись { addEmployee(size, list); break; } case 2: //изменить запись { changeEmployee(list); break; } case 3: //удалить запись { deleteEmployee(size, list); break; } case 4: //вывести текущий список { printEmployeeList(size, list); break; } case 5: //посчитать общие выплаты и среднюю зп { int allPayments = calculateAllPayments(size, list); float averageSalary = calculateAvarageSalary(size, allPayments, list); printPaymentsAndSalary(allPayments, averageSalary); break; } case 6: //закончить работу { endWork(filePath, size, list); break; } default: { printErrMessage(); } } } free(list); return 0; }