#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Transaction {
char date[11];
char category[21];
double amount;
};
// 添加一条交易记录
void addRecord(struct Transaction* records, int* count) {
struct Transaction trans;
printf("请输入交易日期(格式:YYYY-MM-DD):");
scanf("%s", trans.date);
printf("请输入交易类别:");
scanf("%s", trans.category);
printf("请输入交易金额:");
scanf("%lf", &trans.amount);
while (trans.amount <= 0) {
printf("错误:金额必须为正数,请重新输入:");
scanf("%lf", &trans.amount);
}
records[*count] = trans;
(*count)++;
printf("交易记录已添加。\n");
}
// 查找交易记录
int searchRecord(struct Transaction* records, int count, char* date, char* category) {
for (int i = 0; i < count; i++) {
if (strcmp(records[i].date, date) == 0 && strcmp(records[i].category, category) == 0) {
return i;
}
}
return -1;
}
// 删除一条交易记录
void deleteRecord(struct Transaction* records, int* count) {
char date[11], category[21];
printf("请输入要删除的交易日期(格式:YYYY-MM-DD):");
scanf("%s", date);
printf("请输入要删除的交易类别:");
scanf("%s", category);
int index = searchRecord(records, *count, date, category);
if (index == -1) {
printf("错误:未找到符合条件的交易记录。\n");
} else {
for (int i = index; i < *count - 1; i++) {
records[i] = records[i + 1];
}
(*count)--;
printf("交易记录已删除。\n");
}
}
// 修改一条交易记录
void modifyRecord(struct Transaction* records, int count) {
char date[11], category[21];
printf("请输入要修改的交易日期(格式:YYYY-MM-DD):");
scanf("%s", date);
printf("请输入要修改的交易类别:");
scanf("%s", category);
int index = searchRecord(records, count, date, category);
if (index == -1) {
printf("错误:未找到符合条件的交易记录。\n");
} else {
printf("请输入修改后的交易日期(原值为 %s):", records[index].date);
scanf("%s", records[index].date);
printf("请输入修改后的交易类别(原值为 %s):", records[index].category);
scanf("%s", records[index].category);
printf("请输入修改后的交易金额(原值为 %.2f):", records[index].amount);
scanf("%lf", &records[index].amount);
while (records[index].amount <= 0) {
printf("错误:金额必须为正数,请重新输入:");
scanf("%lf", &records[index].amount);
}
printf("交易记录已修改。\n");
}
}
// 显示交易记录列表
void showRecords(struct Transaction* records, int count) {
const int DATE_WIDTH = 12;
const int CATEGORY_WIDTH = 20;
const int AMOUNT_WIDTH = 12;
printf("%*s%*s%*s\n", DATE_WIDTH, "日期", CATEGORY_WIDTH, "类别", AMOUNT_WIDTH, "金额");
for (int i = 0; i < count; i++) {
printf("%*s%*s%*.2f\n", DATE_WIDTH, records[i].date, CATEGORY_WIDTH, records[i].category, AMOUNT_WIDTH, records[i].amount);
}
}
// 统计支出
double totalExpense(struct Transaction* records, int count) {
double total = 0;
for (int i = 0; i < count; i++) {
if (records[i].amount < 0) {
total += -records[i].amount;
}
}
return total;
}
// 统计收入
double totalIncome(struct Transaction* records, int count) {
double total = 0;
for (int i = 0; i < count; i++) {
if (records[i].amount > 0) {
total += records[i].amount;
}
}
return total;
}
int main() {
struct Transaction records[100];
int count = 0;
while (1) {
printf("请选择操作:\n");
printf("1. 添加交易记录\n");
printf("2. 删除交易记录\n");
printf("3. 修改交易记录\n");
printf("4. 显示交易记录\n");
printf("5. 统计支出\n");
printf("6. 统计收入\n");
printf("0. 退出程序\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
addRecord(records, &count);
break;
case 2:
deleteRecord(records, &count);
break;
case 3:
modifyRecord(records, count);
break;
case 4:
showRecords(records, count);
break;
case 5:
printf("总支出为:%.2f\n", totalExpense(records, count));
break;
case 6:
printf("总收入为:%.2f\n", totalIncome(records, count));
break;
case 0:
printf("程序已退出。\n");
return 0;
default:
printf("错误:无效的选择,请重新输入。\n");
}
}
return 0;
}
欢迎订阅专栏,数据结构实验,期末大作业,前端后端都有哦,想我发哪个方面的资源或文章可以私信我,免费的哦
标签:count,case,财务,管理系统,简单,break,records,printf,trans From: https://blog.csdn.net/2301_79235379/article/details/140902680