C语言版停车位管理系统

本文实例为大家分享了C语言实现停车位管理系统的具体代码,供大家参考,具体内容如下
简单功能介绍 1、录入车辆信息
2、查找车辆信息
3、删除车辆信息
4、修改车辆信息
5、查找区域车辆信息
6、排序(按照车主姓名排序)
7、展示所有车辆信息
8、将录入的数据保存在文件中
9、将文件中的数据读出
算法构造 链表的增删改查

  • 结构体的定义
typedef struct Parking{char name[20]; //车主姓名char carname[10]; //车牌号long information; //车主联系方式char region; //车位区域编号int num; //车位编号struct Parking *next; //指针}Parking;

  • 录入车辆信息
利用尾插法插入新添加的数据
Parking *Addcar(Parking *head)//录入车辆信息{int x; system("cls"); //清屏操作system("color B"); //改变字体颜色Parking *p = head; while(p->next!=NULL)//利用尾插法插入新用户信息{p = p->next; }printf("输入需要添加的车辆个数:"); scanf("%d", &x); while(x--)//输入用户信息{system("cls"); Parking *Node = (Parking *)malloc(sizeof(Parking)); printf("\n输入用户姓名:"); scanf("%s",Node->name); printf("\n输入车牌号:"); scanf("%s",Node->carname); printf("\n输入车主联系方式:"); scanf("%ld",&Node->information); printf("\n输入车位区域编号:"); getchar(); scanf("%c",&Node->region); printf("\n输入车位编号:"); scanf("%d",&Node->num); Node->next=NULL; p->next = Node; p = p->next; }printf("\n输入完成!"); F(head); //打印添加后的用户信息printf("\n按任意键返回主页\n"); getch(); //用来显示结果return head; }

  • 删除用户信息
根据用户名匹配查找用户信息进行删除
Parking *Delete(Parking *head)//删除用户信息{char name[20]; system("cls"); system("color B"); printf("\n\t\t输入你要删除的用户姓名:"); scanf("%s", name); Parking *p = head; Parking *q = p->next; while(q)//找到需要删除数据的前结点{if(strcmp(q->name,name)==0)//判断字符串是否相等函数{p->next = q->next; free(q); //释放内存空间break; }p = q; q = p->next; }if(p->next==NULL){printf("\n\t\t未找到该用户信息!"); }else{printf("\n\t\t删除成功!!"); F(head); }printf("\n\t\t按任意键返回主菜单"); getch(); return head; }

  • 查找用户信息
【C语言版停车位管理系统】根据字符串匹配查找用户信息
voidFind(Parking *head)//查找用户信息{system("cls"); system("color B"); char name[20]; printf("\n\t\t输入你要查找的用户姓名:"); scanf("%s", name); Parking *p = head; while(p->next){if(strcmp(p->next->name,name)==0){printf("找到了!\n"); printf("该用户的信息如下:"); printf("\n\t\t---用户姓名%s",p->next->name); printf("\n\t\t---车牌号%s",p->next->carname); printf("\n\t\t---车主联系方式%ld",p->next->information); printf("\n\t\t---车位区域编号%c",p->next->region); printf("\n\t\t---车位编号%d",p->next->num); break; }p = p->next; }if(p->next==NULL)printf("\n\t\t没有找到该用户信息"); printf("\n\t\t按任意键返回主菜单"); getch(); }

  • 修改用户信息
查找用户找到后显示该用户的信息,根据用户选择修改信息
Parking *Change(Parking *head)//修改用户信息{char name[20]; system("cls"); system("color B"); printf("输入需要修改的用户姓名:"); scanf("%s", name); Parking *p = head->next; while(p){if(strcmp(p->name,name)==0){system("cls"); system("color B"); int x; printf("\n\t\t--该用户信息--\n\n"); printf("\t---------------------------------------------------------------------\n"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); printf("\n输入你要修改的信息编号:\n"); printf("1-车主姓名\n"); printf("2-车牌号\n"); printf("3-车主联系方式 \n"); printf("4-车位区域编号 \n"); printf("5-车位编号\n"); scanf("%d", &x); switch(x){case 1:printf("\n\t输入修改后的信息"); scanf("%s", p->name); break; case 2:printf("\n\t输入修改后的信息"); scanf("%s", p->carname); break; case 3:printf("\n\t输入修改后的信息"); scanf("%ld", &p->information); break; case 4:printf("\n\t输入修改后的信息"); scanf("%c", &p->region); break; case 5:printf("\n\t输入修改后的信息"); scanf("%d", &p->num); break; }printf("\n\t\t修改后的用户信息为"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); break; }p = p->next; }if(p==NULL){printf("\n未找到相应用户"); printf("\n按任意键返回主页\n"); getch(); return head; }printf("\n修改完成!"); F(head); //显示修改后的全部信息printf("\n按任意键返回主页\n"); getch(); return head; }

其他链表操作
  • 排序(根据用户姓名排序)
void *Sort(Parking *head)//排序{Parking *p=head; Parking *q,*p1=NULL,*p2=NULL; while(p->next != NULL){q = p->next; while(q->next != NULL){if(strcmp(p->next->name,q->next->name)>0)//字符串比较{p1 = p->next; //记录p与q的下一结点p2 = q->next; q->next = q->next->next; p->next = p2; p2->next = p1; }elseq = q->next; }p = p->next; }F(head); printf("\n\t\t按任意键返回主菜单"); getch(); }

  • 查找区域车辆信息
void RegionalVehicles(Parking *head)//寻找指定区域内车辆信息{system("cls"); system("color B"); char c; printf("\n\t\t输入你想要查看的区域"); getchar(); scanf("%c", &c); Parking *p = head->next; printf("\n\t\t--%c区域内车辆用户信息--\n\n",c); printf("\t---------------------------------------------------------------------\n"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); while(p){if(p->region==c)//判断区域字符与输入字符是否相等,相等则输出{printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); }p = p->next; }printf("\n\t\t按任意键返回主菜单"); getch(); }

文件的读取与输入
  • 将数据保存在文件中
之所以用w形式打开文件是因为我在程序运行前已将文件内数据读出,最后进行保存数据时覆盖原数据不会导致原数据丢失
voidPreservation(Parking *head)//保存数据在文件中{const char* filename1 = "C:/Users/judicious/Desktop/car.text"; //文件路径FILE *fp = fopen(filename1, "w"); //以写的方式打开文件,若文件不存在,则建立新的文件。若文件内有内容则会覆盖原有内容if (fp == NULL)//判断是否成功打开{puts("Fail to open file!"); exit(1); }Parking * p = head->next; while(p){fprintf(fp,"%s %s %ld %c %d\n", p->name, p->carname, p->information, p->region, p->num); //向文件内输入数据p = p->next; }fclose(fp); //关闭文件return; }

  • 将文件中数据读出
将文件内数据读出并保存在链表当中,方便对数据进行修改
Parking *Read()//读取文件内数据{const char* filename1 = "C:/Users/judicious/Desktop/car.text"; FILE *fp = fopen(filename1, "rt"); //以读的方式打开文件if (fp == NULL){puts("Fail to open file!"); exit(1); }Parking *head = (Parking *)malloc(sizeof(Parking)); //为头指针申请内存空间head->next = NULL; Parking *p1 = (Parking *)malloc(sizeof(Parking)); //申请存放数据的结点p1->next = NULL; if(fscanf(fp, "%s %s %ld %c %d", p1->name, p1->carname, &p1->information, &p1->region, &p1->num)!=EOF)//判断所读文件内容是否为空{head->next=p1; Parking *tmp=head; while (tmp->next)//利用尾插法插入结点,确保读出的数据安正序输出{tmp=tmp->next; }while(fgetc(fp)!=EOF)//判断文件是否为空{Parking *p = (Parking *)malloc(sizeof(Parking)); fscanf(fp, "%s %s %ld %c %d", p->name, p->carname, &p->information, &p->region, &p->num); tmp->next=p; p->next=NULL; tmp = tmp->next; }Parking *p2 = head; //删除最后一个结点while(p2->next->next){p2 = p2->next; }p2->next = NULL; }elsereturn NULL; fclose(fp); return head->next; }

删除最后一个结点的原因是我存入数据时每行数据后有一个换行符,fgetc()将换行符读走,最后一行时也将换行符读走。此时判断文件不为空,继续读取内容,所以最后一个结点内的内容为随机值,因此删除该结点。
主菜单
void menu()//主菜单{Parking *head = (Parking *)malloc(sizeof(Parking)); head->next = Read(); //读取文件信息system("cls"); HideCursor(); //隐藏光标int choice; do{system("cls"); //主页面printf("--------------------------------------------------------------"); printf("\n\n"); printf("********************欢迎来到停车位管理系统********************\n"); printf("\n\n"); printf("---------------------------------------------------------\n"); printf("\n1- 录入车辆信息"); printf("\n2- 查找车辆信息"); printf("\n3- 移除车辆信息"); printf("\n4- 修改车辆信息"); printf("\n5- 查找区域车辆信息"); printf("\n6- 展示所有车辆信息"); printf("\n7- 按车主姓名排序"); printf("\n0- 退出管理系统并保存数据\n"); printf("\n-------------------------------------------------------\n"); scanf("%d", &choice); switch(choice){case 1:head = Addcar(head); break; case 2:Find(head); break; case 3:head = Delete(head); break; case 4:head = Change(head); break; case 5:RegionalVehicles(head); break; case 6:F(head); break; case 7:Sort(head); break; }} while (choice!=0); Preservation(head); //保存数据}

C语言版停车位管理系统
文章图片

主函数
int main(){menu(); return 0; }

完整代码
#include #include #include #include //system("cls"):清屏#include //getch()typedef struct Parking{char name[20]; //车主姓名char carname[10]; //车牌号long information; //车主联系方式char region; //车位区域编号int num; //车位编号struct Parking *next; //指针}Parking; void HideCursor()//隐藏光标{ CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }voidF(Parking *head)//打印用户信息{system("cls"); system("color B"); Parking *p = head->next; printf("\n\t\t--停车场用户信息--\n\n"); printf("\t---------------------------------------------------------------------\n"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); while (p){printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); p = p->next; }getch(); return; }Parking *Addcar(Parking *head)//录入车辆信息{int x; system("cls"); system("color B"); Parking *p = head; while(p->next!=NULL){p = p->next; }printf("输入需要添加的车辆个数:"); scanf("%d", &x); while(x--){system("cls"); Parking *Node = (Parking *)malloc(sizeof(Parking)); printf("\n输入用户姓名:"); scanf("%s",Node->name); printf("\n输入车牌号:"); scanf("%s",Node->carname); printf("\n输入车主联系方式:"); scanf("%ld",&Node->information); printf("\n输入车位区域编号:"); getchar(); scanf("%c",&Node->region); printf("\n输入车位编号:"); scanf("%d",&Node->num); Node->next=NULL; p->next = Node; p = p->next; }printf("\n输入完成!"); F(head); printf("\n按任意键返回主页\n"); getch(); return head; }Parking *Change(Parking *head)//修改用户信息{char name[20]; system("cls"); system("color B"); printf("输入需要修改的用户姓名:"); scanf("%s", name); Parking *p = head->next; while(p){if(strcmp(p->name,name)==0){system("cls"); system("color B"); int x; printf("\n\t\t--该用户信息--\n\n"); printf("\t---------------------------------------------------------------------\n"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); printf("\n输入你要修改的信息编号:\n"); printf("1-车主姓名\n"); printf("2-车牌号\n"); printf("3-车主联系方式 \n"); printf("4-车位区域编号 \n"); printf("5-车位编号\n"); scanf("%d", &x); switch(x){case 1:printf("\n\t输入修改后的信息"); scanf("%s", p->name); break; case 2:printf("\n\t输入修改后的信息"); scanf("%s", p->carname); break; case 3:printf("\n\t输入修改后的信息"); scanf("%ld", &p->information); break; case 4:printf("\n\t输入修改后的信息"); scanf("%c", &p->region); break; case 5:printf("\n\t输入修改后的信息"); scanf("%d", &p->num); break; }printf("\n\t\t修改后的用户信息为"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); break; }p = p->next; }if(p==NULL){printf("\n未找到相应用户"); printf("\n按任意键返回主页\n"); getch(); return head; }printf("\n修改完成!"); F(head); printf("\n按任意键返回主页\n"); getch(); return head; }Parking *Delete(Parking *head)//删除用户信息{char name[20]; system("cls"); system("color B"); printf("\n\t\t输入你要删除的用户姓名:"); scanf("%s", name); Parking *p = head; Parking *q = p->next; while(q){if(strcmp(q->name,name)==0){p->next = q->next; free(q); break; }p = q; q = p->next; }if(p->next==NULL){printf("\n\t\t未找到该用户信息!"); }else{printf("\n\t\t删除成功!!"); F(head); }printf("\n\t\t按任意键返回主菜单"); getch(); return head; }voidFind(Parking *head)//查找用户信息{system("cls"); system("color B"); char name[20]; printf("\n\t\t输入你要查找的用户姓名:"); scanf("%s", name); Parking *p = head; while(p->next){if(strcmp(p->next->name,name)==0){printf("找到了!\n"); printf("该用户的信息如下:"); printf("\n\t\t---用户姓名%s",p->next->name); printf("\n\t\t---车牌号%s",p->next->carname); printf("\n\t\t---车主联系方式%ld",p->next->information); printf("\n\t\t---车位区域编号%c",p->next->region); printf("\n\t\t---车位编号%d",p->next->num); break; }p = p->next; }if(p->next==NULL)printf("\n\t\t没有找到该用户信息"); printf("\n\t\t按任意键返回主菜单"); getch(); }void RegionalVehicles(Parking *head)//寻找指定区域内车辆信息{system("cls"); system("color B"); char c; printf("\n\t\t输入你想要查看的区域"); getchar(); scanf("%c", &c); Parking *p = head->next; printf("\n\t\t--%c区域内车辆用户信息--\n\n",c); printf("\t---------------------------------------------------------------------\n"); printf("\t车主姓名--------车牌号-------车主联系方式---车位区域编号-----车位编号----\n"); while(p){if(p->region==c){printf("\t%-17s%-16s%-15ld%-16c%-17d\n", p->name, p->carname, p->information, p->region, p->num); }p = p->next; }printf("\n\t\t按任意键返回主菜单"); getch(); }void *Sort(Parking *head)//排序{Parking *p=head; Parking *q,*p1=NULL,*p2=NULL; while(p->next != NULL){q = p->next; while(q->next != NULL){if(strcmp(p->next->name,q->next->name)>0){p1 = p->next; p2 = q->next; q->next = q->next->next; p->next = p2; p2->next = p1; }elseq = q->next; }p = p->next; }F(head); printf("\n\t\t按任意键返回主菜单"); getch(); }voidPreservation(Parking *head)//保存数据在文件中{const char* filename1 = "C:/Users/judicious/Desktop/car.text"; FILE *fp = fopen(filename1, "w"); if (fp == NULL){puts("Fail to open file!"); exit(1); }Parking * p = head->next; while(p){fprintf(fp,"%s %s %ld %c %d\n", p->name, p->carname, p->information, p->region, p->num); p = p->next; }fclose(fp); return; }Parking *Read()//读取文件内数据{const char* filename1 = "C:/Users/judicious/Desktop/car.text"; FILE *fp = fopen(filename1, "rt"); if (fp == NULL){puts("Fail to open file!"); exit(1); }Parking *head = (Parking *)malloc(sizeof(Parking)); head->next = NULL; Parking *p1 = (Parking *)malloc(sizeof(Parking)); p1->next = NULL; if(fscanf(fp, "%s %s %ld %c %d", p1->name, p1->carname, &p1->information, &p1->region, &p1->num)!=EOF){head->next=p1; Parking *tmp=head; while (tmp->next){tmp=tmp->next; }while(fgetc(fp)!=EOF){Parking *p = (Parking *)malloc(sizeof(Parking)); fscanf(fp, "%s %s %ld %c %d", p->name, p->carname, &p->information, &p->region, &p->num); tmp->next=p; p->next=NULL; tmp = tmp->next; }Parking *p2 = head; while(p2->next->next){p2 = p2->next; }p2->next = NULL; }elsereturn NULL; fclose(fp); return head->next; }void menu()//主菜单{Parking *head = (Parking *)malloc(sizeof(Parking)); head->next = Read(); //读取文件信息system("cls"); HideCursor(); //隐藏光标int choice; do{system("cls"); printf("--------------------------------------------------------------"); printf("\n\n"); printf("********************欢迎来到停车位管理系统********************\n"); printf("\n\n"); printf("---------------------------------------------------------\n"); printf("\n1- 录入车辆信息"); printf("\n2- 查找车辆信息"); printf("\n3- 移除车辆信息"); printf("\n4- 修改车辆信息"); printf("\n5- 查找区域车辆信息"); printf("\n6- 展示所有车辆信息"); printf("\n7- 按车主姓名排序"); printf("\n0- 退出管理系统并保存数据\n"); printf("\n-------------------------------------------------------\n"); scanf("%d", &choice); switch(choice){case 1:head = Addcar(head); break; case 2:Find(head); break; case 3:head = Delete(head); break; case 4:head = Change(head); break; case 5:RegionalVehicles(head); break; case 6:F(head); break; case 7:Sort(head); break; }} while (choice!=0); Preservation(head); //保存数据}int main(){menu(); return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读