| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 659 人关注过本帖
标题:有人能看看我这个代码为什么内存不足吗
取消只看楼主 加入收藏
羽笙
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-6-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
有人能看看我这个代码为什么内存不足吗
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

// 存储账户信息的结构体
struct AccountInfo {
    string name;
    string password;
    string phone;
    double balance;
    bool isLost;
};

// 存储交易记录的结构体
struct Transaction {
    string type;
    double amount;
    double fee;
};

class Account {
private:
    AccountInfo info;
    vector<Transaction> transactions;

public:
    Account(string name, string password, string phone, double balance) {
        info = {name, password, phone, balance, false};
    }
    ~Account() {}

    // 公有的交易记录访问接口
    const vector<Transaction>& get_transactions() const { return transactions; }

    // 存款
    bool deposit(double amount) {
        if (amount > 0) {
            info.balance += amount;
            Transaction t = {"Deposit", amount, 0};
            transactions.push_back(t);
            return true;
        }
        return false;
    }

    // 取款
    bool withdraw(double amount, double fee) {
        if (amount + fee <= info.balance && amount > 0 && fee >= 0) {
            info.balance -= (amount + fee);
            Transaction t = {"Withdraw", amount, fee};
            transactions.push_back(t);
            return true;
        }
        return false;
    }

    // 查询余额
    double getBalance() const {
        return info.balance;
    }

    // 查询交易记录
    void printTransactions() const {
        cout << "Transaction history:\n";
        for (const auto& t : transactions) {
            cout << "  " << t.type << " " << t.amount << " fee: " << t.fee << endl;
        }
    }

    // 判断账户是否挂失
    bool isLost() const {
        return info.isLost;
    }

    // 设置账户挂失状态
    void setLost(bool lost) {
        info.isLost = lost;
    }

    // 获取账户信息
    AccountInfo getInfo() const {
        return info;
    }
};

// 保存所有账户的信息,以文本文件的格式存储
void saveAccounts(const vector<Account>& accounts) {
    ofstream outfile("accounts.txt");
    for (const auto& acc : accounts) {
        outfile << acc.getInfo().name << " " << acc.getInfo().password << " "
                << acc.getInfo().phone << " " << acc.getInfo().balance << " "
                << acc.isLost() << endl;
    }
    outfile.close();
}

// 读取账户信息文件,返回所有账户信息
vector<AccountInfo> readAccountInfo() {
    vector<AccountInfo> accounts;
    ifstream infile("accounts.txt");
    while (true) {
        string name, password, phone, isLostStr;
        double balance;
        infile >> name >> password >> phone >> balance >> isLostStr;
        if (infile.eof()) break;
        bool isLost = (isLostStr == "1");
        AccountInfo info = {name, password, phone, balance, isLost};
        accounts.push_back(info);
    }
    infile.close();
    return accounts;
}

// 加载所有账户信息
vector<Account> loadAccounts() {
    vector<Account> accounts;
    vector<AccountInfo> accountInfos = readAccountInfo();
    for (const auto& info : accountInfos) {
        Account account(info.name, info.password, info.phone, info.balance);
        if (info.isLost) {
            account.setLost(true);
        }
        accounts.push_back(account);
    }
    return accounts;
}

// 保存交易记录,以文本文件的格式存储
void saveTransactions(const vector<Account>& accounts) {
    ofstream outfile("transactions.txt");
    for (const auto& acc : accounts) {
        const vector<Transaction>& transactions = acc.get_transactions();
        for (const auto& t : transactions) {
            outfile << acc.getInfo().name << " " << t.type << " " << t.amount << " " << t.fee << endl;
        }
    }
    outfile.close();
}

// 读取交易记录文件,返回所有交易记录
vector<Transaction> readTransactions() {
    vector<Transaction> transactions;
    ifstream infile("transactions.txt");
    while (true) {
        string name, type;
        double amount, fee;
        infile >> name >> type >> amount >> fee;
        if (infile.eof()) break;
        Transaction t = {type, amount, fee};
        transactions.push_back(t);
    }
    infile.close();
    return transactions;
}

// 加载所有交易记录
void loadTransactions(vector<Account>& accounts) {
    vector<Transaction> transactions = readTransactions();
    for (auto& acc : accounts) {
        for (const auto& t : transactions) {
            string name = acc.getInfo().name;
            if (t.type == "Deposit" && name == acc.getInfo().name) {
                acc.deposit(t.amount);
            } else if (t.type == "Withdraw" && name == acc.getInfo().name) {
                acc.withdraw(t.amount, t.fee);
            }
        }
    }
}

