鱼C论坛

 找回密码
 立即注册
查看: 965|回复: 1

[已解决]ofo共享单车借车系统

[复制链接]
发表于 2023-6-20 15:56:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
设计一个ofo共享单车借车系统,该系统主要是利用单车的这些信息,通过其中的任意一个信息,找出我们所需要的查找的单车的所有信息,采用基数排序法对一组具有结构特点的单车号进行排序,利用二分查找法对排序好的借车记录按单车号实现快速查找,并按其他关键字的查找可以采用最简单的顺序查找方法进行。
以下是我完成的代码,但是不能继续完成了,程序可以运行,但是功能不能实现,求助各位大佬
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // 调用isdigit函数检查注册账号是否为存数字账号
#define N 50   //宏定义
#define M 100 //宏定义,用于用户信息结构体数组

typedef struct userinfo
{
        char Accont[N]; //账号
        char Password[N];//密码
        char ID[N]; //学号
        char People_name[N];//用户姓名
}USER;

struct BikeInfo {
    int bikeID;// 编号
    char color[20];//颜色
    char state[20];//状态
    time_t borrowTime;//借车时间
    time_t returnTime;//还车时间
    struct BikeInfo* next;
} BIKE;

void MainMenu();//登录注册主菜单界面
void Sign_up_Menu();//注册菜单界面
void Log_in_Menu();//登录菜单界面
void BMainMenu();//B端主菜单界面
void CMainMenu();//C端主菜单界面
void Register_system();//用户登记注册菜单函数
void Log_in();//系统用户登录函数
void Sign_in();//新用户注册
//void Load(BIKE bike[],  int *nptr);//把文本文件中用户信息加载到内存中
//void Save(BIKE bike[],  int n);//把数据保存到文本文件中
void search_bike_Num();//按单车号进行查询
void search_bike_RentTime();//按借车时间进行查询
void search_bike_RebackTime();//按还车时间进行查询
void search_bike_User();//按使用者进行查询
void Rent_bike();//借车
void Reback_bike();//还车
void X_MaHuateng();//充值查询
void MaHuateng();//充值


struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID);// 二分查找自行车信息
void bubbleSort(struct BikeInfo* head);// 使用冒泡排序对链表进行排序
void readDataFromFile(struct BikeInfo** head);//读取文件
void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr);// 添加单车信息
void saveBikeInfoToFile(struct BikeInfo* head);//保存文件
void freeBikeInfoList(struct BikeInfo* head) ;//释放内存
void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state);//添加单车信息
void printBikeInfoList(struct BikeInfo* head);//打印单车信息
void searchByBikeID(struct BikeInfo* head);// 按单车号查询自行车信息

int sign = 0;// 区分用户标识符, 1: 系统管理员,  2:普通用户
char ACCOUNT[N]; // 用于保存普通用户登录后的账号
struct BikeInfo* head = NULL;

int main()
{
        int n;
        int numBikes = 0;
    USER user[M]; //定义结构体数组保存用户信息
    struct BikeInfo* bikeInfoList = NULL;
    struct BikeInfo* head = NULL;
    system("color 2F") ;//界面颜色
    Register_system();//登录
        //Load(bike, &n); // 把信息加载到内存中
        readDataFromFile(&head);
        readDataFromFile(&bikeInfoList);
        if(sign==1)
        {
                int select;
                FILE* fp;
                while(1)
                {
                        BMainMenu();
                printf("请输入菜单选项:\n");
                scanf("%d", &select);
                switch(select)
                {
                        case 0://退出系统
                                printf("\n成功退出系统!期待你的下次使用O(∩_ ∩)O ,祝你生活愉快!\n");
                            exit(0);
                       
                        case 1://按单车号进行查询
                                searchByBikeID(bikeInfoList);
                               
                        case 2://按借车时间进行查询
                               
                                break;
                        case 3://按还车时间进行查询
                               
                                break;
                        case 4://按使用者进行查询
                               
                                break;
                        case 5://添加单车信息
                                Creat_Bike(&bikeInfoList, &numBikes);
                                printBikeInfoList(bikeInfoList);
                               
                                scanf("%*c%*c");
                                break;
                        default:
                    printf("\n输入错误!无该菜单,输入任意键返回 *系统管理员功能菜单界面* ...\n");
                    scanf("%*c%*c");
                    break;
                        }
                        freeBikeInfoList(bikeInfoList);
                }
               
        }
        if(sign==2)
        {
                int select;
                FILE* fp;
                while(1)
                {
                        BMainMenu();
                printf("请输入菜单选项:\n");
                scanf("%d", &select);
                switch(select)
                {
                        case 0://退出系统
                                printf("\n成功退出系统!期待你的下次使用O(∩_ ∩)O ,祝你生活愉快!\n");
                            exit(0);
                               
                        case 1://租车
                                Rent_bike();
                                break;
                               
                        case 2://还车
                                Reback_bike();
                                break;
                               
                        case 3://充值
                                MaHuateng();
                                break;
                               
                        case 4://余额查询
                                X_MaHuateng();
                                break;
                               
                               
                               
                                default:
                    printf("\n输入错误!无该菜单,输入任意键返回 *ofo共享单车借车系统界面* ...\n");
                    scanf("%*c%*c");
                    break;
                                 
                        }
                }
        }
}
//登录注册主菜单界面
void MainMenu()       
{
        system("cls");
    printf("-----------------------------------\n");
        printf("|                                 |\n");
        printf("|欢迎使用ofo共享单车借车系统      |\n");
        printf("|1:用户登录                      |\n");
        printf("|2:用户注册                      |\n");
        printf("|0:退出系统                      |\n");
        printf("|                                 |\n");
        printf("-----------------------------------\n");
}

