Home  >  Article  >  Backend Development  >  How to write a simple accounting program in C++?

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

PHPz
PHPzOriginal
2023-11-03 09:52:511059browse

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