酷代码 AI
菜单
服务商

C语言实现:简单银行账户类的设计与功能实现

```c #include <stdio.h> #include <stdlib.h> typedef struct BankAccount { int balance; } BankAccount; BankAccount* openaccount(int initial_balance) { BankAccount* account = (BankAccount*)malloc(sizeof(BankAccount)); if (account == NULL) { printf("Error: Unable to allocate memory for the account.\n"); exit(1); } account->balance = initial_balance; return account; } void deposit(BankAccount* account, int amount) { if (amount <= 0) { printf("Error: Deposit amount must be positive.\n"); return; } account->balance += amount; } void withdraw(BankAccount* account, int amount) { if (amount <= 0) { printf("Error: Withdrawal amount must be positive.\n"); return; } if (account->balance < amount) { printf("Error: Insufficient balance.\n"); return; } account->balance -= amount; } int getbalance(BankAccount* account) { return account->balance; } int main() { BankAccount* my_account = openaccount(1000); // 开户,初始余额为1000 deposit(my_account, 500); // 存款500 printf("Current balance: %d\n", getbalance(my_account)); // 查询余额 withdraw(my_account, 200); // 取款200 printf("Current balance: %d\n", getbalance(my_account)); // 查询余额 free(my_account); // 释放内存 return 0; } ``` ######[AI写代码神器 | 436点数解答 | 2024-08-08 14:47:08]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]