物业费管理系统(C语言程序设计课题设计)

        完成小区物业费用管理系统设计。新住户信息包括:户主姓名、性别、联系电话、楼号、房号、平米数、每平米物业价格、应缴纳物业费,备注信息。

        实现以下菜单显示的功能:

        1、新住户信息的添加,数据存入文件中。

        2、修改住户信息的功能。

        3、删除住户信息的功能。

        4、查询住户信息。

               根据户主姓名

                根据楼号查询

        5、应缴物业费自动生成。每月1号,自动生成本月份的物业费。如果该住户之前的物业费未交清, 则本月物业费与之前拖欠费用进行累加,为该用户应缴纳的物业费。

        6、缴费功能。根据用户缴纳金额,修改“应缴纳物业费”  。

        7、统计功能:能够按楼号分类统计所有未交清物业费的记录。能够按拖欠款项多少,对所有用户信息进行从大到小排序。

代码实现如下:(已添加添加账户密码系统)

#define N 100//定义列表最大人数 
#define MAX_NAME_LENGTH 20//定义姓名最大长度 
#define MAX_GENDER_LENGTH 6//定义性别最大长度 
#define MAX_PHONE_LENGTH 20//定义号码最大长度 
#define MAX_REMARKS_LENGTH 50//定义备注最大长度 
#define MAX_USERNAME_LENGTH 20//定义账号最大长度
#define MAX_PASSWORD_LENGTH 20//定义密码最大长度
//结构体 
typedef struct date {
	int year; 
	int month;
	int day; 
} DATE;

typedef struct RESIDENTident {
    char name[MAX_NAME_LENGTH];       // 户主姓名
    char gender[MAX_GENDER_LENGTH];     // 性别
    char phone[MAX_PHONE_LENGTH];      // 联系电话
    int building;        // 楼号
    int room;            // 房号
    float area;          // 面积平米数
    float unitPrice;     // 每平米物业价格
    float propertyFee;   // 应缴纳物业费
    float balance;       //用户余额 
    char remarks[MAX_REMARKS_LENGTH];    // 备注
} RESIDENT;

typedef struct {
    char username[MAX_USERNAME_LENGTH];
    char password[MAX_PASSWORD_LENGTH];
} ACCOUNT;
//函数的声明 
void printMenu_1();
void printMenu_2();
void chooseMenu_1(int option);
void chooseMenu_2(int option);
void addInfo();
void modifyInfo();
void deleteInfo();
void searchInfoByBuilding();
void searchInfoByName();
void calculateCost();
void pay();
void statistic();
void registerAccount();
void validateAccount();

