You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言实现递增账户编号的函数问题求助

问题解决:C语言账号编号递增及客户信息存储问题

问题根源

你的代码存在两个核心问题:

  1. 未调用生成账号的函数:你编写了NewCustomer函数来生成递增账号,但NewAccount完全没调用它,导致TopAccountNumber静态变量从未生效,NewAccount里的customer.AccountNumber是未初始化的局部变量,输出的是随机值或0。
  2. 无客户信息存储机制:没有保存创建的客户数据,后续无法实现按账号删除的需求。

修正方案及代码

下面是修复后的完整代码,同时实现了账号递增生成、客户信息存储和按账号删除的功能:

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

#define MAX_CUSTOMERS 20  // 最大客户数限制

struct Customer
{
    char FirstName[100];
    char LastName[100];
    int CheckingBalance;
    int SavingsBalance;
    int MoneyMarketBalance;
    unsigned AccountNumber;
};

// 静态数组存储客户信息,程序运行期间有效,无需持久化
static struct Customer customers[MAX_CUSTOMERS];
static int customerCount = 0;  // 当前客户数量
static unsigned TopAccountNumber = 0;  // 账号计数器,静态变量保证值不重置

// 生成带递增账号的空客户结构体
struct Customer NewCustomer(void)
{
    TopAccountNumber++;
    struct Customer customer;
    // 初始化所有字段为默认值
    customer.AccountNumber = TopAccountNumber;
    customer.CheckingBalance = 0;
    customer.SavingsBalance = 0;
    customer.MoneyMarketBalance = 0;
    memset(customer.FirstName, 0, sizeof(customer.FirstName));
    memset(customer.LastName, 0, sizeof(customer.LastName));
    return customer;
}

// 创建新账户并保存到数组
void NewAccount()
{
    if (customerCount >= MAX_CUSTOMERS) {
        printf("\n错误:已达到最大客户数量限制。\n");
        return;
    }
    // 获取带递增账号的客户结构体
    struct Customer customer = NewCustomer();
    
    printf("\n请输入名字\n");
    scanf("%s", customer.FirstName);
    printf("\n请输入姓氏\n");
    scanf("%s", customer.LastName);
    printf("\n请输入支票账户初始存款\n");
    scanf("%d", &customer.CheckingBalance);
    printf("\n请输入储蓄账户初始存款\n");
    scanf("%d", &customer.SavingsBalance);
    
    // 将新客户存入数组
    customers[customerCount++] = customer;
    
    printf("\n欢迎 %s %s 加入Global Bank。你的账号是 %u,支票账户初始余额为 %d,储蓄账户初始余额为 %d。", 
           customer.FirstName, customer.LastName, customer.AccountNumber, 
           customer.CheckingBalance, customer.SavingsBalance);
}

// 按账号删除客户
void DeleteAccount(unsigned accountNum)
{
    int i, foundIndex = -1;
    // 查找目标账号
    for (i = 0; i < customerCount; i++) {
        if (customers[i].AccountNumber == accountNum) {
            foundIndex = i;
            break;
        }
    }
    if (foundIndex == -1) {
        printf("\n未找到账号 %u。\n", accountNum);
        return;
    }
    // 前移后续客户,覆盖被删除项
    for (; foundIndex < customerCount - 1; foundIndex++) {
        customers[foundIndex] = customers[foundIndex + 1];
    }
    customerCount--;
    printf("\n账号 %u 已成功删除。\n", accountNum);
}

int main()
{
    int choice;
    unsigned accountNum;
    printf("\n1. 创建新账户\n");
    printf("2. 删除账户\n");
    printf("7. 退出\n");
    printf("请输入选择:");
    while(1){
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                NewAccount();
                break;
            case 2:
                printf("\n请输入要删除的账号:");
                scanf("%u", &accountNum);
                DeleteAccount(accountNum);
                break;
            case 7:
                printf("祝您愉快,感谢使用Global Bank服务!\n");
                exit(0);
            default:
                printf("\n无效选择,请重新输入。\n");
        }
        // 重新打印菜单
        printf("\n\n1. 创建新账户\n");
        printf("2. 删除账户\n");
        printf("7. 退出\n");
        printf("请输入选择:");
    }
}

关键改动说明

  • 静态变量的使用:TopAccountNumber设为静态,确保每次程序运行期间调用NewCustomer时,账号会持续递增,不会每次重置为0。
  • 客户信息存储:用静态数组customers保存所有客户数据,customerCount跟踪当前客户数量,为后续删除操作提供基础。
  • 调用NewCustomer生成账号:NewAccount现在通过调用NewCustomer获取带有正确递增账号的客户结构体,避免了未初始化变量的问题。
  • 新增删除功能:实现了DeleteAccount函数,通过遍历数组找到目标账号,然后将后续元素前移完成删除操作。

内容的提问来源于stack exchange,提问作者TZZR-Gaming

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:10:24