//注册菜单界面
void Sign_up_Menu()
{
        system("cls");
    printf("-----------------------------------\n");
        printf("|                                 |\n");
        printf("| 请选择你需要的服务              |\n");
        printf("| 1:注册账号                     |\n");
        printf("| 2:返回*用户登录注册界面*       |\n");
        printf("| 0:退出                         |\n");
        printf("|                                 |\n");
        printf("-----------------------------------\n");
}
//登录菜单界面
void Log_in_Menu()
{
        system("cls");
    printf("-----------------------------------\n");
        printf("|                                 |\n");
        printf("| 请选择你需要的服务              |\n");
        printf("| 1:登录账号                     |\n");
        printf("| 2:返回*用户登录注册界面*       |\n");
        printf("| 0:退出                         |\n");
        printf("|                                 |\n");
        printf("-----------------------------------\n");
}
//B端主菜单界面
void BMainMenu()
{
       
        system("cls");
    printf("-----------------------------------\n");
        printf("|                                 |\n");
        printf("| 请选择你需要的服务              |\n");
        printf("| 1:按单车号查询                 |\n");
        printf("| 2:按借车时间查询               |\n");
        printf("| 3:按还车时间查询               |\n");
        printf("| 4:按使用者查询                 |\n");
        printf("| 5:添加单车信息                 |\n");
        printf("| 0:退出                         |\n");
        printf("|                                 |\n");
        printf("-----------------------------------\n");
       
}
//C端主菜单界面
void CMainMenu()
{
        system("cls");
    printf("-----------------------------------\n");
        printf("|                                 |\n");
        printf("| 请选择你需要的服务              |\n");
        printf("| 1:租车                         |\n");
        printf("| 2:还车                         |\n");
        printf("| 3:充值                         |\n");
        printf("| 4:余额查询                     |\n");
        printf("| 0:退出                         |\n");
        printf("|                                 |\n");
        printf("-----------------------------------\n");
}
//用户登记注册菜单函数
void Register_system()
{
        int select;
    MainMenu();
    scanf("%d", &select);
    switch(select)
    {
        case 0:  //退出系统
            printf("\n成功退出系统!期待你的下次使用O(∩_ ∩)O ,祝你生活愉快!\n");
            exit(0);
        case 1: //登录账户
            Log_in();
            return;
        case 2://注册账号
            Sign_in();
            return;
        default: //重新输入选项
            printf("\n输入错误,输入任意键返回 *系统用户登录注册界面* ...\n");
            scanf("%*c%*c");
            system("cls");
            Register_system();
            return;
    }
}
//系统用户登录函数
void Log_in()
{
        int select;
    char Accont[N], Password[N];
    char Accont1[N], Password1[N], ID1[N], People_name1[N];
    FILE *fp;
    if((fp = fopen("userinfo.dat", "a")) == NULL) //创建 userinfo.dat 文本文件
    {
        printf("can not open this file\n");
        exit(0);
    }
    fclose(fp);
    Log_in_Menu(); // 系统用户登录界面
    scanf("%d", &select);
    switch(select)
    {
        case 1: //登录账号密码
            if((fp = fopen("userinfo.dat", "r")) == NULL)
            {
                printf("can not open this file\n");
                exit(0);
            }
            printf("请输入账号:\n");
            scanf("%s", Accont);
            printf("请输入密码:\n");
            scanf("%s", Password);
            while(!feof(fp))
            {
                fscanf(fp, "%s %s", Accont1, Password1, ID1,  People_name1);
                if(strcmp(Accont, "1") == 0 && strcmp(Password, "1") == 0 )  // 账户及密码识别
                {
                    sign = 1;   //系统管理员标识符
                    fclose(fp);
                    printf("账号登录成功!!尊敬无敌帅气的管理员,欢迎你使用\n *欢 迎 使 用 ofo 共 享 单 车 借 车 系 统* \n");
                    printf("\n输入任意键前往 *系统管理员功能菜单界面* ...\n");
                    scanf("%*c%*c");
                    return;
                }
                if(strcmp(Accont, Accont1) == 0 && strcmp(Password, Password1) == 0)  // 普通用户账号及密码识别
                {
                    strcpy(ACCOUNT, Accont);
                    sign = 2;  //普通用户标识符
                    fclose(fp);
                    printf("账号登录成功!!欢迎你使用 *欢 迎 使 用 ofo 共 享 单 车 借 车 系 统*\n");
                    printf("\n输入任意键前往 *系统用户功能菜单界面* ...\n");
                    scanf("%*c%*c");
                    return;
                }
            }
            if(sign == 0)
            {
                printf("账号密码错误!! 请重新输入......\n");
                printf("如若忘记账号密码,你叫我一声爹,爹给你找回密码\n");
                char die[50],die2[50];
                                scanf("%s", die);
                                if (strcmp(die, die2) == 0)
                                {
                                        printf("儿子乖,来10-3-102-5来找你爹吧");
                                }
                                else
                                {
                                        printf("密码已无法找回");
                                }         
                printf("\n输入任意键返回 *系统用户登录界面* ...\n");
                scanf("%*c%*c");
                Log_in();
                return;
            }
        case 2: //返回上界面
            printf("\n输入任意键返回 *系统用户登录注册界面* ...\n");
            scanf("%*c%*c");
            system("cls");
            Register_system();
            return ;
        case 0: //退出系统
            printf("\n成功退出系统!\n");
            exit(0);
        default: //重新输入
            printf("\n输入错误!无该菜单,输入任意键返回 *系统用户登录界面* ...\n");
            scanf("%*c%*c");
            Log_in();
            return;
    }
}
//新用户注册函数
void Sign_in()
{
        int select;
    char Accont[N], Password1[N], Password2[N], ID[N], People_name[N];
    FILE *fp;
    Sign_up_Menu(); // 新用户注册功能菜单
    if((fp = fopen("userinfo.dat", "a")) == NULL) //创建 userinfo.dat 文本文件
    {
        printf("can not open this file\n");
        exit(0);
    }
    fclose(fp);
    scanf("%d", &select);
    switch(select)
    {
        case 1:            // 注册账号密码 , 并检验注册账号是否符合规定,是否被注册 。
            while(1)
            {
                FILE *fp;
                int i;
                char Accont1[N], password1[N], ID[N], People_name[N];
                int sign1 = 0; // 检查账号是否为数字账号,0:是  1:否
                system("cls");
                Sign_up_Menu();
                printf("1\n");
                printf("请输入注册账号(九位数数字):\n");
                scanf("%s", Accont);
                if((fp = fopen("userinfo.dat", "r")) == NULL)
                {
                    printf("can not open this file\n");
                    exit(0);
                }
                for(i = 0; Accont[i] != '\0'; i++) // 检查是否为九位账号
                {
                    if(isdigit(Accont[i]) == 0)  // 检查账号是否为数字账号
                    {
                        sign1 = 1; break;
                    }
                }
                if(i >= 10 || sign1 == 1 || i < 9)
                {
                    sign1 = 1;
                    printf("未按要求注册账号,请重新注册! \n");
                    printf("\n输入任意键返回 *系统用户注册界面* ...\n");
                    scanf("%*c%*c");
                    Sign_in();
                    return;
                }
                while(!feof(fp)) // 检查账号是否被注册
                {
                    fscanf(fp, "%s %s %s %s", Accont1, password1, ID,  People_name);
                    if(strcmp(Accont, Accont1) == 0)
                    {
                        fclose(fp);
                        printf("该账号已被注册,请重新注册!\n");
                        printf("\n输入任意键返回 *系统用户注册界面* ...\n");
                        scanf("%*c%*c");
                        sign1 = 1;
                        Sign_in();
                        return;
                    }
                }
                if(i == 9 && sign1 == 0)
                {
                    printf("注册账号符合要求。");
                    break;
                }

            }
            if((fp = fopen("userinfo.dat", "a")) == NULL)
            {
                printf("can not open this file\n");
                exit(0);
            }
            printf("请输入你的身份证号:\n");
            scanf("%s", ID);
            printf("请输入你的姓名:\n");
            scanf("%s", People_name);
            printf("请输入账号密码:\n");
            scanf("%s", Password1);
            printf("请再次输入密码:\n");
            scanf("%s", Password2);
            if(strcmp(Password1, Password2) == 0)
            {
                fprintf(fp, "\n%s  %s  %s  %s\n", Accont, Password1, ID, People_name);
                fclose(fp);
                printf("****新用户创建成功( ̄▽ ̄)**** \n");
                printf("\n输入任意键返回 *系统用户登录注册界面* ...\n");
                scanf("%*c%*c");
                system("cls");
                Register_system();
                return;
            }else
            {
                printf("\n****两次输入密码不一致!!!! 请重新注册****\n");
                printf("\n输入任意键返回 *系统用户注册界面* ...\n");
                scanf("%*c%*c");
                Sign_in();
                return;
            }
        case 2: //返回上一界面
            printf("\n输入任意键返回 *系统用户登录注册界面* ...\n");
            scanf("%*c%*c");
            system("cls");
            Register_system();
            return;
        case 0:  //退出系统
            printf("\n成功退出系统!期待你的下次使用O(∩_ ∩)O,祝你生活愉快!\n");
            exit(0);
        default:  //重新输入
            printf("\n输入错误,无该菜单,输入任意键返回 *系统用户注册界面* ...\n");
            scanf("%*c%*c");
            system("cls");
            Sign_in();
            return;
    }
}


