search
HomeBackend DevelopmentC++How to write a simple accounting program in C++?
How to write a simple accounting program in C++?Nov 03, 2023 am 09:52 AM
programmingc++Pocket book

How to write a simple accounting program in C++?

This article will introduce how to use C to write a simple accounting program. As the cost of living continues to rise, more and more people are beginning to pay attention to their financial status. Using a ledger can record income and expenses and improve financial management skills. The advantage of C language is its efficiency and portability, which makes it very suitable for writing such programs.

1. Determine the program functions and requirements

Before writing the program, we first need to clarify the functions and requirements the program wants to achieve. A simple accounting book program needs to have the following functions:

(1) Be able to record the amount and type of each expenditure and income, and record the time;

(2) Be able to calculate the overall income and expenditure, including the total number of income and expenditure;

(3) Ability to generate reports and count the total expenditure and income of each type;

(4) Ability to add, delete, modify and check records.

  1. Design data structures and algorithms

In the program, we need to use data structures to store each record. Commonly used data structures include linear tables, stacks and queues wait. Here, we choose to use a linear table to store each record. Each record includes the following information:

(1) The unique ID number of the record;

(2) The timestamp of the record ;

(3) Record type, including income and expenses;

(4) Record amount;

(5) Record details.

For the algorithm, we need to design functions to implement various operations, such as adding, deleting, updating, querying and statistics. These operations require access to the recorded data, and also need to calculate the total revenue and expenditure and type classification. sum and generate corresponding reports.

  1. Writing code

Before we start writing code, we need to make some preparations, including:

(1) Choose an integrated development environment ( IDE), such as Visual Studio;

(2) Learn basic C syntax and use STL library for programming;

(3) Design program classes and functions.

In C, we can use classes to represent accounting programs. This class can contain various member functions and member variables, as well as corresponding access control characters.

The following is a code example of a simple accounting program:

#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. Test program

After completing the code writing, we need to test the program carry out testing. Specific methods for testing the program include:

(1) Enter data and operations, such as adding, deleting, updating, querying, reporting, etc.;

(2) Check whether the program outputs correct results ;

(3) Check whether the program can exit normally.

During testing, we can use different data for testing to ensure the correctness and stability of the program. If problems are found in the program, the code needs to be modified and debugged.

  1. Summary

This article introduces how to use C to write a simple accounting program, including determining program functions and requirements, designing data structures and algorithms, writing code and test program. This program has functions such as adding, deleting, updating, querying, reporting and calculating balances, which can help people better manage their financial status. By studying the contents of this article, readers can have a deeper understanding of the application of C language and the basic methods of programming, and improve their programming skills.

The above is the detailed content of How to write a simple accounting program in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
计算机编程中常见的if语句是什么计算机编程中常见的if语句是什么Jan 29, 2023 pm 04:31 PM

计算机编程中常见的if语句是条件判断语句。if语句是一种选择分支结构,它是依据明确的条件选择选择执行路径,而不是严格按照顺序执行,在编程实际运用中要根据程序流程选择适合的分支语句,它是依照条件的结果改变执行的程序;if语句的简单语法“if(条件表达式){// 要执行的代码;}”。

Python编程:详解命名元组(namedtuple)的使用要点Python编程:详解命名元组(namedtuple)的使用要点Apr 11, 2023 pm 09:22 PM

前言本文继续来介绍Python集合模块,这次主要简明扼要的介绍其内的命名元组,即namedtuple的使用。闲话少叙,我们开始——记得点赞、关注和转发哦~ ^_^创建命名元组Python集合中的命名元组类namedTuples为元组中的每个位置赋予意义,并增强代码的可读性和描述性。它们可以在任何使用常规元组的地方使用,且增加了通过名称而不是位置索引方式访问字段的能力。其来自Python内置模块collections。其使用的常规语法方式为:import collections XxNamedT

如何在Go中进行图像处理?如何在Go中进行图像处理?May 11, 2023 pm 04:45 PM

作为一门高效的编程语言,Go在图像处理领域也有着不错的表现。虽然Go本身的标准库中没有提供专门的图像处理相关的API,但是有一些优秀的第三方库可以供我们使用,比如GoCV、ImageMagick和GraphicsMagick等。本文将重点介绍使用GoCV进行图像处理的方法。GoCV是一个高度依赖于OpenCV的Go语言绑定库,其

PHP8.0中的邮件库PHP8.0中的邮件库May 14, 2023 am 08:49 AM

最近,PHP8.0发布了一个新的邮件库,使得在PHP中发送和接收电子邮件变得更加容易。这个库具有强大的功能,包括构建电子邮件,发送电子邮件,解析电子邮件,获取附件和解决电子邮件获得卡住的问题。在很多项目中,我们都需要使用电子邮件来进行通信和一些必备的业务操作。而PHP8.0中的邮件库可以让我们轻松地实现这一点。接下来,我们将探索这个新的邮件库,并了解如何在我

PHP8.0中的DOMDocumentPHP8.0中的DOMDocumentMay 14, 2023 am 08:18 AM

随着PHP8.0的发布,DOMDocument作为PHP内置的XML解析库,也有了新的变化和增强。DOMDocument在PHP中的重要性不言而喻,尤其在处理XML文档方面,它的功能十分强大,而且使用起来也十分简单。本文将介绍PHP8.0中DOMDocument的新特性和应用。一、DOMDocument概述DOM(DocumentObjectModel)

学Python,还不知道main函数吗学Python,还不知道main函数吗Apr 12, 2023 pm 02:58 PM

Python 中的 main 函数充当程序的执行点,在 Python 编程中定义 main 函数是启动程序执行的必要条件,不过它仅在程序直接运行时才执行,而在作为模块导入时不会执行。要了解有关 Python main 函数的更多信息,我们将从如下几点逐步学习:什么是 Python 函数Python 中 main 函数的功能是什么一个基本的 Python main() 是怎样的Python 执行模式Let’s get started什么是 Python 函数相信很多小伙伴对函数都不陌生了,函数是可

PHP8.0中的Symbol类型PHP8.0中的Symbol类型May 14, 2023 am 08:39 AM

PHP8.0是PHP语言的最新版本,自发布以来已经引发了广泛的关注和争议。其中,最引人瞩目的新特性之一就是Symbol类型。Symbol类型是PHP8.0中新增的一种数据类型,它类似于JavaScript中的Symbol类型,可用于表示独一无二的值。这意味着,两个Symbol类型的值即使完全相同,它们也是不相等的。Symbol类型的使用可以避免在不同的代码段

为拯救童年回忆,开发者决定采用古法编程:用Flash高清重制了一款游戏为拯救童年回忆,开发者决定采用古法编程:用Flash高清重制了一款游戏Apr 11, 2023 pm 10:16 PM

两年多前,Adobe 发布了一则引人关注的公告 —— 将在 2020 年 12 月 31 日终止支持 Flash,宣告了一个时代的结束。一晃两年过去了,Adobe 早已从官方网站中删除了 Flash Player 早期版本的所有存档,并阻止基于 Flash 的内容运行。微软也已经终止对 Adobe Flash Player 的支持,并禁止其在任何 Microsoft 浏览器上运行。Adobe Flash Player 组件于 2021 年 7 月通过 Windows 更新永久删除。当 Flash

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor