用c++写个人财务管理系统代码 设计要求; 1)可以输入个人信息(第一次进入请选择); 2)财务收支记录管理(可以添加新的收支信息,删除和修改已有信息); 3)财务收支清单可打印出来; 4)可统计总收入和总支出情况并保存到文件; 5)可查询当前个人信息及余额,并将结果保存到txt文件。
时间: 2024-04-09 12:29:50 浏览: 19
以下是一个简单的C++代码示例,实现了个人财务管理系统的基本功能。请注意,这只是一个示例,可能需要根据你的具体需求进行修改和扩展。
```cpp
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct PersonalInfo {
string name;
string address;
double balance;
};
struct FinancialRecord {
string date;
string description;
double amount;
};
void printMenu() {
cout << "1. 输入个人信息" << endl;
cout << "2. 添加收支记录" << endl;
cout << "3. 删除收支记录" << endl;
cout << "4. 修改收支记录" << endl;
cout << "5. 打印收支清单" << endl;
cout << "6. 统计总收入和总支出" << endl;
cout << "7. 查询个人信息和余额" << endl;
cout << "0. 退出" << endl;
}
void savePersonalInfo(const PersonalInfo& info) {
ofstream file("personal_info.txt");
if (file.is_open()) {
file << info.name << endl;
file << info.address << endl;
file << info.balance << endl;
file.close();
}
}
PersonalInfo loadPersonalInfo() {
PersonalInfo info;
ifstream file("personal_info.txt");
if (file.is_open()) {
getline(file, info.name);
getline(file, info.address);
file >> info.balance;
file.close();
}
return info;
}
void addFinancialRecord(vector<FinancialRecord>& records) {
FinancialRecord record;
cout << "请输入日期(YYYY-MM-DD): ";
cin >> record.date;
cin.ignore();
cout << "请输入描述: ";
getline(cin, record.description);
cout << "请输入金额: ";
cin >> record.amount;
records.push_back(record);
}
void deleteFinancialRecord(vector<FinancialRecord>& records) {
int index;
cout << "请输入要删除的记录索引: ";
cin >> index;
if (index >= 0 && index < records.size()) {
records.erase(records.begin() + index);
cout << "删除成功!" << endl;
} else {
cout << "无效的索引!" << endl;
}
}
void modifyFinancialRecord(vector<FinancialRecord>& records) {
int index;
cout << "请输入要修改的记录索引: ";
cin >> index;
if (index >= 0 && index < records.size()) {
FinancialRecord& record = records[index];
cout << "请输入新的日期(当前值为 " << record.date << "): ";
cin >> record.date;
cin.ignore();
cout << "请输入新的描述(当前值为 " << record.description << "): ";
getline(cin, record.description);
cout << "请输入新的金额(当前值为 " << record.amount << "): ";
cin >> record.amount;
cout << "修改成功!" << endl;
} else {
cout << "无效的索引!" << endl;
}
}
void printFinancialRecords(const vector<FinancialRecord>& records) {
for (const auto& record : records) {
cout << "日期: " << record.date << endl;
cout << "描述: " << record.description << endl;
cout << "金额: " << record.amount << endl;
cout << endl;
}
}
void calculateTotalIncomeAndExpense(const vector<FinancialRecord>& records, double& totalIncome, double& totalExpense) {
totalIncome = 0.0;
totalExpense = 0.0;
for (const auto& record : records) {
if (record.amount > 0) {
totalIncome += record.amount;
} else {
totalExpense += record.amount;
}
}
}
void printPersonalInfo(const PersonalInfo& info) {
cout << "姓名: " << info.name << endl;
cout << "地址: " << info.address << endl;
cout << "余额: " << info.balance << endl;
}
void savePersonalInfoToFile(const PersonalInfo& info) {
ofstream file("personal_info.txt");
if (file.is_open()) {
file << "姓名: " << info.name << endl;
file << "地址: " << info.address << endl;
file << "余额: " << info.balance << endl;
file.close();
}
}
int main() {
PersonalInfo info;
vector<FinancialRecord> records;
int choice;
do {
printMenu();
cout << "请输入选项: ";
cin >> choice;
switch (choice) {
case 1: {
cout << "请输入姓名: ";
cin.ignore();
getline(cin, info.name);
cout << "请输入地址: ";
getline(cin, info.address);
cout << "请输入余额: ";
cin >> info.balance;
savePersonalInfo(info);
break;
}
case 2: {
addFinancialRecord(records);
break;
}
case 3: {
deleteFinancialRecord(records);
break;
}
case 4: {
modifyFinancialRecord(records);
break;
}
case 5: {
printFinancialRecords(records);
break;
}
case 6: {
double totalIncome, totalExpense;
calculateTotalIncomeAndExpense(records, totalIncome, totalExpense);
cout << "总收入: " << totalIncome << endl;
cout << "总支出: " << totalExpense << endl;
break;
}
case 7: {
printPersonalInfo(info);
savePersonalInfoToFile(info);
break;
}
case 0: {
cout << "再见!" << endl;
break;
}
default: {
cout << "无效的选项!" << endl;
break;
}
}
cout << endl;
} while (choice != 0);
return 0;
}
```
请注意,此代码仅提供了基本的功能实现,可能还需要进行错误处理、输入验证和其他功能的添加。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)