//按借车时间进行查询
void search_bike_RentTime()
{
}
//按还车时间进行查询
void search_bike_RebackTime()
{
}
//按使用者进行查询
void search_bike_User()
{
}
//借车
void Rent_bike()
{
}
//还车
void Reback_bike()
{
}
//充值查询
void X_MaHuateng()
{
}
//充值
void MaHuateng()
{
}

//批量添加单车信息
/*void Creat_Bike(BIKE bike[],  int *nptr)
{
    int i = *nptr;
    printf("请输入待添加单车信息, 输入 0 表示结束添加!\n");
    printf("编号 ---- 颜色 ---- 状态 \n");
    while(1)
    {
            printf("编号:");
        scanf("%d", &bike[i].bikeID);
        if(bike[i].bikeID== 0)
        {
            break;
        }
        printf("颜色:");
        scanf("%s", &bike[i].color);
        printf("状态:");
        scanf("%d", &bike[i].state);
        i++;
    }
    *nptr = i;
    Save(bike, *nptr); // 把数据保存到文本文件中
    printf("\n添加成功!!输入任意键返回 *系统管理员功能菜单界面*....... \n");
    scanf("%*c%*c");
}

//把数据保存到文本文件中
void Save(BIKE bike[],  int n)
{
    int i;
    FILE *fp;
    if((fp = fopen("bikeinfo.dat", "a")) == NULL)
    {
        printf("can not open this file!!");
        exit(0);
    }
    for(i = 0; i < n; i++)
    {
        fprintf(fp, "%d %s %d\n",  bike[i].bikeID, bike[i].color, bike[i].state);
    }
    fclose(fp);
}

*/
// 添加自行车信息到链表
void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state) {
    struct BikeInfo* newBike = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
    newBike->bikeID = bikeID;
    strcpy(newBike->color, color);
    strcpy(newBike->state, state);
    newBike->next = NULL;

    if (*head == NULL) {
        *head = newBike;
    } else {
        struct BikeInfo* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newBike;
    }
}

