选择std::vector存储收支记录,因其便于动态添加且性能足够;设计命令行菜单界面,提供添加、查看、统计等功能,使用setw格式化输出;通过遍历vector,按类型累加收入与支出,计算总收入、总支出及结余。

C++开发简易收支统计程序,关键在于数据结构的选择、输入输出的处理以及统计功能的实现。核心是管理好收支数据,并能方便地进行查询和统计。
选择合适的数据结构存储收支记录。 实现输入模块,允许用户添加收支记录。 构建统计模块,计算总收入、总支出和结余。 设计用户界面,方便用户操作和查看结果。
对于收支记录,可以考虑使用
std::vector
std::vector
例如:
#include <iostream>
#include <vector>
#include <string>
struct Record {
std::string date;
std::string description;
double amount;
std::string type; // "income" or "expense"
};
std::vector<Record> records; // Global variable to store records
void addRecord() {
Record newRecord;
std::cout << "Date (YYYY-MM-DD): ";
std::cin >> newRecord.date;
std::cout << "Description: ";
std::cin.ignore(); // Consume the newline character left by previous input
std::getline(std::cin, newRecord.description);
std::cout << "Amount: ";
std::cin >> newRecord.amount;
std::cout << "Type (income/expense): ";
std::cin >> newRecord.type;
records.push_back(newRecord);
std::cout << "Record added successfully!\n";
}
int main() {
addRecord();
return 0;
}如果需要更快的查找速度(例如,按日期范围查找),可以考虑使用
std::map
vector
std::vector
立即学习“C++免费学习笔记(深入)”;
一个简单的命令行界面已经足够。可以提供一个菜单,包含以下选项:
使用
switch
std::setw
#include <iostream>
#include <vector>
#include <string>
#include <iomanip> // For setw
// ... (Record struct and records vector from previous example)
void displayRecords() {
if (records.empty()) {
std::cout << "No records found.\n";
return;
}
std::cout << std::setw(12) << "Date"
<< std::setw(25) << "Description"
<< std::setw(10) << "Amount"
<< std::setw(10) << "Type" << std::endl;
std::cout << "----------------------------------------------------------\n";
for (const auto& record : records) {
std::cout << std::setw(12) << record.date
<< std::setw(25) << record.description
<< std::setw(10) << record.amount
<< std::setw(10) << record.type << std::endl;
}
}
// ... (addRecord function from previous example)
int main() {
int choice;
do {
std::cout << "\nMenu:\n";
std::cout << "1. Add Record\n";
std::cout << "2. Display Records\n";
std::cout << "3. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
addRecord();
break;
case 2:
displayRecords();
break;
case 3:
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 3);
return 0;
}遍历收支记录的
vector
type
#include <iostream>
#include <vector>
#include <string>
// ... (Record struct and records vector from previous example)
double calculateTotalIncome() {
double totalIncome = 0.0;
for (const auto& record : records) {
if (record.type == "income") {
totalIncome += record.amount;
}
}
return totalIncome;
}
double calculateTotalExpense() {
double totalExpense = 0.0;
for (const auto& record : records) {
if (record.type == "expense") {
totalExpense += record.amount;
}
}
return totalExpense;
}
double calculateBalance() {
return calculateTotalIncome() - calculateTotalExpense();
}
int main() {
// ... (Code to add records)
double totalIncome = calculateTotalIncome();
double totalExpense = calculateTotalExpense();
double balance = calculateBalance();
std::cout << "Total Income: " << totalIncome << std::endl;
std::cout << "Total Expense: " << totalExpense << std::endl;
std::cout << "Balance: " << balance << std::endl;
return 0;
}可以考虑添加错误处理,例如检查用户输入的金额是否为有效数字,或者类型是否为“income”或“expense”。此外,可以将数据持久化到文件中,以便下次启动程序时可以加载之前的记录。 可以使用
fstream
以上就是C++如何开发简易收支统计程序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号