RESIDENT residentList[N];
ACCOUNT accounts[N];
int option = 0;
//主函数
int main() {
    printMenu_1();
    do{
        chooseMenu_1(option);
        if (option != 0) {
            printMenu_1();
        }
    } while (option != 0);
}
//打印菜单
void printMenu_1() {
    system("cls");
    printf("----------------------------------------\n");
    printf("\x1b[32m*****--欢迎使用物业费管理系统--******\n\x1b[0m");
    printf("* 1. 注册新管理员账号 *\n");
    printf("* 2. 登录管理员账号   *\n");
    printf("* 0. 退出             *\n");
    printf("----------------------------------------\n");
    printf("\x1b[31m提示:请输入数字序号选择对应的操作!\n\x1b[0m"); 
    printf("请输入你的选择:");
}
//打印菜单
void printMenu_2() {
    system("cls");
    printf("\x1b[32m***********--物业费管理系统--***********\n\x1b[0m");  // 字体颜色修改为红色
    printf("* 1、添加住户信息                      *\n");
    printf("* 2、修改住户信息                      *\n");
    printf("* 3、删除住户信息                      *\n");
    printf("* 4、查询住户信息(根据户主姓名)      *\n");
    printf("* 5、查询住户信息(根据楼号查询)      *\n");
    printf("* 6、自动生成应缴物业费                *\n");
    printf("* 7、缴费系统                          *\n");
    printf("* 8、统计系统                          *\n");
    printf("* 0、退出程序                          *\n");
    printf("----------------------------------------\n");
    printf("\x1b[31m提示:请输入数字序号选择对应的操作!\n\x1b[0m"); 
    printf("请输入你的选择:");
}
//注册账号
void registerAccount() {
    FILE* file = fopen("admin.id", "ab");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }
    ACCOUNT temp;
    char username[MAX_USERNAME_LENGTH];
    char password[MAX_PASSWORD_LENGTH];

    printf("请输入用户名: ");
    scanf("%s", username);
    printf("请输入密码: ");
    scanf("%s", password);

    strncpy(temp.username, username, MAX_USERNAME_LENGTH);
    strncpy(temp.password, password, MAX_PASSWORD_LENGTH);
    // 将新账户信息写入文件
    fwrite(&temp, sizeof(ACCOUNT), 1, file);

    fclose(file);

    printf("注册成功!\n");
    printf("按回车键继续\n");
    getchar();
    getchar();
    printMenu_1();
    return;
}
//账户验证
void validateAccount() {  
    // 进行账户验证
    FILE* file = fopen("admin.id","rb");
    if(file == NULL){
        printf("无法打开文件");
        return;
    }
    
    char username[MAX_USERNAME_LENGTH];
    char password[MAX_PASSWORD_LENGTH];
    int flag = 0;
    int i=0;
    char choice;
    fread(accounts,sizeof(ACCOUNT),N,file);
    fclose(file);
    
    printf("请输入用户名: ");
    scanf("%s",&username);
    printf("请输入密码: ");
    scanf("%s",&password);
    
    for (int i = 0; i < N; i++) {
        if (strcmp(accounts[i].username, username) == 0 && strcmp(accounts[i].password, password) == 0) {
            printMenu_2();
        do {
            chooseMenu_2(option);
            if (option != 0) {
                printMenu_2();
                }
            } while (option != 0);
        }
    }

        while(i<10){
            printf("账户或密码错误,请检查后重新输入\n");
            printf("按y继续,n退出: ");
            scanf(" %c",&choice);
            if(choice == 'y' || choice == 'Y'){
                validateAccount();
            }
            else exit(0);
        }   
}
//选择菜单
void chooseMenu_1(int option){
    int scanfResult;

    do{
        scanfResult = scanf("%d",&option);
        while(getchar() != '\n');
        if(scanfResult != 1){
            printf("无效输入,请重新输入\n");
        }
        else{
            switch(option){
                case 1:
                    registerAccount();
                    break;
                case 2:
                    validateAccount();
                    break;
                case 0:
                    printf("程序已退出");
                    exit(0);
                default:
                    printf("输入有误,请重新输入\n");
                    break;
            }
        }
    } while (scanfResult != 1 || option != 0);
}
//选择菜单
void chooseMenu_2(int option) {
    int scanfResult;

    do {
        scanfResult = scanf("%d", &option);
        // 清除错误输入数据
        while (getchar() != '\n');
        if (scanfResult != 1) {
            printf("无效输入,请重新输入\n");
        }
        else {
            switch (option) {
                case 1:
                    addInfo();
                    break;
                case 2:
                    modifyInfo();
                    break;
                case 3:
                    deleteInfo();
                    break;
                case 4:
                    searchInfoByName();
                    break;
                case 5:
                    searchInfoByBuilding();
                    break;
                case 6:
                    calculateCost();
                    break;
                case 7:
                    pay();
                    break;
                case 8:
                    statistic();
                    break;
                case 0:
                    printf("程序已退出");
                    exit(0);
                default:
                    printf("输入有误,请重新输入\n");
                    break;
            }
        }
    } while (scanfResult != 1 || option != 0);
}
//添加信息
void addInfo() {
	residentList->balance=0.0; 
	residentList->propertyFee = 0.0;
    FILE* file = fopen("residentList.txt", "a+");//创建文件 
    if (file == NULL) {
        printf("无法创建文件。\n");
        return;
    }
    
    printf("户主姓名: ");
    scanf("%19s", residentList->name);  // 限制输入的字符数,防止缓冲区溢出
    while (getchar() != '\n');  // 清除输入缓冲区中的换行符和多余的字符

    printf("性别(man or woman): ");
    scanf("%9s", residentList->gender);
    while (getchar() != '\n');

    printf("联系电话: ");
    scanf("%19s", residentList->phone);
    while (getchar() != '\n');

    printf("楼号: ");
    while (scanf("%d", &(residentList->building)) != 1) {  // 循环直到成功读取一个整数
        printf("无效输入,请重新输入楼号: ");
        while (getchar() != '\n');
    }

    printf("房号: ");
    while (scanf("%d", &(residentList->room)) != 1) {
        printf("无效输入,请重新输入房号: ");
        while (getchar() != '\n');
    }

    printf("平米数: ");
    while (scanf("%f", &(residentList->area)) != 1) {
        printf("无效输入,请重新输入平米数: ");
        while (getchar() != '\n');
    }

    printf("每平米物业价格: ");
    while (scanf("%f", &(residentList->unitPrice)) != 1) {
        printf("无效输入,请重新输入每平米物业价格: ");
        while (getchar() != '\n');
    }

    printf("备注信息: ");
    getchar();  // 清除输入缓冲区中的换行符
    fgets(residentList->remarks, sizeof(residentList->remarks), stdin);
    residentList->remarks[strcspn(residentList->remarks, "\n")] = '\0';  //替换换行符为字符串结束符

    fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
            MAX_NAME_LENGTH, residentList->name,
			MAX_GENDER_LENGTH, residentList->gender,
            MAX_PHONE_LENGTH, residentList->phone,
			sizeof(int), residentList->building,
            sizeof(int), residentList->room,
			sizeof(float), residentList->area,
            sizeof(float), residentList->unitPrice,
			sizeof(float),residentList->propertyFee,
			sizeof(float),residentList->balance,
			MAX_REMARKS_LENGTH, residentList->remarks);

    fclose(file);
    // 询问是否继续添加信息
    printf("是否继续添加住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) {
        addInfo();  // 递归重新调用addInfo()函数
    } 
	else {
		printMenu_2();
        return;  // 结束addInfo()函数,返回到上一级调用(即主函数main())
    } 
}
//修改信息
void modifyInfo() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[MAX_NAME_LENGTH];
    int residentCount = 0;
    int flag = 0;
    RESIDENT temp;
    RESIDENT modifiedList[N];
    printf("请输入要修改的住户的户主姓名:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0'; // 替换换行符为字符串结束符

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp.name,
                  temp.gender,
                  temp.phone,
                  &(temp.building),
                  &(temp.room),
                  &(temp.area),
                  &(temp.unitPrice),
                  &(temp.propertyFee),
                  &(temp.balance)) == 9) {
        fseek(file, 1, SEEK_CUR); // 跳过空格字符

        // 读取remarks的方式更改为fgets
        fgets(temp.remarks, sizeof(temp.remarks), file);
        temp.remarks[strcspn(temp.remarks, "\n")] = '\0';

        // 匹配户主姓名
        if (strcmp(temp.name, name) == 0) {
            flag = 1;

            printf("请输入新的户主姓名: ");
            fgets(temp.name, sizeof(temp.name), stdin);
            temp.name[strcspn(temp.name, "\n")] = '\0';

            printf("请输入新的性别(man or woman): ");
            fgets(temp.gender, sizeof(temp.gender), stdin);
            temp.gender[strcspn(temp.gender, "\n")] = '\0';
            while (getchar() != '\n');
			
            printf("请输入新的联系电话: ");
            fgets(temp.phone, sizeof(temp.phone), stdin);
            temp.phone[strcspn(temp.phone, "\n")] = '\0';

            printf("请输入新的楼号: ");
            while (scanf("%d", &(temp.building)) != 1) {
                printf("无效输入,请重新输入楼号: ");
            }
            getchar();

            printf("请输入新的房号: ");
            while (scanf("%d", &(temp.room)) != 1) {
                printf("无效输入,请重新输入房号: ");
            }
            getchar();

            printf("请输入新的平米数: ");
            while (scanf("%f", &(temp.area)) != 1) {
                printf("无效输入,请重新输入平米数: ");
            }
            getchar();

            printf("请输入新的每平米物业价格: ");
            while (scanf("%f", &(temp.unitPrice)) != 1) {
                printf("无效输入,请重新输入每平米物业价格: ");
            }
            getchar();

            printf("请输入新的备注信息: ");
            fgets(temp.remarks, sizeof(temp.remarks), stdin);
            temp.remarks[strcspn(temp.remarks, "\n")] = '\0';

            printf("住户信息修改成功!\n");
        }
        modifiedList[residentCount++] = temp;
    }

    if (!flag) {
        printf("未找到匹配的住户信息。\n");
    }

    fclose(file);

    file = fopen("residentList.txt", "w");
    if (file == NULL) {
        printf("无法打开文件进行写入。\n");
    }

    for (int i = 0; i < residentCount; i++) {
        fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
                MAX_NAME_LENGTH, modifiedList[i].name,
                MAX_GENDER_LENGTH, modifiedList[i].gender,
                MAX_PHONE_LENGTH, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                MAX_REMARKS_LENGTH, modifiedList[i].remarks);
    }

    fclose(file);

    printf("是否继续修改住户信息? (1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while(getchar() != '\n');

    if (choice != 0) {
        modifyInfo();
    }
    else {
        printMenu_2();
        return;
    }
}
//删除住户信息
void deleteInfo() {
    FILE *file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[MAX_NAME_LENGTH];
    int flag = 0;
    int residentCount = 0;
    RESIDENT temp[N];
    printf("请输入要删除的住户的户主姓名:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp[residentCount].name,
                  temp[residentCount].gender,
                  temp[residentCount].phone,
                  &(temp[residentCount].building),
                  &(temp[residentCount].room),
                  &(temp[residentCount].area),
                  &(temp[residentCount].unitPrice),
                  &(temp[residentCount].propertyFee),
                  &(temp[residentCount].balance)) == 9) {
        fseek(file, 1, SEEK_CUR); 
        fgets(temp[residentCount].remarks, sizeof(temp[residentCount].remarks), file);
        temp[residentCount].remarks[strcspn(temp[residentCount].remarks, "\n")] = '\0';
        if(strcmp(temp[residentCount].name, name) == 0){
            flag = 1;
            continue;
        }
        residentCount++;
    }

    fclose(file);
    
    if (flag) 
	{
        file = fopen("residentList.txt", "w");
        if (file == NULL) {
            printf("无法打开文件进行写入。\n");
    }
        for(int i = 0;i < residentCount;i++){
            fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
                MAX_NAME_LENGTH, temp[i].name,
                MAX_GENDER_LENGTH, temp[i].gender,
                MAX_PHONE_LENGTH, temp[i].phone,
                sizeof(int), temp[i].building,
                sizeof(int), temp[i].room,
                sizeof(float), temp[i].area,
                sizeof(float), temp[i].unitPrice,
                sizeof(float), temp[i].propertyFee,
                sizeof(float), temp[i].balance,
                MAX_REMARKS_LENGTH, temp[i].remarks); 
            }
            fclose(file);
            printf("\x1b[31m住户信息已删除!\x1b[0m\n");
    }
	else 
	{
        printf("未找到匹配的住户信息。\n");
    }

    printf("是否继续删除住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        deleteInfo();  
    } 
	else 
	{
		printMenu_2();
        return;  
    } 
}
// 查询住户信息(根据姓名查询)
void searchInfoByName() {
    FILE *file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[MAX_NAME_LENGTH];
    int flag = 0;
    printf("请输入户主姓名进行查询:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    RESIDENT temp;
    char line[200]; 
    while (fgets(line, sizeof(line), file) != NULL) 
	{
        sscanf(line, "%s %s %s %d %d %f %f %f %f %[^\n]", 
        temp.name, 
        temp.gender, 
        temp.phone, 
        &(temp.building), 
        &(temp.room), 
        &(temp.area), 
        &(temp.unitPrice), 
        &(temp.propertyFee), 
        &(temp.balance),temp.remarks);

        if (strcmp(temp.name, name) == 0) 
		{
            flag = 1;
            printf("\x1b[31m查询结果:\x1b[0m\n");
            printf("户主姓名: %s\n", temp.name);
            printf("性别: %s\n", temp.gender);
            printf("联系电话: %s\n", temp.phone);
            printf("楼号: %d\n", temp.building);
            printf("房号: %d\n", temp.room);
            printf("平米数: %.2f\n", temp.area);
            printf("每平米物业价格: %.2f\n", temp.unitPrice);
            printf("应缴物业费:%.2f\n",temp.propertyFee);
            printf("余额:%.2f\n",temp.balance);
            printf("备注信息: %s\n\n", temp.remarks);
            break;
        }
    }

    if (!flag) 
	{
        printf("未找到匹配的住户信息。\n");
    }

    fclose(file);
    printf("是否继续查询住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        searchInfoByName();  
    } 
	else 
	{
        printMenu_2();
        return;  
    }
}
// 查询住户信息(根据楼号查询)
void searchInfoByBuilding() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    int building;
    printf("请输入要查询的楼号:");
    scanf("%d", &building);
    getchar();  

    RESIDENT temp;
    char line[200]; 
    int flag = 0;

    printf("\x1b[31m查询结果:\x1b[0m\n");
    printf("**********************************************************************************************************\n");
    printf("姓名       性别  电话         楼号  房号  平米数   每平米物业价格   应缴物业费   余额    备注         \n");
    while (fgets(line, sizeof(line), file) != NULL) 
	{
        sscanf(line, "%s %s %s %d %d %f %f %f %f %[^\n]", 
        temp.name, temp.gender, 
        temp.phone, &(temp.building), 
        &(temp.room), 
        &(temp.area), 
        &(temp.unitPrice), 
        &(temp.propertyFee), 
        &(temp.balance), 
        temp.remarks);
        if (temp.building == building) 
		{
            printf("%-10s %-5s %-14s %-4d %-4d %-8.2f %-16.2f %6.2f %10.2f   %s\n", 
            temp.name, 
            temp.gender, 
            temp.phone, 
            temp.building, 
            temp.room, 
            temp.area, 
            temp.unitPrice, 
            temp.propertyFee, 
            temp.balance, 
            temp.remarks);
            flag = 1;
			printf("**********************************************************************************************************\n");
        }
    }

    if (!flag)
	{
        printf("未找到匹配的住户信息。\n");
        while(getchar()!='\n');
    }

    fclose(file);

    printf("是否继续查询住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        searchInfoByBuilding();  
    } 
	else
	{
        printMenu_2();
        return; 
    }
}
// 计算费用
void calculateCost() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RESIDENT modifiedList[N];
    int residentCount = 0;
    DATE currentDate;
    char separator; // 分隔符
    // 循环获取用户输入,直到输入格式正确为止
    while (1) {
        printf("请输入当前日期,格式为YYYY-MM-DD:");
        if (scanf("%d%c%d%c%d", &currentDate.year, &separator, &currentDate.month, &separator, &currentDate.day) != 5 || separator != '-') {
            // 如果用户输入格式不正确,清空输入缓冲区,重新输入
            printf("日期格式不正确,请重新输入\n");
            while (getchar() != '\n');
            continue;
        }
        // 检查年、月、日的范围是否合法
        if (currentDate.year < 1900 || currentDate.year > 2100 || currentDate.month < 1 || currentDate.month > 12 || currentDate.day < 1 || currentDate.day > 31) {
            printf("日期范围不正确,请重新输入\n");
            continue;
        }
        // 如果输入格式和范围都符合要求,退出循环
        break;
    }
    // 判断当前日期是否为1号
    if (currentDate.day == 1) {
        while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
            modifiedList[residentCount].name,
            modifiedList[residentCount].gender,
            modifiedList[residentCount].phone,
            &(modifiedList[residentCount].building),
            &(modifiedList[residentCount].room),
            &(modifiedList[residentCount].area),
            &(modifiedList[residentCount].unitPrice),
            &(modifiedList[residentCount].propertyFee),
            &(modifiedList[residentCount].balance)) == 9) {

            fseek(file, 1, SEEK_CUR);

            fgets(modifiedList[residentCount].remarks, sizeof(modifiedList[residentCount].remarks), file);
            modifiedList[residentCount].remarks[strcspn(modifiedList[residentCount].remarks, "\n")] = '\0';
            residentCount++;
        }

        for (int i = 0; i < residentCount; i++) {
            float previousArrears = modifiedList[i].propertyFee;
            // 检查是否存在拖欠费用
            if (previousArrears > 0) {
                // 计算本月物业费(假设为每平米物业价格乘以平米数)
                float currentPropertyFee = modifiedList[i].unitPrice * modifiedList[i].area;
                // 累加拖欠费用和本月物业费
                modifiedList[i].propertyFee = previousArrears + currentPropertyFee;
            }
            else {
                // 计算本月物业费(假设为每平米物业价格乘以平米数)
                modifiedList[i].propertyFee = modifiedList[i].unitPrice * modifiedList[i].area;
            }
        }
            fclose(file);

            file = fopen("residentList.txt", "w");
            if (file == NULL) {
                printf("无法打开文件进行写入。\n");
            }
            for(int i = 0;i < residentCount;i++){
                fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s\n",
                MAX_NAME_LENGTH, modifiedList[i].name,
                MAX_GENDER_LENGTH, modifiedList[i].gender,
                MAX_PHONE_LENGTH, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                MAX_REMARKS_LENGTH, modifiedList[i].remarks);
            }
        
        fclose(file);
        printf("本月物业费已自动生成!\n");
    }
    else {
        printf("当前日期不是每月1号,无需生成物业费。\n");
    }

    printf("按回车返回主菜单\n");
    getchar(); // 清除回车
    getchar(); // 延迟
    printMenu_2();
}
// 缴费
void pay() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RESIDENT modifiedList[N];
    char name[MAX_NAME_LENGTH];
    int residentCount = 0;
    int flag = 0;

    printf("请输入要缴纳物业费的居民姓名:");
    scanf("%s", name);

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
            modifiedList[residentCount].name,
            modifiedList[residentCount].gender,
            modifiedList[residentCount].phone,
            &(modifiedList[residentCount].building),
            &(modifiedList[residentCount].room),
            &(modifiedList[residentCount].area),
            &(modifiedList[residentCount].unitPrice),
            &(modifiedList[residentCount].propertyFee),
            &(modifiedList[residentCount].balance)) == 9) {

            fseek(file, 1, SEEK_CUR);

            fgets(modifiedList[residentCount].remarks, sizeof(modifiedList[residentCount].remarks), file);
            modifiedList[residentCount].remarks[strcspn(modifiedList[residentCount].remarks, "\n")] = '\0';
        // 查找匹配的居民姓名
        if (strcmp(modifiedList[residentCount].name, name) == 0) {
            flag = 1;  
            // 显示用户应缴费用和余额
            printf("应缴费用:%.2f\n", modifiedList[residentCount].propertyFee);
            printf("余额:%.2f\n", modifiedList[residentCount].balance);
            float paymentAmount;
            printf("请输入要缴纳的物业费金额:");
            scanf("%f", &paymentAmount);
            // 检查缴费金额是否合法
            if (paymentAmount < 0) {
                printf("缴费金额不能为负数。\n");
                fclose(file);
                return;
            }
            // 更新用户余额和拖欠费用
            modifiedList[residentCount].balance += paymentAmount;
            modifiedList[residentCount].balance -= modifiedList[residentCount].propertyFee;
            modifiedList[residentCount].propertyFee = 0;

            // 打印缴费信息
            printf("姓名:%s\n", modifiedList[residentCount].name);
            printf("缴费金额:%.2f\n", paymentAmount);
            printf("更新后余额:%.2f\n\n", modifiedList[residentCount].balance);
        }
        residentCount++;
    }

    fclose(file);
    // 如果未找到住户,输出未找到匹配的住户信息
    if (!flag) {
        printf("未找到匹配的住户。\n");
    }

    else{
        file = fopen("residentList.txt", "w");
        if (file == NULL) {
            printf("无法打开文件进行写入。\n");
        }

        for(int i = 0;i < residentCount;i++){
                fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s\n",
                MAX_NAME_LENGTH, modifiedList[i].name,
                MAX_GENDER_LENGTH, modifiedList[i].gender,
                MAX_PHONE_LENGTH, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                MAX_REMARKS_LENGTH, modifiedList[i].remarks);
            }

        fclose(file);

    }
  
    printf("是否继续缴费?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    getchar();

    if (choice != 0) {
        pay();
    } else {
        printMenu_2();
        return;
    }
}
//统计
void statistic() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RESIDENT temp;
    int residentCount = 0;

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp.name,
                  temp.gender,
                  temp.phone,
                  &(temp.building),
                  &(temp.room),
                  &(temp.area),
                  &(temp.unitPrice),
                  &(temp.propertyFee),
                  &(temp.balance)) == 9) {

                fseek(file, 1, SEEK_CUR);

                fgets(temp.remarks, sizeof(temp.remarks), file);
                temp.remarks[strcspn(temp.remarks, "\n")] = '\0';
                residentList[residentCount++] = temp;
            }

    fclose(file);
    // 按欠款金额大小排序
    for (int i = 0; i < residentCount - 1; i++) {
        for (int j = 0; j < residentCount - i - 1; j++) {
            if (residentList[j].propertyFee < residentList[j + 1].propertyFee) {
                RESIDENT temp = residentList[j];
                residentList[j] = residentList[j + 1];
                residentList[j + 1] = temp;
            }
        }
    }
    // 输出结果到屏幕
    printf("按未缴清物业费金额大小排序的所有记录:\n");
    for (int i = 0; i < residentCount; i++) {
        printf("姓名:%s  楼号:%d  未缴清金额:%.2f元\n",
               residentList[i].name,
               residentList[i].building,
               residentList[i].propertyFee);
        printf("--------------------------------------------------------------------\n");
    }
	printf("按回车键回到主菜单\n"); 
	getchar();
	printMenu_2(); 
	return;
}

 以下是运行实图

 文本内容情况如下

 

代码仅供参考,未经允许禁止转载

 以下是未添加账户密码系统的代码:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define N 100//定义列表最大人数 
#define NAMLEN 20//定义姓名最大长度 
#define GENLEN 6//定义性别最大长度 
#define PHOLEN 20//定义号码最大长度 
#define REMLEN 50//定义备注最大长度 

//结构体 
typedef struct date {
	int year; 
	int month;
	int day; 
} DATE;

typedef struct resident {
    char name[NAMLEN];       // 户主姓名
    char gender[GENLEN];     // 性别
    char phone[PHOLEN];      // 联系电话
    int building;        // 楼号
    int room;            // 房号
    float area;          // 面积平米数
    float unitPrice;     // 每平米物业价格
    float propertyFee;   // 应缴纳物业费
    float balance;       //用户余额 
    char remarks[50];    // 备注
} RES;

//函数的声明 
void printMenu();
void chooseMenu(int option);
void addInfo();
void modifyInfo();
void deleteInfo();
void searchInfoByBuilding();
void searchInfoByName();
void calculateCost();
void pay();
void statistic();
RES residentList[N];  //用户列表

//主函数
int main() {
    int option = 0;  //选择  
    printMenu();
    do {
        chooseMenu(option);
        if (option != 0) {
            printMenu();
        }
    } while (option != 0);
    return 0;
}

