本文主要是介绍C++记账簿,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
头文件
account_item.h
#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
#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
#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";}
}
menus.cpp
#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
#include "common.h"
#include "account_item.h"// ------------------------------- 0 载入 -----------------------------------
void loadDataFromFile(vector<AccountItem>& items) {// 逐行读取每一条账目,包装成AccountItemifstream 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
#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 << "输入错误,请重新选择:";elsebreak;}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):";elsebreak;}return toupper(str[0]);
}int readAmount() {int amount;string str;while (true) {getline(cin, str);// 字符串inttry {amount = stoi(str);break;}catch (invalid_argument e) {cout << "输入错误,请正确输入数字:";}}return amount;
}
这篇关于C++记账簿的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!