// 释放自行车信息链表的内存
void freeBikeInfoList(struct BikeInfo* head) {
    while (head != NULL) {
        struct BikeInfo* temp = head;
        head = head->next;
        free(temp);
    }
}

// 保存自行车信息到文件
void saveBikeInfoToFile(struct BikeInfo* head) {
    FILE* file = fopen("bikeinfo.dat", "a");
    if (file == NULL) {
        printf("无法打开文件!\n");
        return;
    }

    struct BikeInfo* current = head;
    while (current != NULL) {
        fprintf(file, "%d\t%s\t%s\n", current->bikeID, current->color, current->state);
        current = current->next;
    }

    fclose(file);
}


void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr) {
    int i = *nptr;
    printf("请输入待添加单车信息, 输入 0 表示结束添加!\n");
    printf("编号 ---- 颜色 ---- 状态 \n");
    while (1) {
        printf("编号:");
        int bikeID;
        scanf("%d", &bikeID);
        if (bikeID == 0) {
            break;
        }
        printf("颜色:");
        char color[20];
        scanf("%s", color);
        printf("状态:");
        char state[20];
        scanf("%s", state);
        
        addBikeInfo(bikeInfoList, bikeID, color, state);
        i++;
    }
    *nptr = i;
    saveBikeInfoToFile(*bikeInfoList);
    printf("\n添加成功!!输入任意键返回 *系统管理员功能菜单界面*....... \n");
    scanf("%*c%*c");
}

// 打印自行车信息链表
void printBikeInfoList(struct BikeInfo* head) {
    printf("自行车信息链表:\n");
    while (head != NULL) {
        printf("编号:%d\n", head->bikeID);
        printf("颜色:%s\n", head->color);
        printf("状态:%s\n\n", head->state);
        head = head->next;
    }
}


