Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
C++如何开发简易收支统计程序_创想鸟

C++如何开发简易收支统计程序

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

c++如何开发简易收支统计程序

C++开发简易收支统计程序,关键在于数据结构的选择、输入输出的处理以及统计功能的实现。核心是管理好收支数据,并能方便地进行查询和统计。

选择合适的数据结构存储收支记录。实现输入模块,允许用户添加收支记录。构建统计模块,计算总收入、总支出和结余。设计用户界面,方便用户操作和查看结果。

如何选择合适的数据结构来存储收支记录?

对于收支记录,可以考虑使用

std::vector

配合自定义的结构体或类。结构体/类可以包含诸如日期、描述、金额、类型(收入/支出)等字段。

std::vector

动态数组的优势在于方便添加记录,且在数据量不大时性能足够。

例如:

#include #include #include struct Record {    std::string date;    std::string description;    double amount;    std::string type; // "income" or "expense"};std::vector records; // Global variable to store recordsvoid addRecord() {    Record newRecord;    std::cout <> newRecord.date;    std::cout << "Description: ";    std::cin.ignore(); // Consume the newline character left by previous input    std::getline(std::cin, newRecord.description);    std::cout <> newRecord.amount;    std::cout <> 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 #include #include #include  // 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 Recordn";        std::cout << "2. Display Recordsn";        std::cout << "3. Exitn";        std::cout <> 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 #include #include // ... (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++如何开发简易收支统计程序的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1475392.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C++11 lambda表达式与捕获列表混合使用
上一篇 2025年12月18日 23:21:26
C++指针和引用混合使用语法解析
下一篇 2025年12月18日 23:21:38

相关推荐

发表回复

登录后才能评论
关注微信