C语言编程题,一个简易的银行账户管理系统,这个系统应该能够实现基本的账户操作,如创建账户、存款、取款、查询余额和显示账户信息等功能。将代码分成不同的模块,提高代码的可读性和可维护性。

1、定义账户信息

设计一个银行账户管理系统时,我们需要明确哪些信息是必不可少的。为了方便管理这些信息,我们可以使用C语言的结构体来定义一个账户。使用typedef struct定义了一个BankAccount结构体,包含账户号码、名字和余额。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

2、创建账户

将输入的账户信息,包括账户号码和账户持有人姓名。然后将这些信息存储到 Account 结构体的实例中,并初始化账户的余额为0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void createAccount(Account accounts[], int *accountCount) {
    if (*accountCount >= MAX_ACCOUNTS) {
        printf("无法创建更多账户,已达到最大账户数量。\n");
        return;
    }

    Account *account = &accounts[*accountCount];

    printf("请输入账户号码: ");
    scanf("%d", &account->accountNumber);

    printf("请输入账户持有人姓名: ");
    scanf("%s", account->name);

    account->balance = 0.0;
    (*accountCount)++;

    printf("账户创建成功!\n");
}

int main() {
  printf("Hello, World!");
  return 0;
}

3、帐户存款

定义了一个deposit函数,用于处理存款操作。用户输入需要存款的账户号。在已有的账户数组中查找与输入账户号匹配的账户,用户输入存款金额。将存款金额加到对应账户的余额中,显示存款成功的消息和新的账户余额。

#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void deposit(Account accounts[], int accountCount) {
    int accountNumber;
    double amount;
    printf("请输入存款账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("请输入存款金额: ");
            scanf("%lf", &amount);
            accounts[i].balance += amount;
            printf("存款成功!当前余额为: %.2f\n", accounts[i].balance);
            return;
        }
    }

    printf("账户未找到。\n");
}

int main() {
  printf("Hello, World!");
  return 0;
}

4、账户取款

用户输入需要取款的账户号,在已有的账户数组中查找与输入账户号匹配的账户。 用户输入取款金额,检查账户余额是否足够,如果余额充足,则从账户余额中扣除取款金额,显示取款成功或余额不足的消息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void withdraw(Account accounts[], int accountCount) {
    int accountNumber;
    double amount;
    printf("请输入取款账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("请输入取款金额: ");
            scanf("%lf", &amount);
            if (accounts[i].balance >= amount) {
                accounts[i].balance -= amount;
                printf("取款成功!当前余额为: %.2f\n", accounts[i].balance);
            } else {
                printf("余额不足。\n");
            }
            return;
        }
    }

    printf("账户未找到。\n");
}

int main() {
  printf("Hello, World!");
  return 0;
}

5、查询余额

查询余额是银行账户管理系统中最基本的功能之一。用户可以通过输入账户号来查询对应的账户余额。 用户输入需要查询余额的账户号,在已有的账户数组中查找与输入账户号匹配的账户,如果找到账户,则输出该账户的余额。如果未找到账户,则提示“账户未找到”。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void checkBalance(Account accounts[], int accountCount) {
    int accountNumber;
    printf("请输入查询余额的账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("账户余额为: %.2f\n", accounts[i].balance);
            return;
        }
    }

    printf("账户未找到。\n");
}

int main() {
  printf("Hello, World!");
  return 0;
}

6、显示帐户信息

displayAccounts函数用于显示所有账户的信息,包括账户号码、账户持有人姓名和账户余额。如果没有任何账户信息,则输出提示信息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void displayAccounts(Account accounts[], int accountCount) {
    if (accountCount == 0) {
        printf("没有账户信息。\n");
        return;
    }

    printf("\n所有账户信息:\n");
    for (int i = 0; i < accountCount; i++) {
        printf("账户号码: %d\n", accounts[i].accountNumber);
        printf("账户持有人姓名: %s\n", accounts[i].name);
        printf("账户余额: %.2f\n", accounts[i].balance);
        printf("----------------------------\n");
    }
}

int main() {
  printf("Hello, World!");
  return 0;
}

7、完整代码

一个简易的银行账户管理系统,可以根据自己的需求进行扩展和完善。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ACCOUNTS 100
#define NAME_LENGTH 50

typedef struct {
    int accountNumber;
    char name[NAME_LENGTH];
    double balance;
} Account;

void createAccount(Account accounts[], int *accountCount) {
    if (*accountCount >= MAX_ACCOUNTS) {
        printf("无法创建更多账户,已达到最大账户数量。\n");
        return;
    }

    Account *account = &accounts[*accountCount];

    printf("请输入账户号码: ");
    scanf("%d", &account->accountNumber);

    printf("请输入账户持有人姓名: ");
    scanf("%s", account->name);

    account->balance = 0.0;
    (*accountCount)++;

    printf("账户创建成功!\n");
}

void deposit(Account accounts[], int accountCount) {
    int accountNumber;
    double amount;
    printf("请输入存款账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("请输入存款金额: ");
            scanf("%lf", &amount);
            accounts[i].balance += amount;
            printf("存款成功!当前余额为: %.2f\n", accounts[i].balance);
            return;
        }
    }

    printf("账户未找到。\n");
}

void withdraw(Account accounts[], int accountCount) {
    int accountNumber;
    double amount;
    printf("请输入取款账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("请输入取款金额: ");
            scanf("%lf", &amount);
            if (accounts[i].balance >= amount) {
                accounts[i].balance -= amount;
                printf("取款成功!当前余额为: %.2f\n", accounts[i].balance);
            } else {
                printf("余额不足。\n");
            }
            return;
        }
    }

    printf("账户未找到。\n");
}

void checkBalance(Account accounts[], int accountCount) {
    int accountNumber;
    printf("请输入查询余额的账户号码: ");
    scanf("%d", &accountNumber);

    for (int i = 0; i < accountCount; i++) {
        if (accounts[i].accountNumber == accountNumber) {
            printf("账户余额为: %.2f\n", accounts[i].balance);
            return;
        }
    }

    printf("账户未找到。\n");
}

void displayAccounts(Account accounts[], int accountCount) {
    if (accountCount == 0) {
        printf("没有账户信息。\n");
        return;
    }

    printf("\n所有账户信息:\n");
    for (int i = 0; i < accountCount; i++) {
        printf("账户号码: %d\n", accounts[i].accountNumber);
        printf("账户持有人姓名: %s\n", accounts[i].name);
        printf("账户余额: %.2f\n", accounts[i].balance);
        printf("----------------------------\n");
    }
}

int main() {
    Account accounts[MAX_ACCOUNTS];
    int accountCount = 0;
    int choice;

    while (1) {
        printf("\n银行账户管理系统菜单:\n");
        printf("1. 创建账户\n");
        printf("2. 存款\n");
        printf("3. 取款\n");
        printf("4. 查询余额\n");
        printf("5. 显示所有账户\n");
        printf("6. 退出\n");
        printf("请选择操作: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                createAccount(accounts, &accountCount);
                break;
            case 2:
                deposit(accounts, accountCount);
                break;
            case 3:
                withdraw(accounts, accountCount);
                break;
            case 4:
                checkBalance(accounts, accountCount);
                break;
            case 5:
                displayAccounts(accounts, accountCount);
                break;
            case 6:
                printf("退出系统。\n");
                exit(0);
            default:
                printf("无效的选择,请重试。\n");
        }
    }

    return 0;
}

推荐文档

相关文档

大家感兴趣的内容

随机列表