下面展示一些 内联代码片
。
部分源码,完整源码看首张截图
#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);
}