首頁 後端開發 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)

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 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

表演競賽:Golang vs.C 表演競賽:Golang vs.C Apr 16, 2025 am 12:07 AM

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

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

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

vscode怎麼在終端運行程序 vscode怎麼在終端運行程序 Apr 15, 2025 pm 06:42 PM

在 VS Code 中,可以通過以下步驟在終端運行程序:準備代碼和打開集成終端確保代碼目錄與終端工作目錄一致根據編程語言選擇運行命令(如 Python 的 python your_file_name.py)檢查是否成功運行並解決錯誤利用調試器提升調試效率

Python與C:學習曲線和易用性 Python與C:學習曲線和易用性 Apr 19, 2025 am 12:20 AM

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

在 visual studio code 中使用 c 嗎 在 visual studio code 中使用 c 嗎 Apr 15, 2025 pm 08:03 PM

在 VS Code 中編寫 C 語言不僅可行,而且高效優雅。關鍵在於安裝優秀的 C/C 擴展,它提供代碼補全、語法高亮和調試等功能。 VS Code 的調試功能可幫助你快速定位 bug,而 printf 輸出是老式但有效的調試方法。此外,動態內存分配時應檢查返回值並釋放內存以防止內存洩漏,調試這些問題在 VS Code 中很方便。雖然 VS Code 無法直接幫助進行性能優化,但它提供了一個良好的開發環境,便於分析代碼性能。良好的編程習慣、可讀性和可維護性也至關重要。總之,VS Code 是一

See all articles