//打印菜单
void printMenu() {
    system("cls");
    printf("\x1b[32m***********--物业费管理系统--***********\n\x1b[0m");  // 字体颜色修改为红色
    printf("* 1、添加住户信息                      *\n");
    printf("* 2、修改住户信息                      *\n");
    printf("* 3、删除住户信息                      *\n");
    printf("* 4、查询住户信息(根据户主姓名)      *\n");
    printf("* 5、查询住户信息(根据楼号查询)      *\n");
    printf("* 6、自动生成应缴物业费                *\n");
    printf("* 7、缴费系统                          *\n");
    printf("* 8、统计系统                          *\n");
    printf("* 0、退出程序                          *\n");
    printf("----------------------------------------\n");
    printf("\x1b[31m提示:请输入数字序号选择对应的操作!\n\x1b[0m"); 
    printf("请输入你的选择:");
}

//选择菜单
void chooseMenu(int option) {
    int scanfResult;

    do {
        scanfResult = scanf("%d", &option);

        // 清除错误输入数据
        while (getchar() != '\n');

        if (scanfResult != 1) {
            printf("无效输入,请重新输入\n");
        }
        else {
            switch (option) {
                case 1:
                    addInfo();
                    break;
                case 2:
                    modifyInfo();
                    break;
                case 3:
                    deleteInfo();
                    break;
                case 4:
                    searchInfoByName();
                    break;
                case 5:
                    searchInfoByBuilding();
                    break;
                case 6:
                    calculateCost();
                    break;
                case 7:
                    pay();
                    break;
                case 8:
                    statistic();
                    break;
                case 0:
                    printf("程序已退出");
                    break;
                default:
                    printf("输入有误,请重新输入\n");
                    break;
            }
        }
    } while (scanfResult != 1 || option != 0);
}

