#include <stdio.h>
#include <stdlib.h>
#define MAX_TRANSACTIONS 1000
// 交易记录结构体
typedef struct Transaction {
float amount; // 交易金额
char type; // 交易类型(I表示收入,E表示支出)
} Transaction;
// 读取文件中的交易记录
int read_transactions(Transaction transactions[]) {
FILE *fp = fopen("transactions.txt", "r");
if (fp == NULL) {
printf("无法打开文件。\n");
return 0;
}
int count = 0;
while (fscanf(fp, "%f %c", &transactions[count].amount, &transactions[count].type) == 2 && count < MAX_TRANSACTIONS) {
count++;
}
fclose(fp);
return count;
}
// 记录交易并写入文件
void record_transaction(Transaction transactions[], int *count, float *balance, float *expenses, float *income) {
Transaction transaction;
printf("请输入交易金额:");
scanf("%f", &transaction.amount);
printf("请选择交易类型(I表示收入,E表示支出):");
scanf(" %c", &transaction.type); // 注意需要加空格跳过之前输入的换行符
transactions[*count] = transaction;
(*count)++;
FILE *fp = fopen("transactions.txt", "a");
fprintf(fp, "%.2f %c\n", transaction.amount, transaction.type);
fclose(fp);
if (transaction.type == 'I') {
(*income) += transaction.amount;
(*balance) += transaction.amount;
} else if (transaction.type == 'E') {
(*expenses) += transaction.amount;
(*balance) -= transaction.amount;
}
}
// 显示余额
void show_balance(float balance) {
printf("当前余额:%.2f\n", balance);
}
// 显示收支报表
void show_report(float balance, float expenses, float income) {
printf("总收入:%.2f\n总支出:%.2f\n净收入:%.2f\n", income, expenses, income - expenses);
}
// 数据可视化
void chart(Transaction transactions[], int count) {
printf("交易记录可视化:\n");
// 计算每种交易类型的总额
float income = 0, expenses = 0;
for (int i = 0; i < count; i++) {
if (transactions[i].type == 'I') {
income += transactions[i].amount;
} else if (transactions[i].type == 'E') {
expenses += transactions[i].amount;
}
}
// 计算并输出交易记录部分
printf("收支记录:\n");
for (int i = 0; i < count; i++) {
printf("|");
if (transactions[i].type == 'I') {
for (int j = 0; j < (int)(transactions[i].amount / income * 40); j++) {
printf("+");
}
printf(" %.2f\n", transactions[i].amount);
} else if (transactions[i].type == 'E') {
printf("%*.2f ", (int)(transactions[i].amount / expenses * 20), transactions[i].amount);
for (int j = 0; j < (int)(transactions[i].amount / expenses * 20); j++) {
printf("-");
}
printf("|\n");
}
}
// 输出收支汇总部分
printf("\n收支汇总:\n");
printf("| 收入:%*.2f |", (int)(income / (income + expenses) * 40), income);
printf(" 支出:%*.2f |\n", (int)(expenses / (income + expenses) * 40), expenses);
for (int i = 0; i < 46; i++) {
printf("-");
}
printf("\n");
}
int main() {
int choice;
float balance = 0, expenses = 0, income = 0;
Transaction transactions[MAX_TRANSACTIONS];
int count = read_transactions(transactions);
printf("欢迎使用个人财务管理系统!\n");
while (1) {
printf("\n请选择操作:\n");
printf("1. 记录交易\n2. 显示余额\n3. 显示收支报表\n4. 数据可视化\n5. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
record_transaction(transactions, &count, &balance, &expenses, &income);
break;
case 2:
show_balance(balance);
break;
case 3:
show_report(balance, expenses, income);
break;
case 4:
chart(transactions, count);
break;
case 5:
exit(0);
default:
printf("无效选项,请重新输入。\n");
break;
}
}
return 0;
}
欢迎订阅专栏,数据结构实验,期末大作业,前端后端都有哦,想我发哪个方面的资源或文章可以私信我,免费的哦