c语言家庭财务管理系统
时间: 2023-12-12 08:34:40 浏览: 22
以下是一个简单的C语言家庭财务管理系统的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义一个结构体来存储财务信息
struct finance {
char type[20]; // 收入或支出类型
float amount; // 金额
char date[20]; // 日期
char note[50]; // 备注
};
// 定义一个链表节点
struct node {
struct finance data;
struct node *next;
};
// 定义一个全局变量,指向链表头节点
struct node *head = NULL;
// 添加财务信息
void add_finance() {
struct finance f;
printf("请输入收入或支出类型:");
scanf("%s", f.type);
printf("请输入金额:");
scanf("%f", &f.amount);
printf("请输入日期:");
scanf("%s", f.date);
printf("请输入备注:");
scanf("%s", f.note);
// 创建一个新节点
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = f;
new_node->next = NULL;
// 如果链表为空,将新节点作为头节点
if (head == NULL) {
head = new_node;
}
// 否则将新节点插入到链表末尾
else {
struct node *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
printf("添加成功!\n");
}
// 查询财务信息
void query_finance() {
if (head == NULL) {
printf("暂无财务信息!\n");
return;
}
printf("请输入要查询的日期:");
char date[20];
scanf("%s", date);
struct node *p = head;
int count = 0;
float total = 0;
while (p != NULL) {
if (strcmp(p->data.date, date) == 0) {
printf("%s\t%.2f\t%s\t%s\n", p->data.type, p->data.amount, p->data.date, p->data.note);
count++;
total += p->data.amount;
}
p = p->next;
}
if (count == 0) {
printf("未查询到财务信息!\n");
}
else {
printf("共查询到%d条财务信息,总金额为%.2f元。\n", count, total);
}
}
// 删除财务信息
void delete_finance() {
if (head == NULL) {
printf("暂无财务信息!\n");
return;
}
printf("请输入要删除的日期:");
char date[20];
scanf("%s", date);
struct node *p = head;
struct node *prev = NULL;
int count = 0;
float total = 0;
while (p != NULL) {
if (strcmp(p->data.date, date) == 0) {
if (prev == NULL) {
head = p->next;
}
else {
prev->next = p->next;
}
printf("%s\t%.2f\t%s\t%s\n", p->data.type, p->data.amount, p->data.date, p->data.note);
free(p);
count++;
total += p->data.amount;
p = prev == NULL ? head : prev->next;
}
else {
prev = p;
p = p->next;
}
}
if (count == 0) {
printf("未查询到财务信息!\n");
}
else {
printf("共删除了%d条财务信息,总金额为%.2f元。\n", count, total);
}
}
// 修改财务信息
void modify_finance() {
if (head == NULL) {
printf("暂无财务信息!\n");
return;
}
printf("请输入要修改的日期:");
char date[20];
scanf("%s", date);
struct node *p = head;
int count = 0;
float total = 0;
while (p != NULL) {
if (strcmp(p->data.date, date) == 0) {
printf("%s\t%.2f\t%s\t%s\n", p->data.type, p->data.amount, p->data.date, p->data.note);
printf("请输入新的收入或支出类型:");
scanf("%s", p->data.type);
printf("请输入新的金额:");
scanf("%f", &p->data.amount);
printf("请输入新的日期:");
scanf("%s", p->data.date);
printf("请输入新的备注:");
scanf("%s", p->data.note);
printf("修改成功!\n");
count++;
total += p->data.amount;
}
p = p->next;
}
if (count == 0) {
printf("未查询到财务信息!\n");
}
else {
printf("共修改了%d条财务信息,总金额为%.2f元。\n", count, total);
}
}
// 统计财务信息
void statistics_finance() {
if (head == NULL) {
printf("暂无财务信息!\n");
return;
}
struct node *p = head;
int count = 0;
float income = 0;
float expense = 0;
while (p != NULL) {
if (strcmp(p->data.type, "收入") == 0) {
income += p->data.amount;
}
else {
expense += p->data.amount;
}
count++;
p = p->next;
}
printf("共统计了%d条财务信息,其中收入%.2f元,支出%.2f元,净收入%.2f元。\n", count, income, expense, income - expense);
}
int main() {
int choice;
while (1) {
printf("家庭财务管理系统\n");
printf("1.添加财务信息\n");
printf("2.查询财务信息\n");
printf("3.删除财务信息\n");
printf("4.修改财务信息\n");
printf("5.统计财务信息\n");
printf("6.退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_finance();
break;
case 2:
query_finance();
break;
case 3:
delete_finance();
break;
case 4:
modify_finance();
break;
case 5:
statistics_finance();
break;
case 6:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
return 0;
}
```