//添加信息
void addInfo() {
	residentList->balance=0.0; 
	residentList->propertyFee = 0.0;
    FILE* file = fopen("residentList.txt", "a+");//创建文件 
    if (file == NULL) {
        printf("无法创建文件。\n");
        return;
    }
    
    printf("户主姓名: ");
    scanf("%19s", residentList->name);  // 限制输入的字符数,防止缓冲区溢出
    while (getchar() != '\n');  // 清除输入缓冲区中的换行符和多余的字符

    printf("性别(man or woman): ");
    scanf("%9s", residentList->gender);
    while (getchar() != '\n');

    printf("联系电话: ");
    scanf("%19s", residentList->phone);
    while (getchar() != '\n');

    printf("楼号: ");
    while (scanf("%d", &(residentList->building)) != 1) {  // 循环直到成功读取一个整数
        printf("无效输入,请重新输入楼号: ");
        while (getchar() != '\n');
    }

    printf("房号: ");
    while (scanf("%d", &(residentList->room)) != 1) {
        printf("无效输入,请重新输入房号: ");
        while (getchar() != '\n');
    }

    printf("平米数: ");
    while (scanf("%f", &(residentList->area)) != 1) {
        printf("无效输入,请重新输入平米数: ");
        while (getchar() != '\n');
    }

    printf("每平米物业价格: ");
    while (scanf("%f", &(residentList->unitPrice)) != 1) {
        printf("无效输入,请重新输入每平米物业价格: ");
        while (getchar() != '\n');
    }

    printf("备注信息: ");
    getchar();  // 清除输入缓冲区中的换行符
    fgets(residentList->remarks, sizeof(residentList->remarks), stdin);
    residentList->remarks[strcspn(residentList->remarks, "\n")] = '\0';  //替换换行符为字符串结束符

    fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
            NAMLEN, residentList->name,
			GENLEN, residentList->gender,
            PHOLEN, residentList->phone,
			sizeof(int), residentList->building,
            sizeof(int), residentList->room,
			sizeof(float), residentList->area,
            sizeof(float), residentList->unitPrice,
			sizeof(float),residentList->propertyFee,
			sizeof(float),residentList->balance,
			REMLEN, residentList->remarks);

    fclose(file);
    // 询问是否继续添加信息
    printf("是否继续添加住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) {
        addInfo();  // 递归重新调用addInfo()函数
    } 
	else {
		printMenu();
        return;  // 结束addInfo()函数,返回到上一级调用(即主函数main())
    } 
}

