家庭财务管理系统

 家庭财务管理系统  ,记住保存的地址位置,这个系统是将输入的信息保存在本位置的名为data.txt或data的文件夹里。

有需要实验报告可以点击有需要此文件夹中含有一下内容(

一、系统需求分析

1.需求分析

2.功能设计

二、系统概要设计

1.系统结构图

2.主程序流程图

三、系统详细设计与编码实现

1.财务增加模块实现

2.财务删除模块实现

3.财务修改模块实现

4.财务查询模块实现

5.财务统计模块实现

四、调试与测试分析

1.调试

2、测试分析

五、总结)

一、代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAXNAME 11    //家庭成员姓名的最大长度
#define MAXDETAIL 21    //注最大长度


//自定义枚举类型fi_type,用来表示收入与支出
typedef enum _fi_type {
	income = 1, //收入
	payout = -1 //支出
} fi_type;

// 家庭财务信息结构体
typedef struct _fi_info {
	int year;
	int month;
	fi_type type;
	char name[9];
	float money;
	char detail[11];
} fi_info;

// 存储财务数据结构的结构体
typedef struct _fi_data {
	fi_info info;
	struct _fi_data *next;
} fi_data;
fi_data *head;//财务数据的头结点
void menu();//菜单函数
void add_income();
void search_income();
void delete_income();
void update_income();
void add_payout();
void search_payout();
void delete_payout();
void update_payout();
void count_total();
void quit();
//
void search_data(fi_type type);
void delete_data(fi_type type);
void update_data(fi_type type);
//
void initialize();
void save_to_file();
void clear_data();
fi_data *get_last();
fi_data *get_previous(fi_data *p);
void input_info(fi_info *info);
void show_info(fi_data *p);

void menu() {
	printf("\n");
	printf("\t\t            ~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t            ~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t\t       家庭财务管理系统\n");
	printf("\t\t            ~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t            ~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t*******************************************\n");
	printf("\t\t收入管理:");
	printf("1.添加收入");
	printf("  2.查询收入明细\n");
	printf("\t\t\t 3.删除收入");
	printf("  4.修改收入\n");
	printf("\t\t*******************************************\n");
	printf("\t\t支出管理:");
	printf("5.添加支出");
	printf("  6.查询支出明细\n");
	printf("\t\t\t 7.删除支出");
	printf("  8.修改支出\n");
	printf("\t\t*******************************************\n");
	printf("\t\t统    计:");
	printf("9.统计总收入/总支出\n");
	printf("\t\t*******************************************\n");
	printf("\t\t退    出:0.退出系统\n");
	printf("\t\t*******************************************\n\n\n");

}

void main() {
	int selected = 0;
	initialize();
	while (selected >= 0 && selected <= 9) {
		system("cls");
		menu();
		printf("\t\t>请选择要进行的操作(0-9):");
		if (scanf("%d", &selected) != 1 || selected < 0 || selected > 9) {
			printf("\t\t>输入有误!请选择(0-9)之间的数字!按任意键重试");
			getchar();
		} else {
			switch (selected) {
				case 0:
					exit(0);
					clear_data();
					break;
				case 1:
					add_income();
					save_to_file();
					break;
				case 2:
					search_income();
					break;
				case 3:
					delete_income();
					save_to_file();
					break;
				case 4:
					update_income();
					save_to_file();
					break;
				case 5:
					add_payout();
					save_to_file();
					break;
				case 6:
					search_payout();
					break;
				case 7:
					delete_payout();
					save_to_file();
					break;
				case 8:
					update_payout();
					save_to_file();
					break;
				case 9:
					count_total();
					break;
			}

		}

	}

}

void add_income() { //添加收入
	fi_data *p = (fi_data *)malloc(sizeof(fi_data));
	p->next = NULL;
	input_info(&(p->info));
	p->info.type = income;
	if (head == NULL)
		head = p;
	else
		get_last()->next = p;
}

void add_payout() { //添加支出
	fi_data *p = (fi_data *)malloc(sizeof(fi_data));
	p->next = NULL;
	input_info(&(p->info));
	p->info.type = payout;
	if (head == NULL)
		head = p;
	else
		get_last()->next = p;
}

//输入函数
void input_info(fi_info *info) {
	printf("\t\t>请输入年月(YYYY/M)");
	scanf("%d/%d", &(info->year), &(info->month));
	printf("\t\t>请输入家庭成员姓名(最大长度为%d):", MAXNAME - 1);
	scanf("%s", info->name);
	printf("\t\t>输入金额:");
	scanf("%f", &(info->money));
	printf("\t\t>请输入备注(最大长度为%d):", 20);
	scanf("%s", info->detail);
}

//查找数据函数
void search_data(fi_type type) {
	int year = 0;
	int month = 0;
	fi_data *p = NULL;
	int count = 0;
	char input = ' ';
	while (1) {
		printf("\t\t>请输入要查询的年月(YYYY/M)");
		if (scanf("%d/%d", &year, &month) != 2) {
			printf("\t\t>输入有误\n");

		} else {
			p = head;
			count = 0;
			while (p != NULL) {
				if ((p->info.year == year) && (p->info.month == month) && (p->info.type == type)) {
					show_info(p);
					system("PAUSE");
					count++;
				}
				p = p->next;
			}
			if (count == 0) {
				printf("\t\t>没有找到数据\n");
			}
			printf("\t\t>继续查找其他数据?(y or n)");
			input = getchar();
			if (input == 'y' || input == 'Y')
				continue;
			else
				break;
		}
	}
}