int main() {
    // 加载账户和交易记录
    vector<Account> accounts = loadAccounts();
    loadTransactions(accounts);

    // 输出菜单
    cout << "Menu:\n"
         << "  1. Create new account.\n"
         << "  2. Close account.\n"
         << "  3. Report lost.\n"
         << "  4. Deposit.\n"
         << "  5. Withdraw.\n"
         << "  6. Query balance.\n"
         << "  7. Transaction history.\n"
         << "  8. Exit.\n";

    while (true) {
        int choice;
        cout << "Please enter your choice: ";
        cin >> choice;

        if (choice == 1) { // 开户
            string name, password, phone;
            double balance;
            cout << "Please enter your name: ";
            cin >> name;
            cout << "Please enter your password: ";
            cin >> password;
            cout << "Please enter your phone: ";
            cin >> phone;
            cout << "Please enter the initial deposit amount: ";
            cin >> balance;

            Account account(name, password, phone, balance);
            accounts.push_back(account);
            saveAccounts(accounts);
        } else if (choice == 2) { // 销户
            string name;
            cout << "Please enter your name: ";
            cin >> name;

            for (auto it = accounts.begin(); it != accounts.end(); ++it) {
                if (it->getInfo().name == name) {
                    accounts.erase(it);
                    break;
                }
            }
            saveAccounts(accounts);
        } else if (choice == 3) { // 挂失
            string name;
            cout << "Please enter your name: ";
            cin >> name;

            for (auto& acc : accounts) {
                if (acc.getInfo().name == name) {
                    acc.setLost(true);
                    break;
                }
            }
            saveAccounts(accounts);
        } else if (choice == 4) { // 存款
            string name;
            double amount;
            cout << "Please enter your name: ";
            cin >> name;

            auto it = find_if(accounts.begin(), accounts.end(), [&](const Account& acc) {
                return acc.getInfo().name == name;
            });
            if (it == accounts.end()) {
                cout << "Account does not exist.\n";
            } else if (it->isLost()) {
                cout << "Account is lost.\n";
            } else {
                cout << "Please enter the deposit amount: ";
                cin >> amount;
                if (it->deposit(amount)) {
                    saveTransactions(accounts);
                    cout << "Deposit successful.\n";
                } else {
                    cout << "Deposit failed.\n";
                }
            }
        } else if (choice == 5) { // 取款
            string name;
            double amount, fee;
            cout << "Please enter your name: ";
            cin >> name;

            auto it = find_if(accounts.begin(), accounts.end(), [&](const Account& acc) {
                return acc.getInfo().name == name;
            });
            if (it == accounts.end()) {
                cout << "Account does not exist.\n";
            } else if (it->isLost()) {
                cout << "Account is lost.\n";
            } else {
                cout << "Please enter the withdraw amount: ";
                cin >> amount;
                cout << "Please enter the fee amount: ";
                cin >> fee;
                if (it->withdraw(amount, fee)) {
                    saveTransactions(accounts);
                    cout << "Withdraw successful.\n";
                } else {
                    cout << "Withdraw failed.\n";
                }
            }
        } else if (choice == 6) { // 查询余额
            string name;
            cout << "Please enter your name: ";
            cin >> name;

            auto it = find_if(accounts.begin(), accounts.end(), [&](const Account& acc) {
                return acc.getInfo().name == name;
            });
            if (it == accounts.end()) {
                cout << "Account does not exist.\n";
            } else if (it->isLost()) {
                cout << "Account is lost.\n";
            } else {
                cout << "Your balance is: " << it->getBalance() << endl;
            }
        } else if (choice == 7) { // 查询历史交易记录
            string name;
            cout << "Please enter your name: ";
            cin >> name;

            auto it = find_if(accounts.begin(), accounts.end(), [&](const Account& acc) {
                return acc.getInfo().name == name;
            });
            if (it == accounts.end()) {
                cout << "Account does not exist.\n";
            } else {
                it->printTransactions();
            }
        } else if (choice == 8) { // 退出程序
            cout << "Goodbye.\n";
            break;
        } else { // 非法输入
            cout << "Invalid input. Please try again.\n";
        }
    }

    return 0;
}
运行出来就是现实内存不足,刚学没多久,求指点,拜托
搜索更多相关主题的帖子: amount cout const name string 
2023-06-03 20:34
快速回复:有人能看看我这个代码为什么内存不足吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024613 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved