📜  银行管理系统菜单驱动程序(1)

📅  最后修改于: 2023-12-03 15:28:33.595000             🧑  作者: Mango

银行管理系统菜单驱动程序

本文将介绍一种简单的银行管理系统菜单驱动程序。该程序可以方便地管理银行账户,包括添加、删除和查询账户等功能。程序运行时,用户可以通过菜单驱动程序来选择需要的操作,从而快速完成对账户的管理。

功能介绍
添加账户

用户可以输入账户信息,包括账户名、余额等信息,将该账户添加到系统中。添加账户时,系统会自动生成一个唯一的账户号,并将账户信息存储在系统中。

删除账户

用户可以按账户号删除指定账户。在删除账户时,系统会检查该账户是否存在,如果存在,则将该账户从系统中删除。

查询账户

用户可以按账户号或账户名查询指定账户的信息。查询账户时,系统会根据用户输入的账户号或账户名来查找指定账户,并将该账户的信息显示在屏幕上。

程序说明

该程序由以下几个部分组成:

数据结构

程序使用一个结构体来表示银行账户的信息,该结构体包括账户号、账户名、余额等字段。程序还定义了一个数组来存储所有的银行账户信息。

struct account {
    int id;             // 账户号
    char name[30];      // 账户名
    float balance;      // 余额
};

struct account accounts[100];   // 存储所有账户信息的数组
菜单驱动函数

程序使用一个菜单驱动函数来处理用户的输入,并根据用户输入的选项调用相应的函数来完成相应的操作。

以下是该函数的代码:

void menu() {
    int choice;
    do {
        printf("\nBank account management system\n");
        printf("1. Add an account\n");
        printf("2. Delete an account\n");
        printf("3. Search an account\n");
        printf("4. Exit\n");
        printf("Enter your choice (1-4): ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                add_account();
                break;
            case 2:
                delete_account();
                break;
            case 3:
                search_account();
                break;
            case 4:
                printf("Goodbye!\n");
                break;
            default:
                printf("Invalid choice. Please enter a number between 1 and 4.\n");
                break;
        }
    } while (choice != 4);
}
添加账户函数

该函数实现了添加账户的功能,用户可以输入账户信息,并将该账户添加到系统中。函数将自动生成一个唯一的账户号,并将账户信息存储到数组中。

以下是该函数的代码:

void add_account() {
    struct account acc;
    printf("Enter name: ");
    scanf("%s", acc.name);
    printf("Enter balance: ");
    scanf("%f", &acc.balance);

    // 生成一个唯一的账户号
    int id = rand() % 10000 + 1;
    while (find_account_by_id(id) != NULL) {
        id = rand() % 10000 + 1;
    }
    acc.id = id;

    // 将账户信息存储到数组中
    accounts[num_accounts++] = acc;
    printf("Account added successfully. Account number is %d.\n", acc.id);
}
删除账户函数

该函数实现了删除账户的功能,用户可以按账户号删除指定账户。删除账户时,系统会检查该账户是否存在,如果存在,则将该账户从系统中删除。

以下是该函数的代码:

void delete_account() {
    int id;
    printf("Enter account number to delete: ");
    scanf("%d", &id);
    struct account *acc = find_account_by_id(id);
    if (acc == NULL) {
        printf("Account not found.\n");
    } else {
        // 将该账户从数组中删除,并更新账户数量
        int index = acc - accounts;
        for (int i = index; i < num_accounts - 1; i++) {
            accounts[i] = accounts[i + 1];
        }
        num_accounts--;
        printf("Account deleted successfully.\n");
    }
}
查询账户函数

该函数实现了查询账户的功能,用户可以按账户号或账户名查询指定账户的信息。查询账户时,系统会根据用户输入的账户号或账户名来查找指定账户,并将该账户的信息显示在屏幕上。

以下是该函数的代码:

void search_account() {
    int choice;
    printf("Search by:\n");
    printf("1. Account number\n");
    printf("2. Account name\n");
    printf("Enter your choice (1-2): ");
    scanf("%d", &choice);
    if (choice == 1) {
        int id;
        printf("Enter account number to search: ");
        scanf("%d", &id);
        struct account *acc = find_account_by_id(id);
        if (acc == NULL) {
            printf("Account not found.\n");
        } else {
            print_account(acc);
        }
    } else if (choice == 2) {
        char name[30];
        printf("Enter account name to search: ");
        scanf("%s", name);
        struct account *acc = find_account_by_name(name);
        if (acc == NULL) {
            printf("Account not found.\n");
        } else {
            print_account(acc);
        }
    } else {
        printf("Invalid choice. Please enter 1 or 2.\n");
    }
}