//初始化函数,读取文本数据
void initialize() {
	FILE *fp = NULL;
	fi_data *p = NULL;
	fi_data *last = NULL;
	int count = 0;
	fp = fopen("data.txt", "rb");
	if (fp == NULL) {
		fp = fopen("data.txt", "w");
		fclose(fp);
		return;
	}
	p = (fi_data *)malloc(sizeof(fi_data));
	memset(p, 0, sizeof(fi_data));
	p->next = NULL;
	while (fread(&(p->info), sizeof(fi_info), 1, fp) == 1) {
		if (head == NULL)
			head = p;
		else {
			last = get_last();
			last->next = p;
		}
		count++;
		fseek(fp, count * sizeof(fi_info), SEEK_SET);
		p = (fi_data *)malloc(sizeof(fi_data));
		memset(p, 0, sizeof(fi_data));
		p->next = NULL;
	}
	free(p);
	p = NULL;
	fclose(fp);
}

//显示信息函数
void show_info(fi_data *p) {
	int i = 0;
	printf("|\t\t|\t\t|\t\t|\t\t|\t\t|\t\t|\n");
	printf("|NO.\t |年/月\t\t|类型\t\t|姓名\t\t|金额\t\t|备注\t\t|\n");

	printf("|%5d\t|%4d/%02d\t|%4s\t|%10s\t|%10.2f|%20s|\n", i + 1,
	       p->info.year, p->info.month, p->info.type == income ? "收入" : "支出",
	       p->info.name, p->info.money, p->info.detail);
	printf("|\t\t|\t\t|\t\t|\t\t|\t\t|\t\t|\n");
}

//统计总收入、总支出
void count_total() {
	float total_income = 0.0;
	float total_payout = 0.0;
	fi_data *p = head;
	while (p != NULL) {
		if (p->info.type == income) {
			total_income += p->info.money;
		} else {
			total_payout += p->info.money;
		}
		p = p->next;
	}
	printf("\t\t+\t\t+\t\t+\n");
	printf("\t\t|合计收入\t|合计支出\t|结余\t|\n");
	printf("\t\t%12.2f|%12.2f|%12.2f|\n", total_income, total_payout, total_income - total_payout);
	printf("\t\t+\t\t+\t\t+\n");
	printf("\t\t>按任意键返回主菜单");
	getchar();
	getchar();

}

//保存到文件
void save_to_file() {
	FILE *fp = fopen("data.txt", "wb" );
	fi_data *p = head;
	while (p != NULL) {
		fwrite(&(p->info), sizeof(fi_info), 1, fp);
		fseek(fp, 0, SEEK_END);
		p = p->next;
	}
	fclose(fp);
}

//修改信息函数
void update_data(fi_type type) {
	int year = 0;
	int month = 0;
	fi_data *p = NULL;
	fi_data *pre = NULL;
	char input;
	int count = 0;
	int i = 0;
	while (1) {
		printf("\t\t>请输入要查询的年月(YYYY/M)");
		if (scanf("%d/%d", &year, &month) != 2) {
			printf("\t\t>输入有误\n");

		} else {
			p = head;
			count = 0;
			while (p != NULL) {
				if ((p->info.year == year) && (p->info.month == month) && (p->info.type == type)) {
					count++;
					show_info(p);
					input_info(&(p->info));
					printf("\t\t修改成功\n");
					p = get_previous(p);
				}

				p = p->next;
			}
			if (count == 0)
				printf("\t\t>没有找到数据\n");
			printf("\t\t>继续查找其他数据?(y or n)");
			input = getchar();
			if (input == 'y' || input == 'Y')
				continue;
			else
				break;
		}
	}

}

void delete_data(fi_type type) { //删除数据函数
	int year = 0;
	int month = 0;
	fi_data *p = NULL;
	fi_data *pre = NULL;
	int count = 0;
	char input = ' ';
	int i = 0;
	while (1) {
		printf("\t\t>请输入要查询的年月(YYYY/M)");
		if (scanf("%d/%d", &year, &month) != 2) {
			printf("\t\t>输入有误\n");

		} else {
			p = head;
			count = 0;
			while (p != NULL) {
				if ((p->info.year == year) && (p->info.month == month) && (p->info.type == type)) {
					count++;
					show_info(p);
					pre = get_previous(p);
					if (pre == NULL)
						head = head->next;
					else
						pre->next = p->next;
					free(p);
					printf("\t\t>删除成功\n");
				}
				p = p->next;
			}
			if (count == 0)

				printf("\t\t>没有找到数据\n");
			printf("\t\t>继续查找其他数据?(y or n)");
			input = getchar();
			if (input == 'y' || input == 'Y')
				continue;
			else
				break;
		}

	}
}

void clear_data() { //清空链表数据
	fi_data *p = NULL;
	while (head != NULL) {
		if (head->next != NULL) {
			p = head;
			head = head->next;
			free(p);
			p = NULL;
		} else {
			free(head);
			head = NULL;
		}

	}
}

fi_data *get_last() {
	fi_data *p = head;
	if (p == NULL)
		return p;
	while ((p != NULL) && (p->next != NULL))
		p = p->next;
	return p;
}

fi_data *get_previous(fi_data *p) {
	fi_data *previous = head;
	while (previous != NULL) {
		if (previous->next == p)
			break;
		previous = previous->next;
	}
	return previous;
}

void search_payout() {
	search_data(payout);
}

void delete_payout() {
	delete_data(payout);
}

void update_payout() {
	update_data(payout);
}

void search_income() {
	search_data(income);
}

void delete_income() {
	delete_data(income);
}

void update_income() {
	update_data(income);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI·勤川·

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

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

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

打赏作者

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

抵扣说明:

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

余额充值