//修改信息
void modifyInfo() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[NAMLEN];
    int residentCount = 0;
    int flag = 0;
    RES temp;
    RES modifiedList[N];
    printf("请输入要修改的住户的户主姓名:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0'; // 替换换行符为字符串结束符

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp.name,
                  temp.gender,
                  temp.phone,
                  &(temp.building),
                  &(temp.room),
                  &(temp.area),
                  &(temp.unitPrice),
                  &(temp.propertyFee),
                  &(temp.balance)) == 9) {
        fseek(file, 1, SEEK_CUR); // 跳过空格字符

        // 读取remarks的方式更改为fgets
        fgets(temp.remarks, sizeof(temp.remarks), file);
        temp.remarks[strcspn(temp.remarks, "\n")] = '\0';

        // 匹配户主姓名
        if (strcmp(temp.name, name) == 0) {
            flag = 1;

            printf("请输入新的户主姓名: ");
            fgets(temp.name, sizeof(temp.name), stdin);
            temp.name[strcspn(temp.name, "\n")] = '\0';

            printf("请输入新的性别(man or woman): ");
            fgets(temp.gender, sizeof(temp.gender), stdin);
            temp.gender[strcspn(temp.gender, "\n")] = '\0';
            while (getchar() != '\n');
			
            printf("请输入新的联系电话: ");
            fgets(temp.phone, sizeof(temp.phone), stdin);
            temp.phone[strcspn(temp.phone, "\n")] = '\0';

            printf("请输入新的楼号: ");
            while (scanf("%d", &(temp.building)) != 1) {
                printf("无效输入,请重新输入楼号: ");
            }
            getchar();

            printf("请输入新的房号: ");
            while (scanf("%d", &(temp.room)) != 1) {
                printf("无效输入,请重新输入房号: ");
            }
            getchar();

            printf("请输入新的平米数: ");
            while (scanf("%f", &(temp.area)) != 1) {
                printf("无效输入,请重新输入平米数: ");
            }
            getchar();

            printf("请输入新的每平米物业价格: ");
            while (scanf("%f", &(temp.unitPrice)) != 1) {
                printf("无效输入,请重新输入每平米物业价格: ");
            }
            getchar();

            printf("请输入新的备注信息: ");
            fgets(temp.remarks, sizeof(temp.remarks), stdin);
            temp.remarks[strcspn(temp.remarks, "\n")] = '\0';

            printf("住户信息修改成功!\n");
        }
        modifiedList[residentCount++] = temp;
    }

    if (!flag) {
        printf("未找到匹配的住户信息。\n");
    }

    fclose(file);

    file = fopen("residentList.txt", "w");
    if (file == NULL) {
        printf("无法打开文件进行写入。\n");
    }

    for (int i = 0; i < residentCount; i++) {
        fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
                NAMLEN, modifiedList[i].name,
                GENLEN, modifiedList[i].gender,
                PHOLEN, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                REMLEN, modifiedList[i].remarks);
    }

    fclose(file);

    printf("是否继续修改住户信息? (1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while(getchar() != '\n');

    if (choice != 0) {
        modifyInfo();
    }
    else {
        printMenu();
        return;
    }
}

//删除住户信息
void deleteInfo() {
    FILE *file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[NAMLEN];
    int flag = 0;
    int residentCount = 0;
    RES temp[N];
    printf("请输入要删除的住户的户主姓名:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp[residentCount].name,
                  temp[residentCount].gender,
                  temp[residentCount].phone,
                  &(temp[residentCount].building),
                  &(temp[residentCount].room),
                  &(temp[residentCount].area),
                  &(temp[residentCount].unitPrice),
                  &(temp[residentCount].propertyFee),
                  &(temp[residentCount].balance)) == 9) {
        fseek(file, 1, SEEK_CUR); 
        fgets(temp[residentCount].remarks, sizeof(temp[residentCount].remarks), file);
        temp[residentCount].remarks[strcspn(temp[residentCount].remarks, "\n")] = '\0';
        if(strcmp(temp[residentCount].name, name) == 0){
            flag = 1;
            continue;
        }
        residentCount++;
    }

    fclose(file);
    
    if (flag) 
	{
        file = fopen("residentList.txt", "w");
        if (file == NULL) {
            printf("无法打开文件进行写入。\n");
    }
        for(int i = 0;i < residentCount;i++){
            fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s  \n",
                NAMLEN, temp[i].name,
                GENLEN, temp[i].gender,
                PHOLEN, temp[i].phone,
                sizeof(int), temp[i].building,
                sizeof(int), temp[i].room,
                sizeof(float), temp[i].area,
                sizeof(float), temp[i].unitPrice,
                sizeof(float), temp[i].propertyFee,
                sizeof(float), temp[i].balance,
                REMLEN, temp[i].remarks); 
            }
            fclose(file);
            printf("\x1b[31m住户信息已删除!\x1b[0m\n");
    }
	else 
	{
        printf("未找到匹配的住户信息。\n");
    }

    printf("是否继续删除住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        deleteInfo();  
    } 
	else 
	{
		printMenu();
        return;  
    } 
}