struct account* find_account_by_id(int id) {
    for (int i = 0; i < num_accounts; i++) {
        if (accounts[i].id == id) {
            return &accounts[i];
        }
    }
    return NULL;
}

struct account* find_account_by_name(char *name) {
    for (int i = 0; i < num_accounts; i++) {
        if (strcmp(accounts[i].name, name) == 0) {
            return &accounts[i];
        }
    }
    return NULL;
}

void print_account(struct account *acc) {
    printf("Account number: %d\n", acc->id);
    printf("Account name: %s\n", acc->name);
    printf("Balance: %f\n", acc->balance);
}
完整代码

完整代码如下:

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

struct account {
    int id;             // 账户号
    char name[30];      // 账户名
    float balance;      // 余额
};

struct account accounts[100];   // 存储所有账户信息的数组
int num_accounts = 0;           // 账户数量

void add_account();
void delete_account();
void search_account();
struct account* find_account_by_id(int id);
struct account* find_account_by_name(char *name);
void print_account(struct account *acc);
void menu();

int main() {
    menu();
    return 0;
}

void menu() {
    int choice;
    do {
        printf("\nBank account management system\n");
        printf("1. Add an account\n");
        printf("2. Delete an account\n");
        printf("3. Search an account\n");
        printf("4. Exit\n");
        printf("Enter your choice (1-4): ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                add_account();
                break;
            case 2:
                delete_account();
                break;
            case 3:
                search_account();
                break;
            case 4:
                printf("Goodbye!\n");
                break;
            default:
                printf("Invalid choice. Please enter a number between 1 and 4.\n");
                break;
        }
    } while (choice != 4);
}

void add_account() {
    struct account acc;
    printf("Enter name: ");
    scanf("%s", acc.name);
    printf("Enter balance: ");
    scanf("%f", &acc.balance);

    // 生成一个唯一的账户号
    int id = rand() % 10000 + 1;
    while (find_account_by_id(id) != NULL) {
        id = rand() % 10000 + 1;
    }
    acc.id = id;

    // 将账户信息存储到数组中
    accounts[num_accounts++] = acc;
    printf("Account added successfully. Account number is %d.\n", acc.id);
}

void delete_account() {
    int id;
    printf("Enter account number to delete: ");
    scanf("%d", &id);
    struct account *acc = find_account_by_id(id);
    if (acc == NULL) {
        printf("Account not found.\n");
    } else {
        // 将该账户从数组中删除,并更新账户数量
        int index = acc - accounts;
        for (int i = index; i < num_accounts - 1; i++) {
            accounts[i] = accounts[i + 1];
        }
        num_accounts--;
        printf("Account deleted successfully.\n");
    }
}

void search_account() {
    int choice;
    printf("Search by:\n");
    printf("1. Account number\n");
    printf("2. Account name\n");
    printf("Enter your choice (1-2): ");
    scanf("%d", &choice);
    if (choice == 1) {
        int id;
        printf("Enter account number to search: ");
        scanf("%d", &id);
        struct account *acc = find_account_by_id(id);
        if (acc == NULL) {
            printf("Account not found.\n");
        } else {
            print_account(acc);
        }
    } else if (choice == 2) {
        char name[30];
        printf("Enter account name to search: ");
        scanf("%s", name);
        struct account *acc = find_account_by_name(name);
        if (acc == NULL) {
            printf("Account not found.\n");
        } else {
            print_account(acc);
        }
    } else {
        printf("Invalid choice. Please enter 1 or 2.\n");
    }
}

struct account* find_account_by_id(int id) {
    for (int i = 0; i < num_accounts; i++) {
        if (accounts[i].id == id) {
            return &accounts[i];
        }
    }
    return NULL;
}

struct account* find_account_by_name(char *name) {
    for (int i = 0; i < num_accounts; i++) {
        if (strcmp(accounts[i].name, name) == 0) {
            return &accounts[i];
        }
    }
    return NULL;
}

void print_account(struct account *acc) {
    printf("Account number: %d\n", acc->id);
    printf("Account name: %s\n", acc->name);
    printf("Balance: %f\n", acc->balance);
}
总结

本文介绍了一种简单的银行管理系统菜单驱动程序,包括添加账户、删除账户和查询账户等功能。该程序可以方便地管理银行账户,并通过菜单驱动程序来处理用户的输入,快速完成对账户的管理。该程序代码简单,易于理解和修改。