avatar
Untitled

Guest 774 20th May, 2023

#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;

// Hàm để đọc danh sách nhân viên từ tệp
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;
}

// Hàm để tính lương trung bình của từng phòng
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);
    }

    // Tìm phòng có lương trung bình cao nhất
    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);
}

// Hàm để so sánh lương của hai nhân viên
int compareSalary(const void* a, const void* b) {
    Employee* empA = (Employee*)a;
    Employee* empB = (Employee*)b;
    return (empA->LUONG - empB->LUONG);
}

// Hàm để in danh sách nhân viên theo thứ tự tăng dần của lương
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);
    }
}

// Hàm để ghi kết quả vào tệp output.txt
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);
}

// Hàm để giải phóng bộ nhớ đã cấp phát cho danh sách nhân viên
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;
}
Description

No description

To share this paste please copy this url and send to your friends
RAW Paste Data