// 查询住户信息(根据姓名查询)
void searchInfoByName() {
    FILE *file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    char name[NAMLEN];
    int flag = 0;
    printf("请输入户主姓名进行查询:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';

    RES temp;
    char line[200]; 
    while (fgets(line, sizeof(line), file) != NULL) 
	{
        sscanf(line, "%s %s %s %d %d %f %f %f %f %[^\n]", 
        temp.name, 
        temp.gender, 
        temp.phone, 
        &(temp.building), 
        &(temp.room), 
        &(temp.area), 
        &(temp.unitPrice), 
        &(temp.propertyFee), 
        &(temp.balance),temp.remarks);

        if (strcmp(temp.name, name) == 0) 
		{
            flag = 1;
            printf("\x1b[31m查询结果:\x1b[0m\n");
            printf("户主姓名: %s\n", temp.name);
            printf("性别: %s\n", temp.gender);
            printf("联系电话: %s\n", temp.phone);
            printf("楼号: %d\n", temp.building);
            printf("房号: %d\n", temp.room);
            printf("平米数: %.2f\n", temp.area);
            printf("每平米物业价格: %.2f\n", temp.unitPrice);
            printf("应缴物业费:%.2f\n",temp.propertyFee);
            printf("余额:%.2f\n",temp.balance);
            printf("备注信息: %s\n\n", temp.remarks);
            break;
        }
    }

    if (!flag) 
	{
        printf("未找到匹配的住户信息。\n");
    }

    fclose(file);
    printf("是否继续查询住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        searchInfoByName();  
    } 
	else 
	{
        printMenu();
        return;  
    }
}

// 查询住户信息(根据楼号查询)
void searchInfoByBuilding() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) 
	{
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    int building;
    printf("请输入要查询的楼号:");
    scanf("%d", &building);
    getchar();  

    RES temp;
    char line[200]; 
    int flag = 0;

    printf("\x1b[31m查询结果:\x1b[0m\n");
    printf("**********************************************************************************************************\n");
    printf("姓名       性别  电话         楼号  房号  平米数   每平米物业价格   应缴物业费   余额    备注         \n");
    while (fgets(line, sizeof(line), file) != NULL) 
	{
        sscanf(line, "%s %s %s %d %d %f %f %f %f %[^\n]", 
        temp.name, temp.gender, 
        temp.phone, &(temp.building), 
        &(temp.room), 
        &(temp.area), 
        &(temp.unitPrice), 
        &(temp.propertyFee), 
        &(temp.balance), 
        temp.remarks);
        if (temp.building == building) 
		{
            printf("%-10s %-5s %-14s %-4d %-4d %-8.2f %-16.2f %6.2f %10.2f   %s\n", 
            temp.name, 
            temp.gender, 
            temp.phone, 
            temp.building, 
            temp.room, 
            temp.area, 
            temp.unitPrice, 
            temp.propertyFee, 
            temp.balance, 
            temp.remarks);
            flag = 1;
			printf("**********************************************************************************************************\n");
        }
    }

    if (!flag)
	{
        printf("未找到匹配的住户信息。\n");
        while(getchar()!='\n');
    }
	
	
    fclose(file);

    printf("是否继续查询住户信息?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n');

    if (choice != 0) 
	{
        searchInfoByBuilding();  
    } 
	else
	{
        printMenu();
        return; 
    }
}

// 计算费用
void calculateCost() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RES modifiedList[N];
    int residentCount = 0;
    DATE currentDate;
    char separator; // 分隔符

    // 循环获取用户输入,直到输入格式正确为止
    while (1) {
        printf("请输入当前日期,格式为YYYY-MM-DD:");
        if (scanf("%d%c%d%c%d", &currentDate.year, &separator, &currentDate.month, &separator, &currentDate.day) != 5 || separator != '-') {
            // 如果用户输入格式不正确,清空输入缓冲区,重新输入
            printf("日期格式不正确,请重新输入\n");
            while (getchar() != '\n');
            continue;
        }
        // 检查年、月、日的范围是否合法
        if (currentDate.year < 1900 || currentDate.year > 2100 || currentDate.month < 1 || currentDate.month > 12 || currentDate.day < 1 || currentDate.day > 31) {
            printf("日期范围不正确,请重新输入\n");
            continue;
        }
        // 如果输入格式和范围都符合要求,退出循环
        break;
    }

    // 判断当前日期是否为1号
    if (currentDate.day == 1) {
        while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
            modifiedList[residentCount].name,
            modifiedList[residentCount].gender,
            modifiedList[residentCount].phone,
            &(modifiedList[residentCount].building),
            &(modifiedList[residentCount].room),
            &(modifiedList[residentCount].area),
            &(modifiedList[residentCount].unitPrice),
            &(modifiedList[residentCount].propertyFee),
            &(modifiedList[residentCount].balance)) == 9) {

            fseek(file, 1, SEEK_CUR);

            fgets(modifiedList[residentCount].remarks, sizeof(modifiedList[residentCount].remarks), file);
            modifiedList[residentCount].remarks[strcspn(modifiedList[residentCount].remarks, "\n")] = '\0';
            residentCount++;
        }

        for (int i = 0; i < residentCount; i++) {
            float previousArrears = modifiedList[i].propertyFee;

            // 检查是否存在拖欠费用
            if (previousArrears > 0) {
                // 计算本月物业费(假设为每平米物业价格乘以平米数)
                float currentPropertyFee = modifiedList[i].unitPrice * modifiedList[i].area;

                // 累加拖欠费用和本月物业费
                modifiedList[i].propertyFee = previousArrears + currentPropertyFee;
            }
            else {
                // 计算本月物业费(假设为每平米物业价格乘以平米数)
                modifiedList[i].propertyFee = modifiedList[i].unitPrice * modifiedList[i].area;
            }
        }
            fclose(file);

            file = fopen("residentList.txt", "w");
            if (file == NULL) {
                printf("无法打开文件进行写入。\n");
            }
            for(int i = 0;i < residentCount;i++){
                fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s\n",
                NAMLEN, modifiedList[i].name,
                GENLEN, modifiedList[i].gender,
                PHOLEN, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                REMLEN, modifiedList[i].remarks);
            }
        
        fclose(file);
        printf("本月物业费已自动生成!\n");
    }
    else {
        printf("当前日期不是每月1号,无需生成物业费。\n");
    }

    printf("按回车返回主菜单\n");
    getchar(); // 清除回车
    getchar(); // 延迟
    printMenu();
}

