You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C语言从TXT文件计算月度及年度支出?

如何用C语言从TXT文件进行月度和年度支出计算?

我正在编写一个C语言读写程序,但不清楚如何实现月度和年度支出统计操作,请求帮助。

示例TXT文件内容

Jaka 2022 12  10 50000
Juki 2020 10  12 750000
Jaka 12 10  10 4000

现有代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

void input();
void count();
void monthly_expenditure();

int main()
{
    int choice;
    do
    {
        printf("====================================MENU====================================\n");
        printf("1. Input Data\n");
        printf("2. count expenditure\n");
        printf("3. monthly expenditure\n");
        printf("4. exit\n");
        printf("choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            input();
            getchar();
            break;
        case 2:
            count();
            getchar();
            break;
        case 3:
            monthly_expenditure();
            getchar();
            break;
        case 4:
            printf("thank you\n");
            getchar();
            break;
        default:
            printf("error\n");
            break;
        }
    } while (choice != 4);
    return 0;
}

void input()
{
    FILE *fp;
    char name[100], year[100], month[100], date[100], money[100];
    fp = fopen("data.txt", "a");

    printf("Input name : ");
    scanf("%s", name);
    printf("Input year : ");
    scanf("%s", year);
    printf("Input month : ");
    scanf("%s", month);
    printf("Input date : ");
    scanf("%s", date);
    printf("Input money : ");
    scanf("%s", money);
    fprintf(fp, "%s %s %s  %s %s\n", name, year, month, date, money);
    fclose(fp);
}

void count()
{
    FILE *fp;
    char name[100], year[100], month[100], date[100], money[100];
    int total = 0;
    fp = fopen("data.txt", "r");

    while (fscanf(fp, "%s %s %s %s %s", name, year, month, date, money) != EOF)
    {
        total += atoi(money);
    }
    printf("Total expenditure : %d\n", total);
    fclose(fp);
}

void monthly_expenditure(){

}
void year_expenditure(){

}

解决方案

1. 先修复数据输入的合法性问题

示例数据里存在年份输入错误(比如Jaka 12 10 10 4000中的年份仅2位),可以在input函数中添加基础校验,确保年份、月份、金额等数据符合逻辑:

void input()
{
    FILE *fp;
    char name[100];
    int year, month, date, money;
    fp = fopen("data.txt", "a");
    if (!fp) {
        perror("Failed to open file");
        return;
    }

    printf("Input name : ");
    scanf("%s", name);
    
    // 校验4位年份
    do {
        printf("Input year (4 digits): ");
        if (scanf("%d", &year) != 1 || year < 1900 || year > 2100) {
            printf("Invalid year! Enter 4-digit number between 1900-2100.\n");
            while (getchar() != '\n'); // 清除输入缓冲区垃圾
        }
    } while (year < 1900 || year > 2100);
    
    // 校验1-12月份
    do {
        printf("Input month (1-12): ");
        if (scanf("%d", &month) != 1 || month < 1 || month > 12) {
            printf("Invalid month! Enter number between 1-12.\n");
            while (getchar() != '\n');
        }
    } while (month < 1 || month > 12);
    
    // 校验1-31日期
    do {
        printf("Input date (1-31): ");
        if (scanf("%d", &date) != 1 || date < 1 || date > 31) {
            printf("Invalid date! Enter number between 1-31.\n");
            while (getchar() != '\n');
        }
    } while (date < 1 || date > 31);
    
    // 校验正数金额
    do {
        printf("Input money (positive number): ");
        if (scanf("%d", &money) != 1 || money <= 0) {
            printf("Invalid money! Enter positive number.\n");
            while (getchar() != '\n');
        }
    } while (money <= 0);

    fprintf(fp, "%s %d %d  %d %d\n", name, year, month, date, money);
    fclose(fp);
}

2. 实现月度支出统计函数

让用户输入目标年月,遍历文件累加对应记录的金额:

void monthly_expenditure()
{
    FILE *fp;
    char name[100];
    int target_year, target_month;
    int year, month, date, money;
    int total = 0;

    printf("Enter year to query: ");
    scanf("%d", &target_year);
    printf("Enter month to query: ");
    scanf("%d", &target_month);

    fp = fopen("data.txt", "r");
    if (!fp) {
        perror("Failed to open file");
        return;
    }

    while (fscanf(fp, "%s %d %d %d %d", name, &year, &month, &date, &money) != EOF) {
        if (year == target_year && month == target_month) {
            total += money;
        }
    }

    printf("Total expenditure for %d-%d: %d\n", target_year, target_month, total);
    fclose(fp);
}

3. 实现年度支出统计函数

类似月度统计,用户输入目标年份后累加对应记录:

void year_expenditure()
{
    FILE *fp;
    char name[100];
    int target_year;
    int year, month, date, money;
    int total = 0;

    printf("Enter year to query: ");
    scanf("%d", &target_year);

    fp = fopen("data.txt", "r");
    if (!fp) {
        perror("Failed to open file");
        return;
    }

    while (fscanf(fp, "%s %d %d %d %d", name, &year, &month, &date, &money) != EOF) {
        if (year == target_year) {
            total += money;
        }
    }

    printf("Total expenditure for %d: %d\n", target_year, total);
    fclose(fp);
}

4. 更新主菜单

添加年度统计选项,调整main函数的菜单和分支:

int main()
{
    int choice;
    do
    {
        printf("====================================MENU====================================\n");
        printf("1. Input Data\n");
        printf("2. Count Total Expenditure\n");
        printf("3. Monthly Expenditure\n");
        printf("4. Yearly Expenditure\n");
        printf("5. Exit\n");
        printf("choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            input();
            getchar();
            break;
        case 2:
            count();
            getchar();
            break;
        case 3:
            monthly_expenditure();
            getchar();
            break;
        case 4:
            year_expenditure();
            getchar();
            break;
        case 5:
            printf("thank you\n");
            getchar();
            break;
        default:
            printf("error\n");
            break;
        }
    } while (choice != 5);
    return 0;
}

额外优化方向

  • 可以统计每个用户的月度/年度支出,用结构体或数组存储多用户统计结果
  • 封装重复的文件操作逻辑,减少代码冗余
  • 处理闰年、不同月份天数的精确日期校验

内容的提问来源于stack exchange,提问作者rotibakar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:15:35