C语言个人支出管理系统完整源码加文档,制作不易勿白嫖

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
下面展示一些 内联代码片

部分源码,完整源码看首张截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//企鹅联系---------3765140556 
#define MAX_LEN 1000

// 记录结构体
typedef struct {
    int id; // 流水编号
    double amount; // 支出金额
    int year, month, day; // 支出时间
    char reason[MAX_LEN]; // 支出原因
    char type[MAX_LEN]; // 支出类型
} Record;

int num_records = 0; // 记录数量
Record* records = NULL; // 记录数组指针
void menu();
void menu1(int choice1,int choice2);
void menu2(int choice2);
// 加载记录
void load_records(const char* filename) {
	int n = 10000; // 记录的数量
    FILE* fp = fopen(filename, "r");
    if (fp == NULL) {
        printf("无法打开记录文件 %s\n", filename);
        return;
    }

    int max_records = 10000; // 初始最大记录数为10
    //records = malloc(max_records * sizeof(Record)); // 动态分配记录数组
	records = (Record*) malloc(n * sizeof(Record));

    char line[MAX_LEN];
    while (fgets(line, MAX_LEN, fp)) {
        if (num_records >= max_records) {
            max_records *= 2; // 最大记录数翻倍
            //records = realloc(records, max_records * sizeof(Record)); // 重新分配记录数组
            records = (Record*) malloc(n * sizeof(Record));

        }

        sscanf(line, "%d,%lf,%d-%d-%d,%[^,],%[^\n]",
            &records[num_records].id, &records[num_records].amount,
            &records[num_records].year, &records[num_records].month, &records[num_records].day,
            records[num_records].reason, records[num_records].type);

        ++num_records;
    }

    fclose(fp);
}

// 保存记录
void save_records(const char* filename) {
    FILE* fp = fopen(filename, "a+");
    if (fp == NULL) {
        printf("无法打开记录文件 %s\n", filename);
        return;
    }

    for (int i = 0; i < num_records; i++) {
        fprintf(fp, "%d,%.2lf,%04d-%02d-%02d,%s,%s\n",
            records[i].id, records[i].amount,
            records[i].year, records[i].month, records[i].day,
            records[i].reason, records[i].type);
    }

    fclose(fp);
}

// 显示所有记录
void show_records() {
    if (num_records == 0) {
        printf("没有记录\n");
        return;
    }

    printf("流水编号\t支出金额\t支出时间\t支出原因\t支出类型\n");
    printf("--------\t--------\t--------\t--------\t--------\n");

    for (int i = 0; i < num_records; i++) {
        Record* r = &records[i];
        printf("%08d\t%.2lf\t\t%04d-%02d-%02d\t%s\t\t%s\n",
            r->id, r->amount, r->year, r->month, r->day, r->reason, r->type);
    }
}

// 增加一条记录
void add_record() {
    if (num_records >= MAX_LEN) {
        printf("记录数量已达上限\n");
        return;
    }

    Record r;

    printf("请输入支出金额: ");
    scanf("%lf", &r.amount);

    printf("请输入支出时间(格式为 yyyy-mm-dd): ");
    scanf("%d-%d-%d", &r.year, &r.month, &r.day);

    printf("请输入支出原因: ");
    scanf("\n");
    fgets(r.reason, MAX_LEN, stdin);
    r.reason[strlen(r.reason)-1] = '\0'; // 去除末尾的换行符

    printf("请输入支出类型: ");
    fgets(r.type, MAX_LEN, stdin);
    r.type[strlen(r.type)-1] = '\0'; // 去除末尾的换行符

    r.id = num_records + 1;

    records[num_records] = r;
    num_records++;

    printf("成功添加一条记录\n");
}

// 删除一条记录
void delete_record() {
    printf("请输入要删除的记录的流水编号: ");
    int id;
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < num_records; i++) {
        if (records[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("找不到该记录\n");
        return;
    }

    for (int i = index; i < num_records-1; i++) {
        records[i] = records[i+1];
    }

    num_records--;

    printf("成功删除一条记录\n");
}

// 修改一条记录
void modify_record() {
    printf("请输入要修改的记录的流水编号: ");
    int id;
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < num_records; i++) {
        if (records[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("找不到该记录\n");
        return;
    }

    Record* r = &records[index];

    printf("请输入新的支出金额: ");
    scanf("%lf", &r->amount);

    printf("请输入新的支出时间(格式为 yyyy-mm-dd): ");
    scanf("%d-%d-%d", &r->year, &r->month, &r->day);

    printf("请输入新的支出原因: ");
    scanf("\n");
    fgets(r->reason, MAX_LEN, stdin);
    r->reason[strlen(r->reason)-1] = '\0'; // 去除末尾的换行符

    printf("请输入新的支出类型: ");
    fgets(r->type, MAX_LEN, stdin);
    r->type[strlen(r->type)-1] = '\0'; // 去除末尾的换行符

    printf("成功修改一条记录\n");
}

// 按流水编号查询记录
void find_by_id() {
    printf("请输入要查询的记录的流水编号: ");
    int id;
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < num_records; i++) {
        if (records[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("找不到该记录\n");
        return;
    }

    printf("流水编号\t支出金额\t支出时间\t\t\t\t支出原因\t支出类型\n");
    printf("--------\t--------\t--------\t--------\t--------\n");

    Record* r = &records[index];
    printf("%08d\t%.2lf\t%04d-%02d-%02d\t%s\t%s\n",
        r->id, r->amount, r->year, r->month, r->day, r->reason, r->type);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐瑶万正源码,可堪头相,徐福费

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

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

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

打赏作者

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

抵扣说明:

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

余额充值