C++记账簿

头文件

account_item.h

c 复制代码
#pragma once
#include "common.h"

struct AccountItem
{
	string itemType;
	int amount;
	string detail;
};

// 操控记账簿
void loadDataFromFile(vector<AccountItem>& items);
void accounting(vector<AccountItem>& items);
void query(const vector<AccountItem>& items);

void income(vector<AccountItem>& items);
void expand(vector<AccountItem>& items);
void insertIntoFile(const AccountItem& item);

void queryItems(const vector<AccountItem>& items);
void queryItems(const vector<AccountItem>& items, const string itemType);
void printItem(const AccountItem& item);

common.h

c 复制代码
#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

#define INCOME "收入"
#define EXPAND "支出"
#define FILENAME "D:\\AccountBook.txt"

// 显示菜单
void showMainMenu();
void showAccountingMenu();
void showQueryMenu();

// 合法性校验
char readMenuSelection(int options);
char readQuitConfirm();
int readAmount();

C++源码

account.cpp

c 复制代码
#include "common.h"
#include "account_item.h"

int main() {
	vector<AccountItem> items;
	loadDataFromFile(items);

	bool quit = false;
	while (!quit)
	{
		showMainMenu();
		char key = readMenuSelection(3);
		switch (key)
		{
		case '1':
			showAccountingMenu();
			accounting(items);
			break;
		case '2':
			showQueryMenu();
			query(items);
			break;
		case '3':
			cout << "\n确认退出? (Y/N): ";
			if (readQuitConfirm() == 'Y')
				quit = true;
			break;
		default:
			break;
		}
		cout << "\n";
	}
}
c 复制代码
#include "common.h"

void showMainMenu() {
	system("cls");
	cout << "\n";
	cout << "================== 欢迎使用记账簿 ====================" << endl;
	cout << "|                                                    |" << endl;
	cout << "|===============     1  记 账          ==============|" << endl;
	cout << "|===============     2  查 询          ==============|" << endl;
	cout << "|===============     3  退 出          ==============|" << endl;
	cout << "|____________________________________________________|" << endl;
	cout << "\n请选择(1-3):";
}

void showAccountingMenu() {
	system("cls");
	cout << "\n";
	cout << "================== 选择记账种类 =====================" << endl;
	cout << "|                                                   |" << endl;
	cout << "|===============     1  收 入         ==============|" << endl;
	cout << "|===============     2  支 出         ==============|" << endl;
	cout << "|===============     3  返回主菜单    ==============|" << endl;
	cout << "|___________________________________________________|" << endl;
	cout << "\n请选择(1-3):";
}

void showQueryMenu() {
	system("cls");
	cout << "\n";
	cout << "================== 先择查询条件 =====================" << endl;
	cout << "|                                                   |" << endl;
	cout << "|===============     1  统计所以账目  ==============|" << endl;
	cout << "|===============     2  统 计 收 入   ==============|" << endl;
	cout << "|===============     3  统 计 支 出   ==============|" << endl;
	cout << "|===============     4  返回主菜单    ==============|" << endl;
	cout << "|___________________________________________________|" << endl;
	cout << "\n请选择(1-4):";
}

operations.cpp

c 复制代码
#include "common.h"
#include "account_item.h"

// ------------------------------- 0 载入 -----------------------------------
void loadDataFromFile(vector<AccountItem>& items) {
	// 逐行读取每一条账目,包装成AccountItem
	ifstream input(FILENAME);
	AccountItem item;
	while (input >> item.itemType >> item.amount >> item.detail) {
		items.push_back(item);
	}
	input.close();
}


// ------------------------------- 1 记账 -----------------------------------
void accounting(vector<AccountItem>& items) {

	char key = readMenuSelection(3);
	switch (key)
	{
	case '1': //收入
		income(items);
		break;
	case '2': //支出
		expand(items);
		break;
	case '3': //返回主菜单
		break;
	default:
		break;
	}
	cout << "\n";
}

void income(vector<AccountItem>& items) {
	AccountItem item;
	item.itemType = INCOME;
	cout << "\n本次收入金额:";
	item.amount = readAmount();
	cout << "\n备注:";
	getline(cin, item.detail);
	//添加到vector容器中
	items.push_back(item);
	//写入文件做持久化保存
	insertIntoFile(item);
	cout << "\n--------------------------记账成功!---------------------------\n" << endl;
	cout << "\n请按回车建返回主菜单..." << endl;
	//cin.get();
	string line;
	getline(cin, line);
}

void insertIntoFile(const AccountItem& item) {
	// 创建一个ofstream对象,以追加方式写入
	ofstream output(FILENAME, ios::out | ios::app);
	output << item.itemType << "\t" << item.amount << "\t" ;
	output << item.detail << endl;
	output.close();
}