// 使用冒泡排序对链表进行排序
void bubbleSort(struct BikeInfo* head)
{
    int swapped;
    struct BikeInfo* ptr1;
    struct BikeInfo* lptr = NULL;

    if (head == NULL) {
        return;
    }

    do {
        swapped = 0;
        ptr1 = head;

        while (ptr1->next != lptr) {
            if (ptr1->bikeID > ptr1->next->bikeID) {
                int tempID = ptr1->bikeID;
                ptr1->bikeID = ptr1->next->bikeID;
                ptr1->next->bikeID = tempID;

                char tempColor[20];
                strcpy(tempColor, ptr1->color);
                strcpy(ptr1->color, ptr1->next->color);
                strcpy(ptr1->next->color, tempColor);

                char tempState[20];
                strcpy(tempState, ptr1->state);
                strcpy(ptr1->state, ptr1->next->state);
                strcpy(ptr1->next->state, tempState);

                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

// 二分查找自行车信息
struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID)
{
    struct BikeInfo* start = head;
    struct BikeInfo* end = NULL;

    while (start != end)
        {
        struct BikeInfo* mid = start;
        int count = 0;
        while (mid->next != end) {
            mid = mid->next;
            count++;
        }
        
        int midID = mid->bikeID;
        if (midID == targetID) {
            return mid;
        } else if (midID < targetID) {
            start = mid->next;
        } else {
            end = mid;
        }
    }
    return NULL;
}

// 按单车号查询自行车信息
void searchByBikeID(struct BikeInfo* head)
{
    int target;
    bubbleSort(head);
    printf("请输入要查询的单车号:");
    scanf("%d", &target);
    struct BikeInfo* result = binarySearch(head, target);
    if (result != NULL)
        {
        printf("查询结果:\n");
        printf("编号:%d\n", result->bikeID);
        printf("颜色:%s\n", result->color);
        printf("状态:%s\n", result->state);
    }
        else
        {
        printf("未找到编号为%d的自行车信息。\n", target);
    }
}

void readDataFromFile(struct BikeInfo** head)
{
    FILE* file = fopen("bikeinfo.dat", "r");
    if (file == NULL)
        {
        printf("无法打开文件 bikeinfo.dat\n");
        return;
    }

    struct BikeInfo* newNode;
    struct BikeInfo* tail = NULL;

    while (!feof(file))
        {
        newNode = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
        if (fscanf(file, "%d %s %d", &newNode->bikeID, newNode->color, &newNode->state) == 3) {
            newNode->next = NULL;

            if (*head == NULL) {
                *head = newNode;
            } else {
                tail->next = newNode;
            }
            tail = newNode;
        } else {
            free(newNode);
        }
    }
    fclose(file);
}

最佳答案
2023-6-20 16:08:36
[b]在你的代码中,你声明了两个名为 head 的全局变量,一个用于管理用户信息,一个用于管理自行车信息,这会引起命名冲突和内存泄漏的问题。建议删除第二个 head 的声明,只保留一个全局变量用于管理用户信息。

此外,你在函数 Creat_Bike() 中调用了 saveBikeInfoToFile() 函数来保存自行车信息到文件,但是你没有实现这个函数。你需要添加这个函数的实现,该函数的功能是将自行车信息写入到文件 bikeinfo.dat 中。

另外,你通过调用 saveBikeInfoToFile() 函数来将自行车信息保存到文件中,但是没有调用 freeBikeInfoList() 函数来释放自行车信息链表的内存。建议在程序结束之前调用 freeBikeInfoList() 函数来释放内存。

最后,你在调用 binarySearch() 函数时,传入的参数是 head ,但是 head 参数的类型是 struct BikeInfo** ,而应该传入的是 struct BikeInfo* 类型的头结点。你在函数中有两个全局变量名为 head ,一个是用户信息的头结点,一个是自行车信息的头结点,你需要将自行车信息的头结点传入到 binarySearch() 函数中来进行查找。

以下是修改后的代码示例:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. #define N 50  
  6. #define M 100

  7. typedef struct userinfo
  8. {
  9.         char Accont[N];
  10.         char Password[N];
  11.         char ID[N];
  12.         char People_name[N];
  13. }USER;

  14. struct BikeInfo {
  15.     int bikeID;
  16.     char color[20];
  17.     char state[20];
  18.     time_t borrowTime;
  19.     time_t returnTime;
  20.     struct BikeInfo* next;
  21. };

  22. void MainMenu();
  23. void Sign_up_Menu();
  24. void Log_in_Menu();
  25. void BMainMenu();
  26. void CMainMenu();
  27. void Register_system();
  28. void Log_in();
  29. void Sign_in();
  30. void search_bike_Num();
  31. void search_bike_RentTime();
  32. void search_bike_RebackTime();
  33. void search_bike_User();
  34. void Rent_bike();
  35. void Reback_bike();
  36. void X_MaHuateng();
  37. void MaHuateng();
  38. void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr);
  39. void saveBikeInfoToFile(struct BikeInfo* head);
  40. void freeBikeInfoList(struct BikeInfo* head);
  41. void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state);
  42. void printBikeInfoList(struct BikeInfo* head);
  43. void searchByBikeID(struct BikeInfo* head);
  44. struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID);
  45. void bubbleSort(struct BikeInfo* head);
  46. void readDataFromFile(struct BikeInfo** head);

  47. int sign = 0;
  48. char ACCOUNT[N];
  49. struct BikeInfo* head = NULL;

  50. int main()
  51. {
  52.     int n;
  53.     int numBikes = 0;
  54.     USER user[M];
  55.     struct BikeInfo* bikeInfoList = NULL;
  56.     system("color 2F");

  57.     Register_system();

  58.     readDataFromFile(&head);
  59.     readDataFromFile(&bikeInfoList);

  60.     // ...

  61.     freeBikeInfoList(head);
  62.     freeBikeInfoList(bikeInfoList);

  63.     return 0;
  64. }

  65. void MainMenu()
  66. {
  67.     // ...
  68. }

  69. void Sign_up_Menu()
  70. {
  71.     // ...
  72. }

  73. void Log_in_Menu()
  74. {
  75.     // ...
  76. }

  77. void BMainMenu()
  78. {
  79.     // ...
  80. }

  81. void CMainMenu()
  82. {
  83.     // ...
  84. }

  85. void Register_system()
  86. {
  87.     // ...
  88. }

  89. void Log_in()
  90. {
  91.     // ...
  92. }

  93. void Sign_in()
  94. {
  95.     // ...
  96. }

  97. void search_bike_Num()
  98. {
  99.     // ...
  100. }

  101. void search_bike_RentTime()
  102. {
  103.     // ...
  104. }

  105. void search_bike_RebackTime()
  106. {
  107.     // ...
  108. }

  109. void search_bike_User()
  110. {
  111.     // ...
  112. }

  113. void Rent_bike()
  114. {
  115.     // ...
  116. }

  117. void Reback_bike()
  118. {
  119.     // ...
  120. }

  121. void X_MaHuateng()
  122. {
  123.     // ...
  124. }

  125. void MaHuateng()
  126. {
  127.     // ...
  128. }

  129. void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr)
  130. {
  131.     // ...
  132. }

  133. void saveBikeInfoToFile(struct BikeInfo* head)
  134. {
  135.     FILE* file = fopen("bikeinfo.dat", "a");
  136.     if (file == NULL) {
  137.         printf("无法打开文件!\n");
  138.         return;
  139.     }

  140.     struct BikeInfo* current = head;
  141.     while (current != NULL) {
  142.         fprintf(file, "%d\t%s\t%s\n", current->bikeID, current->color, current->state);
  143.         current = current->next;
  144.     }

  145.     fclose(file);
  146. }

  147. void freeBikeInfoList(struct BikeInfo* head)
  148. {
  149.     while (head != NULL) {
  150.         struct BikeInfo* temp = head;
  151.         head = head->next;
  152.         free(temp);
  153.     }
  154. }

  155. void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state)
  156. {
  157.     struct BikeInfo* newBike = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
  158.     newBike->bikeID = bikeID;
  159.     strcpy(newBike->color, color);
  160.     strcpy(newBike->state, state);
  161.     newBike->next = NULL;

  162.     if (*head == NULL) {
  163.         *head = newBike;
  164.     } else {
  165.         struct BikeInfo* temp = *head;
  166.         while (temp->next != NULL) {
  167.             temp = temp->next;
  168.         }
  169.         temp->next = newBike;
  170.     }
  171. }

  172. void printBikeInfoList(struct BikeInfo* head)
  173. {
  174.     // ...
  175. }

  176. void searchByBikeID(struct BikeInfo* head)
  177. {
  178.     int target;
  179.     bubbleSort(head);
  180.     printf("请输入要查询的单车号:");
  181.     scanf("%d", &target);
  182.     struct BikeInfo* result = binarySearch(head, target);
  183.     if (result != NULL) {
  184.         printf("查询结果:\n");
  185.         printf("编号:%d\n", result->bikeID);
  186.         printf("颜色:%s\n", result->color);
  187.         printf("状态:%s\n", result->state);
  188.     } else {
  189.         printf("未找到编号为%d的自行车信息。\n", target);
  190.     }
  191. }

  192. struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID)
  193. {
  194.     struct BikeInfo* start = head;
  195.     struct BikeInfo* end = NULL;

  196.     while (start != end) {
  197.         struct BikeInfo* mid = start;
  198.         int count = 0;
  199.         while (mid->next != end) {
  200.             mid = mid->next;
  201.             count++;
  202.         }

  203.         int midID = mid->bikeID;
  204.         if (midID == targetID) {
  205.             return mid;
  206.         } else if (midID < targetID) {
  207.             start = mid->next;
  208.         } else {
  209.             end = mid;
  210.         }
  211.     }
  212.     return NULL;
  213. }

  214. void bubbleSort(struct BikeInfo* head)
  215. {
  216.     int swapped;
  217.     struct BikeInfo* ptr1;
  218.     struct BikeInfo* lptr = NULL;

  219.     if (head == NULL) {
  220.         return;
  221.     }

  222.     do {
  223.         swapped = 0;
  224.         ptr1 = head;

  225.         while (ptr1->next != lptr) {
  226.             if (ptr1->bikeID > ptr1->next->bikeID) {
  227.                 int tempID = ptr1->bikeID;
  228.                 ptr1->bikeID = ptr1->next->bikeID;
  229.                 ptr1->next->bikeID = tempID;

  230.                 char tempColor[20];
  231.                 strcpy(tempColor, ptr1->color);
  232.                 strcpy(ptr1->color, ptr1->next->color);
  233.                 strcpy(ptr1->next->color, tempColor);

  234.                 char tempState[20];
  235.                 strcpy(tempState, ptr1->state);
  236.                 strcpy(ptr1->state, ptr1->next->state);
  237.                 strcpy(ptr1->next->state, tempState);

  238.                 swapped = 1;
  239.             }
  240.             ptr1 = ptr1->next;
  241.         }
  242.         lptr = ptr1;
  243.     } while (swapped);
  244. }

  245. void readDataFromFile(struct BikeInfo** head)
  246. {
  247.     FILE* file = fopen("bikeinfo.dat", "r");
  248.     if (file == NULL) {
  249.         printf("无法打开文件 bikeinfo.dat\n");
  250.         return;
  251.     }

  252.     struct BikeInfo* newNode;
  253.     struct BikeInfo* tail = NULL;

  254.     while (!feof(file)) {
  255.         newNode = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
  256.         if (fscanf(file, "%d %s %s", &newNode->bikeID, newNode->color, newNode->state) == 3) {
  257.             newNode->next = NULL;

  258.             if (*head == NULL) {
  259.                 *head = newNode;
  260.             } else {
  261.                 tail->next = newNode;
  262.             }
  263.             tail = newNode;
  264.         } else {
  265.             free(newNode);
  266.         }
  267.     }
  268.     fclose(file);
  269. }
