>  기사  >  백엔드 개발  >  C++로 간단한 회계 프로그램을 작성하는 방법은 무엇입니까?

C++로 간단한 회계 프로그램을 작성하는 방법은 무엇입니까?

PHPz
PHPz원래의
2023-11-03 09:52:511058검색

C++로 간단한 회계 프로그램을 작성하는 방법은 무엇입니까?

이 기사에서는 C++를 사용하여 간단한 회계 프로그램을 작성하는 방법을 소개합니다. 생활비가 계속 상승함에 따라 점점 더 많은 사람들이 재정 상태에 관심을 갖기 시작했습니다. 원장을 사용하면 수입과 지출을 기록하고 재무 관리 기능을 향상시킬 수 있습니다. C++ 언어의 장점은 효율성과 이식성이므로 이러한 프로그램을 작성하는 데 매우 적합합니다.

1. 프로그램 기능 및 요구 사항 결정

프로그램을 작성하기 전에 먼저 프로그램이 달성하려는 기능과 요구 사항을 명확히 해야 합니다.

(1) Be 모든 거래를 기록할 수 있습니다. 비용과 수입의 금액과 유형, 시간을 기록할 수 있습니다.

(2) 총 수입과 지출 횟수를 포함한 전체 수입과 지출을 계산할 수 있습니다.

(3) 생성 가능; 지출 및 수입 유형별 합계를 보고하고 계산합니다.

(4) 기록을 추가, 삭제, 수정 및 확인할 수 있습니다.

  1. 데이터 구조 및 알고리즘 설계

프로그램에서는 각 레코드를 저장하기 위해 데이터 구조를 사용해야 합니다. 일반적으로 사용되는 데이터 구조에는 선형 테이블, 스택, 큐 등이 있습니다. 여기서는 각 레코드를 저장하기 위해 선형 테이블을 선택합니다.

(1) 레코드의 고유 ID 번호

(2) 레코드 유형; , 수입과 지출을 포함하여

(4) 금액을 기록합니다.

(5) 세부 사항을 기록합니다.

알고리즘의 경우 추가, 삭제, 업데이트, 쿼리, 통계 등 다양한 작업을 구현하기 위한 기능을 설계해야 하며 이러한 작업은 기록된 데이터에 액세스해야 하며 총 수익 및 지출 유형도 계산해야 합니다. 분류 합계를 계산하고 해당 보고서를 생성합니다.

코드 작성
  1. 코드 작성을 시작하기 전에 다음을 포함한 몇 가지 준비를 해야 합니다.

(1) Visual Studio와 같은 통합 개발 환경(IDE)을 선택합니다.

(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) 프로그램이 올바른 결과를 출력하는지 확인합니다. ) 프로그램이 정상적으로 종료되는지 확인합니다.

테스트 중에 프로그램의 정확성과 안정성을 보장하기 위해 테스트에 다양한 데이터를 사용할 수 있습니다. 프로그램에서 문제가 발견되면 코드를 수정하고 디버깅해야 합니다.

요약

    이 기사에서는 프로그램 기능 및 요구 사항 결정, 데이터 구조 및 알고리즘 설계, 코드 작성 및 프로그램 테스트를 포함하여 C++를 사용하여 간단한 회계 프로그램을 작성하는 방법을 소개합니다. 이 프로그램에는 잔액 추가, 삭제, 업데이트, 조회, 보고 및 계산과 같은 기능이 있어 사람들이 자신의 재정 상태를 더 잘 관리하는 데 도움이 됩니다. 이 글의 내용을 연구함으로써 독자들은 C++ 언어의 응용과 프로그래밍의 기본 방법을 더 깊이 이해하고 프로그래밍 기술을 향상시킬 수 있습니다.

위 내용은 C++로 간단한 회계 프로그램을 작성하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.