#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
typedef struct {
int MNV;
char HT[MAX_LENGTH];
float LUONG;
char PHONG[MAX_LENGTH];
} Employee;
Employee* readEmployeeListFromFile(const char* filename, int* numEmployees) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Không thể mở tệp %s\n", filename);
return NULL;
}
fscanf(file, "%d", numEmployees);
Employee* employees = (Employee*)malloc(*numEmployees * sizeof(Employee));
for (int i = 0; i < *numEmployees; i++) {
fscanf(file, "%d %s %f %s", &employees[i].MNV, employees[i].HT, &employees[i].LUONG, employees[i].PHONG);
}
fclose(file);
return employees;
}
void calculateAverageSalaryByDepartment(Employee* employees, int numEmployees) {
float sumA = 0, sumB = 0, sumC = 0;
int countA = 0, countB = 0, countC = 0;
for (int i = 0; i < numEmployees; i++) {
if (strcmp(employees[i].PHONG, "A") == 0) {
sumA += employees[i].LUONG;
countA++;
} else if (strcmp(employees[i].PHONG, "B") == 0) {
sumB += employees[i].LUONG;
countB++;
} else if (strcmp(employees[i].PHONG, "C") == 0) {
sumC += employees[i].LUONG;
countC++;
}
}
if (countA > 0) {
float avgA = sumA / countA;
printf("Phòng A: Lương trung bình = %.2f\n", avgA);
}
if (countB > 0) {
float avgB = sumB / countB;
printf("Phòng B: Lương trung bình = %.2f\n", avgB);
}
if (countC > 0) {
float avgC = sumC / countC;
printf("Phòng C: Lương trung bình = %.2f\n", avgC);
}
float maxAvg = -1;
char maxDept[MAX_LENGTH];
if (countA > 0 && sumA / countA > maxAvg) {
maxAvg = sumA / countA;
strcpy(maxDept, "A");
}
if (countB > 0 && sumB / countB > maxAvg) {
maxAvg = sumB / countB;
strcpy(maxDept, "B");
}
if (countC > 0 && sumC / countC > maxAvg) {
maxAvg = sumC / countC;
strcpy(maxDept, "C");
}
printf("Phòng có lương trung bình cao nhất: %s\n", maxDept);
}
int compareSalary(const void* a, const void* b) {
Employee* empA = (Employee*)a;
Employee* empB = (Employee*)b;
return (empA->LUONG - empB->LUONG);
}
void printEmployeeList(Employee* employees, int numEmployees) {
qsort(employees, numEmployees, sizeof(Employee), compareSalary);
printf("Danh sách nhân viên theo thứ tự tăng dần của lương:\n");
for (int i = 0; i < numEmployees; i++) {
printf("MNV: %d, Họ tên: %s, Lương: %.2f, Phòng: %s\n", employees[i].MNV, employees[i].HT, employees[i].LUONG, employees[i].PHONG);
}
}
void writeResultToFile(Employee* employees, int numEmployees) {
FILE* file = fopen("output.txt", "w");
if (file == NULL) {
printf("Không thể tạo hoặc mở tệp output.txt\n");
return;
}
fprintf(file, "%d\n", numEmployees);
for (int i = 0; i < numEmployees; i++) {
fprintf(file, "%d %s %.2f %s\n", employees[i].MNV, employees[i].HT, employees[i].LUONG, employees[i].PHONG);
}
fclose(file);
}
void freeEmployeeList(Employee* employees) {
free(employees);
}
int main() {
int numEmployees;
Employee* employees = readEmployeeListFromFile("input.txt", &numEmployees);
if (employees == NULL) {
return 0;
}
calculateAverageSalaryByDepartment(employees, numEmployees);
printEmployeeList(employees, numEmployees);
writeResultToFile(employees, numEmployees);
freeEmployeeList(employees);
return 0;
}