复制代码
[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-20 16:08:36 | 显示全部楼层    本楼为最佳答案   
[b]在你的代码中,你声明了两个名为 head 的全局变量,一个用于管理用户信息,一个用于管理自行车信息,这会引起命名冲突和内存泄漏的问题。建议删除第二个 head 的声明,只保留一个全局变量用于管理用户信息。

此外,你在函数 Creat_Bike() 中调用了 saveBikeInfoToFile() 函数来保存自行车信息到文件,但是你没有实现这个函数。你需要添加这个函数的实现,该函数的功能是将自行车信息写入到文件 bikeinfo.dat 中。

另外,你通过调用 saveBikeInfoToFile() 函数来将自行车信息保存到文件中,但是没有调用 freeBikeInfoList() 函数来释放自行车信息链表的内存。建议在程序结束之前调用 freeBikeInfoList() 函数来释放内存。

最后,你在调用 binarySearch() 函数时,传入的参数是 head ,但是 head 参数的类型是 struct BikeInfo** ,而应该传入的是 struct BikeInfo* 类型的头结点。你在函数中有两个全局变量名为 head ,一个是用户信息的头结点,一个是自行车信息的头结点,你需要将自行车信息的头结点传入到 binarySearch() 函数中来进行查找。

以下是修改后的代码示例:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. #define N 50  
  6. #define M 100

  7. typedef struct userinfo
  8. {
  9.         char Accont[N];
  10.         char Password[N];
  11.         char ID[N];
  12.         char People_name[N];
  13. }USER;

  14. struct BikeInfo {
  15.     int bikeID;
  16.     char color[20];
  17.     char state[20];
  18.     time_t borrowTime;
  19.     time_t returnTime;
  20.     struct BikeInfo* next;
  21. };

  22. void MainMenu();
  23. void Sign_up_Menu();
  24. void Log_in_Menu();
  25. void BMainMenu();
  26. void CMainMenu();
  27. void Register_system();
  28. void Log_in();
  29. void Sign_in();
  30. void search_bike_Num();
  31. void search_bike_RentTime();
  32. void search_bike_RebackTime();
  33. void search_bike_User();
  34. void Rent_bike();
  35. void Reback_bike();
  36. void X_MaHuateng();
  37. void MaHuateng();
  38. void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr);
  39. void saveBikeInfoToFile(struct BikeInfo* head);
  40. void freeBikeInfoList(struct BikeInfo* head);
  41. void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state);
  42. void printBikeInfoList(struct BikeInfo* head);
  43. void searchByBikeID(struct BikeInfo* head);
  44. struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID);
  45. void bubbleSort(struct BikeInfo* head);
  46. void readDataFromFile(struct BikeInfo** head);

  47. int sign = 0;
  48. char ACCOUNT[N];
  49. struct BikeInfo* head = NULL;

  50. int main()
  51. {
  52.     int n;
  53.     int numBikes = 0;
  54.     USER user[M];
  55.     struct BikeInfo* bikeInfoList = NULL;
  56.     system("color 2F");

  57.     Register_system();

  58.     readDataFromFile(&head);
  59.     readDataFromFile(&bikeInfoList);

  60.     // ...

  61.     freeBikeInfoList(head);
  62.     freeBikeInfoList(bikeInfoList);

  63.     return 0;
  64. }

  65. void MainMenu()
  66. {
  67.     // ...
  68. }

  69. void Sign_up_Menu()
  70. {
  71.     // ...
  72. }

  73. void Log_in_Menu()
  74. {
  75.     // ...
  76. }

  77. void BMainMenu()
  78. {
  79.     // ...
  80. }

  81. void CMainMenu()
  82. {
  83.     // ...
  84. }

  85. void Register_system()
  86. {
  87.     // ...
  88. }

  89. void Log_in()
  90. {
  91.     // ...
  92. }

  93. void Sign_in()
  94. {
  95.     // ...
  96. }

  97. void search_bike_Num()
  98. {
  99.     // ...
  100. }

  101. void search_bike_RentTime()
  102. {
  103.     // ...
  104. }

  105. void search_bike_RebackTime()
  106. {
  107.     // ...
  108. }

  109. void search_bike_User()
  110. {
  111.     // ...
  112. }

  113. void Rent_bike()
  114. {
  115.     // ...
  116. }

  117. void Reback_bike()
  118. {
  119.     // ...
  120. }

  121. void X_MaHuateng()
  122. {
  123.     // ...
  124. }

  125. void MaHuateng()
  126. {
  127.     // ...
  128. }

  129. void Creat_Bike(struct BikeInfo** bikeInfoList, int* nptr)
  130. {
  131.     // ...
  132. }

  133. void saveBikeInfoToFile(struct BikeInfo* head)
  134. {
  135.     FILE* file = fopen("bikeinfo.dat", "a");
  136.     if (file == NULL) {
  137.         printf("无法打开文件!\n");
  138.         return;
  139.     }

  140.     struct BikeInfo* current = head;
  141.     while (current != NULL) {
  142.         fprintf(file, "%d\t%s\t%s\n", current->bikeID, current->color, current->state);
  143.         current = current->next;
  144.     }

  145.     fclose(file);
  146. }

  147. void freeBikeInfoList(struct BikeInfo* head)
  148. {
  149.     while (head != NULL) {
  150.         struct BikeInfo* temp = head;
  151.         head = head->next;
  152.         free(temp);
  153.     }
  154. }

  155. void addBikeInfo(struct BikeInfo** head, int bikeID, const char* color, const char* state)
  156. {
  157.     struct BikeInfo* newBike = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
  158.     newBike->bikeID = bikeID;
  159.     strcpy(newBike->color, color);
  160.     strcpy(newBike->state, state);
  161.     newBike->next = NULL;

  162.     if (*head == NULL) {
  163.         *head = newBike;
  164.     } else {
  165.         struct BikeInfo* temp = *head;
  166.         while (temp->next != NULL) {
  167.             temp = temp->next;
  168.         }
  169.         temp->next = newBike;
  170.     }
  171. }

  172. void printBikeInfoList(struct BikeInfo* head)
  173. {
  174.     // ...
  175. }

  176. void searchByBikeID(struct BikeInfo* head)
  177. {
  178.     int target;
  179.     bubbleSort(head);
  180.     printf("请输入要查询的单车号:");
  181.     scanf("%d", &target);
  182.     struct BikeInfo* result = binarySearch(head, target);
  183.     if (result != NULL) {
  184.         printf("查询结果:\n");
  185.         printf("编号:%d\n", result->bikeID);
  186.         printf("颜色:%s\n", result->color);
  187.         printf("状态:%s\n", result->state);
  188.     } else {
  189.         printf("未找到编号为%d的自行车信息。\n", target);
  190.     }
  191. }

  192. struct BikeInfo* binarySearch(struct BikeInfo* head, int targetID)
  193. {
  194.     struct BikeInfo* start = head;
  195.     struct BikeInfo* end = NULL;

  196.     while (start != end) {
  197.         struct BikeInfo* mid = start;
  198.         int count = 0;
  199.         while (mid->next != end) {
  200.             mid = mid->next;
  201.             count++;
  202.         }

  203.         int midID = mid->bikeID;
  204.         if (midID == targetID) {
  205.             return mid;
  206.         } else if (midID < targetID) {
  207.             start = mid->next;
  208.         } else {
  209.             end = mid;
  210.         }
  211.     }
  212.     return NULL;
  213. }

  214. void bubbleSort(struct BikeInfo* head)
  215. {
  216.     int swapped;
  217.     struct BikeInfo* ptr1;
  218.     struct BikeInfo* lptr = NULL;

  219.     if (head == NULL) {
  220.         return;
  221.     }

  222.     do {
  223.         swapped = 0;
  224.         ptr1 = head;

  225.         while (ptr1->next != lptr) {
  226.             if (ptr1->bikeID > ptr1->next->bikeID) {
  227.                 int tempID = ptr1->bikeID;
  228.                 ptr1->bikeID = ptr1->next->bikeID;
  229.                 ptr1->next->bikeID = tempID;

  230.                 char tempColor[20];
  231.                 strcpy(tempColor, ptr1->color);
  232.                 strcpy(ptr1->color, ptr1->next->color);
  233.                 strcpy(ptr1->next->color, tempColor);

  234.                 char tempState[20];
  235.                 strcpy(tempState, ptr1->state);
  236.                 strcpy(ptr1->state, ptr1->next->state);
  237.                 strcpy(ptr1->next->state, tempState);

  238.                 swapped = 1;
  239.             }
  240.             ptr1 = ptr1->next;
  241.         }
  242.         lptr = ptr1;
  243.     } while (swapped);
  244. }

  245. void readDataFromFile(struct BikeInfo** head)
  246. {
  247.     FILE* file = fopen("bikeinfo.dat", "r");
  248.     if (file == NULL) {
  249.         printf("无法打开文件 bikeinfo.dat\n");
  250.         return;
  251.     }

  252.     struct BikeInfo* newNode;
  253.     struct BikeInfo* tail = NULL;

  254.     while (!feof(file)) {
  255.         newNode = (struct BikeInfo*)malloc(sizeof(struct BikeInfo));
  256.         if (fscanf(file, "%d %s %s", &newNode->bikeID, newNode->color, newNode->state) == 3) {
  257.             newNode->next = NULL;

  258.             if (*head == NULL) {
  259.                 *head = newNode;
  260.             } else {
  261.                 tail->next = newNode;
  262.             }
  263.             tail = newNode;
  264.         } else {
  265.             free(newNode);
  266.         }
  267.     }
  268.     fclose(file);
  269. }
复制代码
[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 06:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表