首页 后端开发 C++ 如何通过C++编写一个简单的记账本程序?

如何通过C++编写一个简单的记账本程序?

Nov 03, 2023 am 09:52 AM
编程 c++ 记账本

如何通过C++编写一个简单的记账本程序?

本文将介绍如何使用C++编写一个简单的记账本程序,随着生活成本的不断上升,越来越多的人开始关注自己的财务状况。使用记账本可以记录收支情况,提高理财能力,C++语言的优势在于其高效性和可移植性,非常适合编写此类程序。

1.确定程序功能和需求

在编写程序之前,我们首先需要明确程序要实现的功能和需求,一个简单的记账本程序需要具备以下功能:

(1)能够记录每一笔支出和收入的金额和类型,并记录时间;

(2)能够计算总体收支情况,包括收入和支出的总数;

(3)能够生成报表,统计每个类型的支出和收入总和;

(4)能够对记录进行增删改查操作。

  1. 设计数据结构和算法

在程序中,我们需要使用数据结构来存储每一笔记录,常用的数据结构有线性表、栈和队列等。在此,我们选择使用线性表来存储每一笔记录,每条记录包括以下信息:

(1)记录的唯一ID编号;

(2)记录的时间戳;

(3)记录类型,包括收入和支出;

(4)记录金额;

(5)记录详情。

对于算法,我们需要设计函数来实现各种不同的操作,如添加、删除、更新、查询和统计,这些操作需要访问记录的数据,同时也需要计算收支总额和类型分类总和并生成相应的报表。

  1. 编写代码

在开始编写代码之前,我们需要进行一些准备工作,包括:

(1)选择一个集成开发环境(IDE),例如Visual Studio;

(2)学习C++基本语法和使用STL库进行编程;

(3)设计程序的类和函数。

在C++中,我们可以使用类来表示记账本程序,这个类可以包含各种成员函数和成员变量,以及相应的访问控制符。

下面是一个简单的记账本程序的代码示例:

#include <iostream>
#include <vector>

using namespace std;

class Record {
public:
  int id;
  string date;
  string type;
  double amount;
  string description;
  Record(int _id, string _date, string _type, double _amount, string _description) {
    id = _id;
    date = _date;
    type = _type;
    amount = _amount;
    description = _description;
  }
};

class AccountBook {
public:
  vector<Record> records;

  void addRecord(int id, string date, string type, double amount, string description) {
    records.push_back(Record(id, date, type, amount, description));
  }

  void deleteRecord(int id) {
    for (vector<Record>::iterator it = records.begin(); it != records.end(); it++) {
      if (it->id == id) {
        records.erase(it);
        break;
      }
    }
  }

  void updateRecord(int id, double amount) {
    for (vector<Record>::iterator it = records.begin(); it != records.end(); it++) {
      if (it->id == id) {
        it->amount = amount;
        break;
      }
    }
  }

  void searchRecords(string type) {
    for (vector<Record>::iterator it = records.begin(); it != records.end(); it++) {
      if (it->type == type) {
        cout << "ID: " << it->id << endl;
        cout << "Date: " << it->date << endl;
        cout << "Type: " << it->type << endl;
        cout << "Amount: " << it->amount << endl;
        cout << "Description: " << it->description << endl;
      }
    }
  }

  void generateReport() {
    vector<double> income(5, 0);
    vector<double> expense(5, 0);
    for (vector<Record>::iterator it = records.begin(); it != records.end(); it++) {
      if (it->type == "Income") {
        income[0] += it->amount;
        if (it->description == "Salary") income[1] += it->amount;
        else if (it->description == "Bonus") income[2] += it->amount;
        else if (it->description == "Investment") income[3] += it->amount;
        else if (it->description == "Gift") income[4] += it->amount;
      }
      else if (it->type == "Expense") {
        expense[0] += it->amount;
        if (it->description == "Food") expense[1] += it->amount;
        else if (it->description == "Clothing") expense[2] += it->amount;
        else if (it->description == "Housing") expense[3] += it->amount;
        else if (it->description == "Transportation") expense[4] += it->amount;
      }
    }
    cout << "Total income: " << income[0] << endl;
    cout << "Salary: " << income[1] << endl;
    cout << "Bonus: " << income[2] << endl;
    cout << "Investment: " << income[3] << endl;
    cout << "Gift: " << income[4] << endl;
    cout << "Total expense: " << expense[0] << endl;
    cout << "Food: " << expense[1] << endl;
    cout << "Clothing: " << expense[2] << endl;
    cout << "Housing: " << expense[3] << endl;
    cout << "Transportation: " << expense[4] << endl;
  }

  double calculateBalance() {
    double income = 0, expense = 0;
    for (vector<Record>::iterator it = records.begin(); it != records.end(); it++) {
      if (it->type == "Income") {
        income += it->amount;
      }
      else if (it->type == "Expense") {
        expense += it->amount;
      }
    }
    return income - expense;
  }
};

