C++ 实现有序表折半查找算法及银行账户管理系统

一、用 C++ 语言编程实现有序表的折半查找算法

下面是用 C++ 语言实现有序表的折半查找算法的代码:

#include <iostream>
using namespace std;

int binarySearch(int arr[], int left, int right, int target) {
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            return mid;
        }
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int target = 5;
    int size = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, 0, size - 1, target);
    if (result == -1) {
        cout << 'Element not found' << endl;
    } else {
        cout << 'Element found at index ' << result << endl;
    }
    return 0;
}

二、设计一个 Bank 类,实现银行某账户的资金往来账目管理

程序要求完成以下操作:

  1. 创建账户:包括账号、创建日期、余额。
  2. 存钱:执行存钱操作,并记录存钱日期和存钱数目。
  3. 取钱:执行取钱操作,并记录取钱日期和取钱数目。
  4. 查询交易明细:查询近一个月的账户交易记录。

Bank 类的实现如下:

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;

class Bank {
private:
    string accountNumber;
    string createDate;
    double balance;
    vector<pair<string, double>> transactions;

public:
    Bank(string accountNumber, string createDate, double balance) {
        this->accountNumber = accountNumber;
        this->createDate = createDate;
        this->balance = balance;
    }

    void deposit(double amount) {
        balance += amount;
        time_t now = time(0);
        tm *ltm = localtime(&now);
        string date = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
        transactions.push_back(make_pair(date, amount));
    }

    void withdraw(double amount) {
        if (amount > balance) {
            cout << 'Insufficient balance' << endl;
        } else {
            balance -= amount;
            time_t now = time(0);
            tm *ltm = localtime(&now);
            string date = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
            transactions.push_back(make_pair(date, -amount));
        }
    }

    void printTransactions() {
        time_t now = time(0);
        tm *ltm = localtime(&now);
        string currentDate = to_string(1900 + ltm->tm_year) + '-' + to_string(1 + ltm->tm_mon) + '-' + to_string(ltm->tm_mday);
        cout << 'Recent transactions:' << endl;
        for (int i = transactions.size() - 1; i >= 0; i--) {
            string transactionDate = transactions[i].first;
            double transactionAmount = transactions[i].second;
            if (currentDate == transactionDate || currentDate < transactionDate) {
                cout << transactionDate << ': ';
                if (transactionAmount > 0) {
                    cout << 'Deposit: ' << transactionAmount << endl;
                } else {
                    cout << 'Withdraw: ' << -transactionAmount << endl;
                }
            }
        }
    }
};

int main() {
    Bank bank('123456789', '2021-01-01', 1000);
    bank.deposit(500);
    bank.withdraw(200);
    bank.printTransactions();
    return 0;
}

这个程序创建了一个 Bank 类,包括账号、创建日期和余额。可以使用 deposit() 方法进行存钱操作,使用 withdraw() 方法进行取钱操作,并记录存取钱的日期和金额。可以使用 printTransactions() 方法查询近一个月的账户交易记录。在 main() 函数中,演示了如何使用 Bank 类进行操作,并输出交易明细。

C++ 实现有序表折半查找算法及银行账户管理系统

原文地址: https://www.cveoy.top/t/topic/f3Zv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录