C++ で簡単な会計プログラムを作成するにはどうすればよいですか?

PHPz
リリース: 2023-11-03 09:52:51
オリジナル
1049 人が閲覧しました

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!