C语言停车位管理系统
Judicious_x 人气:0简单功能介绍
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; }
- 查找用户信息
根据字符串匹配查找用户信息
void Find(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; } else q = 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形式打开文件是因为我在程序运行前已将文件内数据读出,最后进行保存数据时覆盖原数据不会导致原数据丢失
void Preservation(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; } else return 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("\n 1- 录入车辆信息 "); printf("\n 2- 查找车辆信息 "); printf("\n 3- 移除车辆信息 "); printf("\n 4- 修改车辆信息 "); printf("\n 5- 查找区域车辆信息 "); printf("\n 6- 展示所有车辆信息 "); printf("\n 7- 按车主姓名排序 "); printf("\n 0- 退出管理系统并保存数据 \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; }
完整代码
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> //system("cls"):清屏 #include <conio.h> //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); } void F(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; } void Find(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; } else q = q->next; } p = p->next; } F(head); printf("\n\t\t按任意键返回主菜单"); getch(); } void Preservation(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; } else return 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("\n 1- 录入车辆信息 "); printf("\n 2- 查找车辆信息 "); printf("\n 3- 移除车辆信息 "); printf("\n 4- 修改车辆信息 "); printf("\n 5- 查找区域车辆信息 "); printf("\n 6- 展示所有车辆信息 "); printf("\n 7- 按车主姓名排序 "); printf("\n 0- 退出管理系统并保存数据 \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; }
加载全部内容