用c++做一个个人消费管理系统
时间: 2024-06-08 12:08:38 浏览: 81
C++开发的个人收支管理系统
好的,一个个人消费管理系统可以分为以下几个模块:
1. 用户登录/注册模块:用户可以通过输入用户名和密码登录系统,或者通过注册新用户账号。
2. 添加消费记录模块:用户可以添加一条新的消费记录,包括消费金额、消费日期、消费类别等信息。
3. 消费记录查询模块:用户可以根据时间、类别等条件查询历史的消费记录。
4. 消费报表模块:系统可以根据用户的消费记录生成消费报表,包括消费总额、各类别消费占比等。
下面是一个简单的示例代码,供您参考:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
// 消费记录结构体
struct ExpenseRecord {
int id;
string category;
double amount;
time_t date;
};
// 消费管理系统类
class ExpenseManager {
private:
string username;
string password;
vector<ExpenseRecord> records;
public:
ExpenseManager(string username, string password) {
this->username = username;
this->password = password;
}
// 用户登录函数
bool login(string username, string password) {
return (this->username == username && this->password == password);
}
// 用户注册函数
void registerUser(string username, string password) {
this->username = username;
this->password = password;
}
// 添加消费记录函数
void addRecord(string category, double amount) {
ExpenseRecord record;
record.id = records.size() + 1;
record.category = category;
record.amount = amount;
record.date = time(NULL);
records.push_back(record);
}
// 查询消费记录函数
vector<ExpenseRecord> getRecords(time_t start, time_t end, string category) {
vector<ExpenseRecord> result;
for (int i = 0; i < records.size(); i++) {
ExpenseRecord record = records[i];
if ((start == -1 || record.date >= start) &&
(end == -1 || record.date <= end) &&
(category.empty() || record.category == category)) {
result.push_back(record);
}
}
return result;
}
// 生成消费报表函数
void generateReport() {
double total = 0;
vector<string> categories;
vector<double> amounts;
for (int i = 0; i < records.size(); i++) {
ExpenseRecord record = records[i];
total += record.amount;
bool found = false;
for (int j = 0; j < categories.size(); j++) {
if (categories[j] == record.category) {
amounts[j] += record.amount;
found = true;
break;
}
}
if (!found) {
categories.push_back(record.category);
amounts.push_back(record.amount);
}
}
cout << "----- 消费报表 -----" << endl;
cout << "总消费金额:" << total << endl;
for (int i = 0; i < categories.size(); i++) {
double percentage = amounts[i] / total * 100;
cout << categories[i] << ":" << amounts[i] << ",占比:" << percentage << "%" << endl;
}
}
// 保存消费记录到文件函数
void saveToFile(string filename) {
ofstream fout(filename);
for (int i = 0; i < records.size(); i++) {
ExpenseRecord record = records[i];
fout << record.id << "," << record.category << "," << record.amount << "," << record.date << endl;
}
fout.close();
}
// 从文件中加载消费记录函数
void loadFromFile(string filename) {
ifstream fin(filename);
string line;
while (getline(fin, line)) {
ExpenseRecord record;
sscanf(line.c_str(), "%d,%[^,],%lf,%ld", &record.id, record.category.c_str(), &record.amount, &record.date);
records.push_back(record);
}
fin.close();
}
};
// 测试代码
int main() {
ExpenseManager manager("admin", "123456");
if (manager.login("admin", "123456")) {
cout << "登录成功!" << endl;
}
manager.addRecord("餐饮", 100);
manager.addRecord("购物", 200);
manager.addRecord("餐饮", 50);
manager.addRecord("交通", 30);
vector<ExpenseRecord> records = manager.getRecords(-1, -1, "餐饮");
cout << "查询结果:" << endl;
for (int i = 0; i < records.size(); i++) {
ExpenseRecord record = records[i];
cout << record.id << " " << record.category << " " << record.amount << " " << record.date << endl;
}
manager.generateReport();
manager.saveToFile("expenses.txt");
manager.loadFromFile("expenses.txt");
return 0;
}
```
以上代码仅供参考,具体实现方式可以根据您的需求进行修改和完善。
阅读全文