// 缴费
void pay() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RES modifiedList[N];
    char name[NAMLEN];
    int residentCount = 0;
    int flag = 0;

    printf("请输入要缴纳物业费的居民姓名:");
    scanf("%s", name);

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
            modifiedList[residentCount].name,
            modifiedList[residentCount].gender,
            modifiedList[residentCount].phone,
            &(modifiedList[residentCount].building),
            &(modifiedList[residentCount].room),
            &(modifiedList[residentCount].area),
            &(modifiedList[residentCount].unitPrice),
            &(modifiedList[residentCount].propertyFee),
            &(modifiedList[residentCount].balance)) == 9) {

            fseek(file, 1, SEEK_CUR);

            fgets(modifiedList[residentCount].remarks, sizeof(modifiedList[residentCount].remarks), file);
            modifiedList[residentCount].remarks[strcspn(modifiedList[residentCount].remarks, "\n")] = '\0';

        // 查找匹配的居民姓名
        if (strcmp(modifiedList[residentCount].name, name) == 0) {
            flag = 1;  

            // 显示用户应缴费用和余额
            printf("应缴费用:%.2f\n", modifiedList[residentCount].propertyFee);
            printf("余额:%.2f\n", modifiedList[residentCount].balance);

            float paymentAmount;
            printf("请输入要缴纳的物业费金额:");
            scanf("%f", &paymentAmount);

            // 检查缴费金额是否合法
            if (paymentAmount < 0) {
                printf("缴费金额不能为负数。\n");
                fclose(file);
                return;
            }

            // 更新用户余额和拖欠费用
            modifiedList[residentCount].balance += paymentAmount;
            modifiedList[residentCount].balance -= modifiedList[residentCount].propertyFee;
            modifiedList[residentCount].propertyFee = 0;

            // 打印缴费信息
            printf("姓名:%s\n", modifiedList[residentCount].name);
            printf("缴费金额:%.2f\n", paymentAmount);
            printf("更新后余额:%.2f\n\n", modifiedList[residentCount].balance);
        }

        residentCount++;
    }

    fclose(file);

    // 如果未找到住户,输出未找到匹配的住户信息
    if (!flag) {
        printf("未找到匹配的住户。\n");
    }

    else{
        file = fopen("residentList.txt", "w");
        if (file == NULL) {
            printf("无法打开文件进行写入。\n");
        }

        for(int i = 0;i < residentCount;i++){
                fprintf(file, "%-*s %-*s %-*s %-*d %-*d %*.2f %*.2f %*.2f %*.2f %-*s\n",
                NAMLEN, modifiedList[i].name,
                GENLEN, modifiedList[i].gender,
                PHOLEN, modifiedList[i].phone,
                sizeof(int), modifiedList[i].building,
                sizeof(int), modifiedList[i].room,
                sizeof(float), modifiedList[i].area,
                sizeof(float), modifiedList[i].unitPrice,
                sizeof(float), modifiedList[i].propertyFee,
                sizeof(float), modifiedList[i].balance,
                REMLEN, modifiedList[i].remarks);
            }

        fclose(file);

    }
  
    printf("是否继续缴费?(1-是,0-否): ");
    int choice;
    scanf("%d", &choice);
    getchar();

    if (choice != 0) {
        pay();
    } else {
        printMenu();
        return;
    }
}

//统计
void statistic() {
    FILE* file = fopen("residentList.txt", "r");
    if (file == NULL) {
        printf("文件不存在,无法打开文件。\n");
        return;
    }

    RES temp;
    int residentCount = 0;

    while (fscanf(file, "%s %s %s %d %d %f %f %f %f",
                  temp.name,
                  temp.gender,
                  temp.phone,
                  &(temp.building),
                  &(temp.room),
                  &(temp.area),
                  &(temp.unitPrice),
                  &(temp.propertyFee),
                  &(temp.balance)) == 9) {

                fseek(file, 1, SEEK_CUR);

                fgets(temp.remarks, sizeof(temp.remarks), file);
                temp.remarks[strcspn(temp.remarks, "\n")] = '\0';
                residentList[residentCount++] = temp;
            }

    fclose(file);

    // 按欠款金额大小排序
    for (int i = 0; i < residentCount - 1; i++) {
        for (int j = 0; j < residentCount - i - 1; j++) {
            if (residentList[j].propertyFee < residentList[j + 1].propertyFee) {
                RES tempResident = residentList[j];
                residentList[j] = residentList[j + 1];
                residentList[j + 1] = tempResident;
            }
        }
    }

    // 输出结果到屏幕
    printf("按未缴清物业费金额大小排序的所有记录:\n");
    for (int i = 0; i < residentCount; i++) {
        printf("姓名:%s  楼号:%d  未缴清金额:%.2f元\n",
               residentList[i].name,
               residentList[i].building,
               residentList[i].propertyFee);
        printf("--------------------------------------------------------------------\n");
    }
	printf("按回车键回到主菜单\n"); 
	getchar();
	printMenu(); 
	return;
}

 代码仅供参考,未经允许禁止转载

  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ubanillx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值