用C++写一个个人银行账户管理系统
时间: 2024-01-15 08:04:44 浏览: 88
c++银行账户管理系统
4星 · 用户满意度95%
好的,以下是一个简单的个人银行账户管理系统的C++代码,其中包含了添加账户、取款、存款、查询余额等基本功能:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Account {
string name;
int accountNumber;
double balance;
};
int main() {
int numAccounts = 0;
const int MAX_ACCOUNTS = 10;
Account accounts[MAX_ACCOUNTS];
while (true) {
int choice;
cout << "请选择操作:" << endl;
cout << "1. 添加账户" << endl;
cout << "2. 取款" << endl;
cout << "3. 存款" << endl;
cout << "4. 查询余额" << endl;
cout << "5. 退出" << endl;
cin >> choice;
switch (choice) {
case 1: {
if (numAccounts < MAX_ACCOUNTS) {
Account newAccount;
cout << "请输入账户名:" << endl;
cin >> newAccount.name;
cout << "请输入账户号码:" << endl;
cin >> newAccount.accountNumber;
cout << "请输入账户余额:" << endl;
cin >> newAccount.balance;
accounts[numAccounts] = newAccount;
numAccounts++;
cout << "账户添加成功!" << endl;
} else {
cout << "账户数量已达上限,无法添加新账户。" << endl;
}
break;
}
case 2: {
int accountNumber;
double amount;
cout << "请输入账户号码:" << endl;
cin >> accountNumber;
int i;
for (i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accountNumber) {
cout << "请输入取款金额:" << endl;
cin >> amount;
if (amount > accounts[i].balance) {
cout << "余额不足,无法取款。" << endl;
} else {
accounts[i].balance -= amount;
cout << "取款成功。" << endl;
}
break;
}
}
if (i == numAccounts) {
cout << "账户不存在。" << endl;
}
break;
}
case 3: {
int accountNumber;
double amount;
cout << "请输入账户号码:" << endl;
cin >> accountNumber;
int i;
for (i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accountNumber) {
cout << "请输入存款金额:" << endl;
cin >> amount;
accounts[i].balance += amount;
cout << "存款成功。" << endl;
break;
}
}
if (i == numAccounts) {
cout << "账户不存在。" << endl;
}
break;
}
case 4: {
int accountNumber;
cout << "请输入账户号码:" << endl;
cin >> accountNumber;
int i;
for (i = 0; i < numAccounts; i++) {
if (accounts[i].accountNumber == accountNumber) {
cout << "账户余额为:" << accounts[i].balance << endl;
break;
}
}
if (i == numAccounts) {
cout << "账户不存在。" << endl;
}
break;
}
case 5: {
cout << "感谢使用本系统,再见!" << endl;
return 0;
}
default: {
cout << "无效操作,请重新输入。" << endl;
break;
}
}
}
}
```
以上代码只是一个简单的示例,可能还需要根据具体需求进行修改和完善。
阅读全文