void printMenu() {
  cout << "1. Add record" << endl;
  cout << "2. Delete record" << endl;
  cout << "3. Update record" << endl;
  cout << "4. Search records" << endl;
  cout << "5. Generate report" << endl;
  cout << "6. Calculate balance" << endl;
  cout << "7. Quit" << endl;
}

int main() {
  AccountBook accountBook;
  int choice;
  while (true) {
    printMenu();
    cout << "Enter your choice: ";
    cin >> choice;
    if (choice == 7) {
      cout << "Goodbye!" << endl;
      break;
    }
    switch (choice) {
      case 1: {
        int id;
        string date, type, description;
        double amount;
        cout << "Enter ID: ";
        cin >> id;
        cout << "Enter date (YYYY-MM-DD): ";
        cin >> date;
        cout << "Enter type (Income/Expense): ";
        cin >> type;
        cout << "Enter amount: ";
        cin >> amount;
        cout << "Enter description: ";
        cin >> description;
        accountBook.addRecord(id, date, type, amount, description);
        cout << "Record added." << endl;
        break;
      }
      case 2: {
        int id;
        cout << "Enter ID: ";
        cin >> id;
        accountBook.deleteRecord(id);
        cout << "Record deleted." << endl;
        break;
      }
      case 3: {
        int id;
        double amount;
        cout << "Enter ID: ";
        cin >> id;
        cout << "Enter amount: ";
        cin >> amount;
        accountBook.updateRecord(id, amount);
        cout << "Record updated." << endl;
        break;
      }
      case 4: {
        string type;
        cout << "Enter type (Income/Expense): ";
        cin >> type;
        accountBook.searchRecords(type);
        break;
      }
      case 5: {
        accountBook.generateReport();
        break;
      }
      case 6: {
        cout << "Balance: " << accountBook.calculateBalance() << endl;
        break;
      }
      default: {
        cout << "Invalid choice." << endl;
        break;
      }
    }
  }
  return 0;
}
登录后复制
  1. 测试程序

在完成代码编写后,我们需要对程序进行测试。测试程序的具体方法包括:

(1)输入数据和操作,例如添加、删除、更新、查询、报表等;

(2)检查程序是否输出了正确的结果;

(3)检查程序是否能够正常退出。

在测试期间,我们可以使用不同的数据进行测试,以确保程序的正确性和稳定性。如果发现程序存在问题,需要对代码进行修改和调试。

  1. 总结

本文介绍了如何使用C++编写一个简单的记账本程序,包括确定程序功能和需求、设计数据结构和算法、编写代码和测试程序。这个程序具有添加、删除、更新、查询、报表和计算余额等功能,能够帮助人们更好地管理自己的财务状况。通过学习本文内容,读者可以更深入地理解C++语言的应用和程序设计的基本方法,提高自己的编程水平。

以上是如何通过C++编写一个简单的记账本程序?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1662
14
CakePHP 教程
1419
52
Laravel 教程
1311
25
PHP教程
1261
29
C# 教程
1234
24
C#与C:历史,进化和未来前景 C#与C:历史,进化和未来前景 Apr 19, 2025 am 12:07 AM

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

Golang和C:并发与原始速度 Golang和C:并发与原始速度 Apr 21, 2025 am 12:16 AM

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

vscode在哪写代码 vscode在哪写代码 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中编写代码简单易行,只需安装 VSCode、创建项目、选择语言、创建文件、编写代码、保存并运行即可。VSCode 的优点包括跨平台、免费开源、强大功能、扩展丰富,以及轻量快速。

表演竞赛:Golang vs.C 表演竞赛:Golang vs.C Apr 16, 2025 am 12:07 AM

Golang和C 在性能竞赛中的表现各有优势:1)Golang适合高并发和快速开发,2)C 提供更高性能和细粒度控制。选择应基于项目需求和团队技术栈。

Python与C:学习曲线和易用性 Python与C:学习曲线和易用性 Apr 19, 2025 am 12:20 AM

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

Golang和C:性能的权衡 Golang和C:性能的权衡 Apr 17, 2025 am 12:18 AM

Golang和C 在性能上的差异主要体现在内存管理、编译优化和运行时效率等方面。1)Golang的垃圾回收机制方便但可能影响性能,2)C 的手动内存管理和编译器优化在递归计算中表现更为高效。

Golang vs.C:性能和速度比较 Golang vs.C:性能和速度比较 Apr 21, 2025 am 12:13 AM

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

vscode如何执行代码 vscode如何执行代码 Apr 15, 2025 pm 09:51 PM

在 VS Code 中执行代码只需六个步骤:1. 打开项目;2. 创建和编写代码文件;3. 打开终端;4. 导航到项目目录;5. 使用适当的命令执行代码;6. 查看输出。

See all articles