#include #include #include #define MAX_LENGTH 100 #define MAX_EMPLOYEES 100 typedef struct { char MNV[MAX_LENGTH]; char HT[MAX_LENGTH]; float LUONG; char PHONG[MAX_LENGTH]; } Employee; // Hàm để tính lương trung bình của từng phòng void calculateAverageSalary(Employee employees[], int numEmployees) { float totalSalaryA = 0.0, totalSalaryB = 0.0, totalSalaryC = 0.0; int countA = 0, countB = 0, countC = 0; for (int i = 0; i < numEmployees; i++) { if (strcmp(employees[i].PHONG, "A") == 0) { totalSalaryA += employees[i].LUONG; countA++; } else if (strcmp(employees[i].PHONG, "B") == 0) { totalSalaryB += employees[i].LUONG; countB++; } else if (strcmp(employees[i].PHONG, "C") == 0) { totalSalaryC += employees[i].LUONG; countC++; } } float avgSalaryA = (countA > 0) ? totalSalaryA / countA : 0.0; float avgSalaryB = (countB > 0) ? totalSalaryB / countB : 0.0; float avgSalaryC = (countC > 0) ? totalSalaryC / countC : 0.0; printf("Lương trung bình của phòng A: %.2f\n", avgSalaryA); printf("Lương trung bình của phòng B: %.2f\n", avgSalaryB); printf("Lương trung bình của phòng C: %.2f\n", avgSalaryC); if (avgSalaryA > avgSalaryB && avgSalaryA > avgSalaryC) { printf("Phòng có lương trung bình cao nhất: A\n"); } else if (avgSalaryB > avgSalaryA && avgSalaryB > avgSalaryC) { printf("Phòng có lương trung bình cao nhất: B\n"); } else if (avgSalaryC > avgSalaryA && avgSalaryC > avgSalaryB) { printf("Phòng có lương trung bình cao nhất: C\n"); } else { printf("Có nhiều phòng có cùng lương trung bình cao nhất\n"); } } // Hàm để so sánh hai nhân viên theo lương (sắp xếp tăng dần) int compareBySalary(const void* a, const void* b) { const Employee* emp1 = (const Employee*)a; const Employee* emp2 = (const Employee*)b; if (emp1->LUONG < emp2->LUONG) return -1; if (emp1->LUONG > emp2->LUONG) return 1; return 0; } // Hàm để in danh sách nhân viên void printEmployees(Employee employees[], int numEmployees) { for (int i = 0; i < numEmployees; i++) { printf("Mã nhân viên: %s\n", employees[i].MNV); printf("Họ tên: %s\n", employees[i].HT); printf("Lương: %.2f\n", employees[i].LUONG); printf("Phòng: %s\n\n", employees[i].PHONG); } } int main() { FILE* inputFile = fopen("input.txt", "r"); FILE* outputFile = fopen("output.txt", "w"); if (inputFile == NULL) { printf("Không thể mở tệp input.txt\n"); return 1; } if (outputFile == NULL) { printf("Không thể mở tệp output.txt\n"); return 1; } int numEmployees; fscanf(inputFile, "%d", &numEmployees); Employee employees[MAX_EMPLOYEES]; for (int i = 0; i < numEmployees; i++) { fscanf(inputFile, "%s", employees[i].MNV); fscanf(inputFile, "%s", employees[i].HT); fscanf(inputFile, "%f", &employees[i].LUONG); fscanf(inputFile, "%s", employees[i].PHONG); } calculateAverageSalary(employees, numEmployees); qsort(employees, numEmployees, sizeof(Employee), compareBySalary); printf("Danh sách nhân viên theo thứ tự tăng dần của lương:\n"); printEmployees(employees, numEmployees); for (int i = 0; i < numEmployees; i++) { fprintf(outputFile, "Mã nhân viên: %s\n", employees[i].MNV); fprintf(outputFile, "Họ tên: %s\n", employees[i].HT); fprintf(outputFile, "Lương: %.2f\n", employees[i].LUONG); fprintf(outputFile, "Phòng: %s\n\n", employees[i].PHONG); } fclose(inputFile); fclose(outputFile); return 0; }