头文件
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"
void loadDataFromFile ( vector< AccountItem> & items) { ifstream input ( FILENAME) ; AccountItem item; while ( input >> item. itemType >> item. amount >> item. detail) { items. push_back ( item) ; } input. close ( ) ;
}
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) ; items. push_back ( item) ; insertIntoFile ( item) ; cout << "\n--------------------------记账成功!---------------------------\n" << endl; cout << "\n请按回车建返回主菜单..." << endl; string line; getline ( cin, line) ;
} void insertIntoFile ( const AccountItem& item) { 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) ; items. push_back ( item) ; insertIntoFile ( item) ; cout << "\n--------------------------记账成功!---------------------------\n" << endl; cout << "\n请按回车建返回主菜单..." << endl; string line; getline ( cin, line) ;
}
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 << "输入错误,请重新选择:" ; 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) ; try { amount = stoi ( str) ; break ; } catch ( invalid_argument e) { cout << "输入错误,请正确输入数字:" ; } } return amount;
}