void expand(vector<AccountItem>& items) {
	AccountItem item;
	item.itemType = EXPAND;
	cout << "\n本次收入金额:";
	item.amount = -readAmount();
	cout << "\n备注:";
	getline(cin, item.detail);
	//添加到vector容器中
	items.push_back(item);
	//写入文件做持久化保存
	insertIntoFile(item);
	cout << "\n--------------------------记账成功!---------------------------\n" << endl;
	cout << "\n请按回车建返回主菜单..." << endl;
	//cin.get();
	string line;
	getline(cin, line);
}

// ------------------------------- 2 查询 -----------------------------------
void query(const vector<AccountItem>& items) {

	char key = readMenuSelection(4);
	switch (key)
	{
	case '1': //查询所有账目,并统计总收支
		queryItems(items);
		break;
	case '2': //查询所有收入,并统计总收入
		queryItems(items, INCOME);
		break;
	case '3': //查询所有支出,并统计总支出
		queryItems(items, EXPAND);
		break;
	case '4': //返回主菜单
		break;
	default:
		break;
	}
	cout << "\n";
}

void queryItems(const vector<AccountItem>& items) {
	cout << "------------------------------ 查询结果 ------------------------------" << endl;
	cout << "\n类型\t\t金额\t\t备注\n" << endl;
	// 遍历账目
	int total = 0;
	for (auto item : items) {
		printItem(item);
		total += item.amount;
	}
	// 打印总收支
	cout << "==========================================================================\n";
	cout << "总收支:" << total << endl;
	cout << "\n请按回车建返回主菜单..." << endl;
	string line;
	getline(cin, line);
}

void printItem(const AccountItem& item) {
	cout << item.itemType << "\t\t" << item.amount << "\t\t" << item.detail << endl;
}


void queryItems(const vector<AccountItem>& items,const string itemType) {
	cout << "------------------------------ 查询结果 ------------------------------" << endl;
	cout << "\n类型\t\t金额\t\t备注\n" << endl;
	// 遍历账目
	int total = 0;
	for (auto item : items) {
		if (item.itemType != itemType)
			continue;
		printItem(item);
		total += item.amount;
	}
	// 打印总收支
	cout << "==========================================================================\n";
	cout << ((itemType == INCOME) ? "总收入:" :"总支出:" )<< total << endl;
	cout << "\n请按回车建返回主菜单..." << endl;
	string line;
	getline(cin, line);
}

read_input.cpp

c 复制代码
#include "common.h"

// 读取键盘输入的菜单选项,进行合法性校验
char readMenuSelection(int options) {
	// 读取键盘输入的字符串
	string str;
	while (true) {
		getline(cin, str);
		//进行合法性校验
		if (str.size() != 1 || str[0] - '0' <= 0 || str[0] - '0' > options)
			cout << "输入错误,请重新选择:";
		else
			break;
	}
	return str[0];
}

char readQuitConfirm() {
	string str;
	while (true) {
		getline(cin, str);
		if (str.size() != 1 ||( toupper(str[0]) != 'Y' && toupper(str[0]) != 'N'))
			cout << "输入错误,请重新输入(Y/N):";
		else
			break;
	}
	return toupper(str[0]);
}

int readAmount() {
	int amount;
	string str;
	while (true) {
		getline(cin, str);
		// 字符串int
		try {
			amount = stoi(str);
			break;
		}
		catch (invalid_argument e) {
			cout << "输入错误,请正确输入数字:";
		}
	}
	return amount;
}
相关推荐
何陈陈6 分钟前
【Linux】线程池
linux·服务器·开发语言·c++
清风玉骨10 分钟前
Qt-QHBoxLayout布局类控件(42)
开发语言·qt
2401_8572979123 分钟前
秋招内推2025-招联金融
java·前端·算法·金融·求职招聘
一 乐27 分钟前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
一 乐28 分钟前
租拼车平台|小区租拼车管理|基于java的小区租拼车管理信息系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·微信·notepad++·拼车
夏旭泽30 分钟前
C-include
开发语言·c++
通信仿真实验室32 分钟前
MATLAB使用眼图分析QPSK通信系统接收端匹配滤波后的信号
开发语言·算法·matlab
感谢地心引力33 分钟前
【Qt】Qt安装(2024-10,QT6.7.3,Windows,Qt Creator 、Visual Studio、Pycharm 示例)
c++·windows·python·qt·visual studio
通信仿真实验室37 分钟前
(15)衰落信道模型作用于信号是相乘还是卷积
开发语言·人工智能·算法·matlab
xmh-sxh-13141 小时前
如何选择数据库架构
java