C语言实现单位车辆调度管理

本文实例为大家分享了C语言实现单位车辆调度管理的具体代码,供大家参考,具体内容如下
【C语言实现单位车辆调度管理】单位车辆信息包括:车牌号、车型、载重(客)量,车牌,生产厂家,出厂日期,购买日期,购买单价等;车辆调度信息还应包括:用车人,用车单位,调度人,出车车牌,出车司机,出车用途,出车日期,出车时间,收车日期,收车时间及出车费用等信息等。设计“车辆调度管理系统”,使之能提供以下功能:
系统以菜单方式工作;
车辆调度信息录入功能(车辆调度信息用文件vehicle.txt保存);
车辆信息及车辆调度信息浏览功能;
车辆调度查询和排序功能:
车辆信息及车辆调度信息的删除与修改等功能。
代码如下,完全原创,代码功能完整!!

#include #include #include #include int feature; //定义输入的功能选项char now_date[12]; //定义系统当前日期存储缓冲区SYSTEMTIME sys; //定义系统时间变量 //定义车辆数据结构typedef struct Vehicle{char *ver_id; //定义车辆编号char *ver_no; //定义车辆牌号char *weight; //定义车辆对应载重量char *ver_trand; //定义车牌char *factory; //定义车辆生产厂家char *outdate; //定义车辆出厂日期char *buydate; //定义车辆购买日期char *section; //定义车辆调用单位char *purpose; //定义车辆出车用途char *usedate; //定义车辆出车日期char *usetime; //定义车辆调度时长char *useprice; //定义车辆出车费用char *price; //定义车辆购买单价char *ver_type; //定义车辆类型char *ver_status; //定义车辆状态char *state; //定义车辆运营状态char *service; //定义车辆维修情况char *violation; //定义车辆违章情况char *ver_last_date; //定义车辆归还日期char *lender_name; //定义租车人姓名char *lender_id; //定义租车人身份证号码char *return_time; //定义归还时间}Vehicle,*VData; //定义车辆链表typedef struct VNode{Vehicle data; struct VNode *next; }VNode,*VehicleList; //声明函数VehicleList select_vehicle(VehicleList L, char * key); //定义车辆查询函数VehicleList InitList(VehicleList L); //定义链表初始化函数VehicleList LoadData(VehicleList L, char *filename); //定义信息读取函数void SaveData(VehicleList L, char *filename); //定义存储数据函数int main_menu(); //定义主菜单int dispatch(VehicleList L, char *lender_name, char *lender_id, char *return_time, char *section,char *purpose,char *usedate,char *useprice,char *usetime); //定义车辆调度函数void back_vehicle(VehicleList L, char * key, char *lender_name, char *lender_id); //定义车辆归还函数void list_all(VehicleList L); //定义车辆总览函数VehicleList select_vehicle(VehicleList L, char * key); //定义车辆查询函数VehicleList register_vehicle(VehicleList L); //定义车辆登记函数void delete_vehicle(VehicleList L, char * key); //定义车辆删除函数int datecmp(char *a,char *b); //定义日期比较函数int quit(); //定义退出函数char *getS(); //定义字符串输入接口函数char *getS(){char *c; char s[30]; scanf("%s",s); c=(char*)malloc(sizeof(char)*(strlen(s)+1)); strcpy(c,s); return c; } //定义链表初始化函数VehicleList InitList(VehicleList L){L=(VehicleList)malloc(sizeof(VNode)); L->next=NULL; return L; } //定义比较日期大小函数int datecmp(char *a,char *b){int i; for(i=0; i<11; i++){if(a[i]>b[i])return 1; else if(a[i]==b[i])continue; elsereturn -1; }return 0; } //定义从文件加载数据函数VehicleList LoadData(VehicleList L,char *filename){VehicleList p,q; //Vehicle v,v2,v3; Vehicle vdata; FILE *fp; //定义文件指针char ver_id[20],ver_no[20],weight[20],ver_trand[20],factory[20],outdate[20]; char buydate[20],price[20],ver_type[20],ver_status[20],ver_date[20],usetime[20]; char section[20],purpose[20],usedate[20],useprice[20]; char state[20],service[20],violation[20]; char lender_name[20],lender_id[20],return_time[20]; q=L; if((fp = fopen(filename,"r+")) == NULL){printf("文件数据读取失败!\n"); }else{while (!feof(fp))//将文件中数据读取到链表中{fscanf(fp,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\r\n",ver_id,ver_no,weight,ver_trand,factory,outdate,buydate,price,ver_type,ver_status,ver_date,purpose,usedate,usetime,useprice,state,service,violation,lender_name,lender_id,return_time); vdata.ver_id=(char*)malloc(sizeof(char)*(strlen(ver_id)+1)); vdata.ver_no=(char*)malloc(sizeof(char)*(strlen(ver_no)+1)); vdata.weight=(char*)malloc(sizeof(char)*(strlen(weight)+1)); vdata.ver_trand=(char*)malloc(sizeof(char)*(strlen(ver_trand)+1)); vdata.factory=(char*)malloc(sizeof(char)*(strlen(factory)+1)); vdata.outdate=(char*)malloc(sizeof(char)*(strlen(outdate)+1)); vdata.buydate=(char*)malloc(sizeof(char)*(strlen(buydate)+1)); vdata.price=(char*)malloc(sizeof(char)*(strlen(price)+1)); //vdata.section=(char*)malloc(sizeof(char)*(strlen(section)+1)); vdata.purpose=(char*)malloc(sizeof(char)*(strlen(purpose)+1)); vdata.usedate=(char*)malloc(sizeof(char)*(strlen(usedate)+1)); vdata.usetime=(char*)malloc(sizeof(char)*(strlen(usetime)+1)); vdata.useprice=(char*)malloc(sizeof(char)*(strlen(useprice)+1)); vdata.ver_type=(char*)malloc(sizeof(char)*(strlen(ver_type)+1)); vdata.state=(char*)malloc(sizeof(char)*(strlen(state)+1)); vdata.service=(char*)malloc(sizeof(char)*(strlen(service)+1)); vdata.violation=(char*)malloc(sizeof(char)*(strlen(violation)+1)); vdata.ver_status=(char*)malloc(sizeof(char)*(strlen(ver_status)+1)); vdata.ver_last_date=(char*)malloc(sizeof(char)*(strlen(ver_date)+1)); vdata.lender_name=(char*)malloc(sizeof(char)*(strlen(lender_name)+1)); vdata.lender_id=(char*)malloc(sizeof(char)*(strlen(lender_id)+1)); vdata.return_time=(char*)malloc(sizeof(char)*(strlen(return_time)+1)); strcpy(vdata.ver_id,ver_id); strcpy(vdata.ver_no,ver_no); strcpy(vdata.weight,weight); strcpy(vdata.ver_trand,ver_trand); strcpy(vdata.factory,factory); strcpy(vdata.outdate,outdate); strcpy(vdata.buydate,buydate); strcpy(vdata.price,price); strcpy(vdata.purpose,purpose); strcpy(vdata.usedate,usedate); strcpy(vdata.usetime,usetime); strcpy(vdata.useprice,useprice); strcpy(vdata.ver_type,ver_type); strcpy(vdata.ver_status,ver_status); strcpy(vdata.state,state); strcpy(vdata.service,service); strcpy(vdata.violation,violation); strcpy(vdata.ver_last_date,ver_date); strcpy(vdata.lender_name,lender_name); strcpy(vdata.lender_id,lender_id); strcpy(vdata.return_time,return_time); p=(VehicleList)malloc(sizeof(VNode)); p->data=https://www.it610.com/article/vdata; q->next=p; q=p; }fclose(fp); }q->next=NULL; return L; }//定义存储数据函数void SaveData(VehicleList L, char *filename){VehicleList p; FILE *fp; //定义文件指针p=L; if((fp = fopen(filename,"w+")) == NULL){printf("文件写入失败!\n"); }else{while (p->next != NULL){fprintf(fp,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\r\n",p->next->data.ver_id,p->next->data.ver_no,p->next->data.weight,p->next->data.ver_trand,p->next->data.factory,p->next->data.outdate,p->next->data.buydate,p->next->data.price,p->next->data.usedate,p->next->data.useprice,p->next->data.ver_type,p->next->data.ver_status,p->next->data.usetime,p->next->data.state,p->next->data.service,p->next->data.violation,p->next->data.ver_last_date,p->next->data.lender_name,p->next->data.lender_id,p->next->data.return_time); p=p->next; }} fclose(fp); } //定义主菜单int main_menu(){int i; printf("******************************************************\n"); printf("*汽车调度程序*\n"); printf("*韩力毅*\n"); printf("******************************************************\n"); printf("*1.汽车调度*\n"); printf("*2.汽车归还*\n"); printf("*3.车辆总况一览*\n"); printf("*4.车辆查询*\n"); printf("*5.新车登记*\n"); printf("*6.车辆注销*\n"); printf("*7.退出系统*\n"); printf("******************************************************\n"); printf("*请选择: 1-7*\n"); printf("******************************************************\n"); printf("请输入您的选项:"); scanf("%d", &i); feature=i; return i; } //定义功能1--汽车调度int dispatch(VehicleList L, char *lender_name, char *lender_id, char *return_time, char *section,char *purpose,char *usedate,char *useprice,char *usetime){char *vtype,*vid; //车型VehicleList p,q1,q2; char tmpdate[20]; int i=0; p=L; q1=L; q2=L; strcpy(tmpdate,now_date); printf("\n请选择您需要的车型(A.大型车 B.中型车 C.小型车 E.返回主菜单 请输入大写字母!):"); vtype = getS(); if (strcmp("E",vtype) == 0) return 0; //如果输入E,返回主菜单printf("\n*************************************本次出车费用为200元*************************************\n"); printf("\n****************************************可选车辆列表******************************************"); printf("\n车辆编号车牌号载重量车牌车辆类型车辆状态上次出车时间(DDYYMM)\n"); while (L->next != NULL){//显示符合条件的在库车辆信息if (strcmp("出车中",L->next->data.ver_status) !=0 && strcmp(vtype,L->next->data.ver_type) ==0){if(datecmp(tmpdate,L->next->data.ver_last_date) > -1)//调用比较未调度时间的函数{q1=L; //获得最长时间没有被调度的车的节点指针strcpy(tmpdate,q1->next->data.ver_last_date); } printf("%6s %10s %-10s %3s %8s %13s %15s\n",L->next->data.ver_id,L->next->data.ver_no,L->next->data.weight,L->next->data.ver_trand,L->next->data.ver_type,L->next->data.ver_status,L->next->data.ver_last_date); }if (strcmp("NEW",L->next->data.ver_last_date) ==0 && strcmp(vtype,L->next->data.ver_type) ==0){i++; //计算新车数量q2=L; //获得新车的节点指针} L=L->next; }L=p; printf("**************************************************************************\n"); printf("请输入车辆编号或输入W智能筛选(输入E返回主菜单):"); vid = getS(); if (strcmp("W",vid) == 0){if(i>0){printf("\n****************************************车辆调度结果****************************************"); printf("\n已调出车辆:%s,载重量:%s,车牌:%s,出车日期:%s\n",q2->next->data.ver_id,q2->next->data.weight,q2->next->data.ver_trand,now_date); printf("**********************************************************************************************\n"); q2->next->data.ver_status="出车中"; //修改车辆状态和上次出车时间q2->next->data.ver_last_date=(char*)malloc(sizeof(char)*(strlen(now_date)+1)); strcpy(q2->next->data.ver_last_date,now_date); strcpy(q2->next->data.lender_name,lender_name); strcpy(q2->next->data.lender_id,lender_id); strcpy(q2->next->data.return_time,return_time); strcpy(q2->next->data.section,section); strcpy(q2->next->data.purpose,purpose); strcpy(q2->next->data.usedate,usedate); strcpy(q2->next->data.useprice,useprice); strcpy(q2->next->data.usetime,usetime); return 1; }else{printf("\n****************************************车辆调度结果****************************************"); printf("\n已调出车辆:%s,载重量:%s,车牌:%s,出车日期:%s\n",q1->next->data.ver_id,q1->next->data.weight,q1->next->data.ver_trand,now_date); printf("**********************************************************************************************\n"); q1->next->data.ver_status="出车中"; //修改车辆状态和上次出车时间q1->next->data.ver_last_date=(char*)malloc(sizeof(char)*(strlen(now_date)+1)); strcpy(q1->next->data.ver_last_date,now_date); strcpy(q1->next->data.lender_name,lender_name); strcpy(q1->next->data.lender_id,lender_id); strcpy(q1->next->data.return_time,return_time); strcpy(q2->next->data.section,section); strcpy(q2->next->data.purpose,purpose); strcpy(q2->next->data.usedate,usedate); strcpy(q2->next->data.useprice,useprice); strcpy(q2->next->data.usetime,usetime); return 1; } }else if ((p=select_vehicle(L,vid)) != NULL){p->next->data.ver_status="出车中"; //修改车辆状态和上次出车时间p->next->data.ver_last_date=(char*)malloc(sizeof(char)*(strlen(now_date)+1)); strcpy(p->next->data.ver_last_date,now_date); strcpy(p->next->data.lender_name,lender_name); strcpy(p->next->data.lender_id,lender_id); strcpy(p->next->data.return_time,return_time); strcpy(q2->next->data.section,section); strcpy(q2->next->data.purpose,purpose); strcpy(q2->next->data.usedate,usedate); strcpy(q2->next->data.useprice,useprice); strcpy(q2->next->data.usetime,usetime); printf("\n****************************************车辆调度结果****************************************"); printf("\n已调出车辆:%s,载重量:%s,车牌:%s,出车日期:%s\n",p->next->data.ver_id,p->next->data.weight,p->next->data.ver_trand,now_date); printf("**********************************************************************************************\n"); return 1; }else if (strcmp("E",vid) == 0){return 0; }else{printf("输入错误,请返回!\n"); return 4; }return 4; } //定义功能2--汽车归还void back_vehicle(VehicleList L, char * key, char *lender_name,char *lender_id){VehicleList p; if ((p=select_vehicle(L,key)) != NULL){if (strcmp(lender_name,p->next->data.lender_name) == 0 &&strcmp(lender_id,p->next->data.lender_id) == 0)//姓名和身份证号输入正确才可还车{if (datecmp(now_date,p->next->data.return_time) < 1)//如果实际归还时间超出预定归还时间,提示到服务台办理{p->next->data.ver_status="可调出"; //恢复车辆状态及租车人信息为初始状态p->next->data.lender_name="N/A"; p->next->data.lender_id="N/A"; p->next->data.return_time="N/A"; printf("\n汽车归还成功!\n\n"); }else{printf("\n归还失败,您的车辆已超期,请到总服务台办理超期还车手续!\n\n"); }}else{printf("\n租车人姓名或身份证号输入有误!\n\n"); } }else{printf("\n没有查询到编号为:%s的车辆信息!\n\n",key); } } //定义功能3--车辆总况一览void list_all(VehicleList L){printf("\n车辆编号车 牌 号载重量车 牌生产厂家出 厂 日 期购 买 日 期购买单价车型车辆状态车辆运营状态车辆维修情况车辆违章情况上次出车时间租车人姓名计划归还时间\n"); while (L->next != NULL){//显示所有已登记车辆信息//if(strcmp("出车中",L->next->data.ver_status) !=0)printf("%6s %10s %6s %8s %9s %11s %12s %9s %4s %10s %10s %12s %13s %13s %18s %14s\n",L->next->data.ver_id,L->next->data.ver_no,L->next->data.weight,L->next->data.ver_trand,L->next->data.factory,L->next->data.outdate,L->next->data.buydate,L->next->data.price,L->next->data.ver_type,L->next->data.ver_status,L->next->data.state,L->next->data.service,L->next->data.violation,L->next->data.ver_last_date,L->next->data.lender_name,L->next->data.return_time); L=L->next; }printf("\n"); system("pause"); } //定义功能4--车辆查询VehicleList select_vehicle(VehicleList L, char * key){VehicleList p; p=L; while (p->next != NULL){//如果查找到符合条件的车辆信息,则返回这个车辆信息节点的指针,strcmp,字符串比较函数if (strcmp(key,p->next->data.ver_id) == 0 || strcmp(key,p->next->data.ver_no) == 0){return p; //P为该车辆信息节点的指针}else{p=p->next; }}return NULL; } //定义功能5--新车登记VehicleList register_vehicle(VehicleList L){VehicleList p,q; Vehicle v; //定义新增车辆结构体int id=1001; //初始车辆编号起始IDchar tmpID[5]; //车辆编号格式化为字符串q=L; while (q->next != NULL){q=q->next; //将指针调至链表尾部以插入新数据}//printf("请输入车辆编号: "); //v.ver_id=getS(); printf("请输入车牌号: "); v.ver_no=getS(); printf("请输入载重量: "); v.weight=getS(); printf("请输入车牌: "); v.ver_trand=getS (); printf("请输入生产厂家: "); v.factory=getS(); printf("请输入出厂日期:(格式,如2012-02-22) "); v.outdate=getS(); printf("请输入购买日期:(格式,如2012-02-22) "); v.buydate=getS(); printf("请输入购买单价: "); v.price=getS(); printf("请选择车辆类型(A/B/C): "); v.ver_type=getS(); printf("请输入车辆运营状态(正常/报废): "); v.state=getS(); printf("请输入车辆维修情况(否/几次): "); v.service=getS(); printf("请输入车辆违章情况(否/几次): "); v.violation=getS(); v.ver_status="可调出"; v.ver_last_date="NEW"; v.lender_name=(char*)malloc(10); strcpy(v.lender_name, "N/A"); v.lender_id=(char*)malloc(10); strcpy(v.lender_id, "N/A"); v.return_time=(char*)malloc(10); strcpy(v.return_time, "N/A"); v.section=(char*)malloc(10); strcpy(v.section,"N/A"); v.purpose=(char*)malloc(10); strcpy(v.purpose,"N/A"); v.usedate=(char*)malloc(10); strcpy(v.usedate,"N/A"); v.useprice=(char*)malloc(10); strcpy(v.useprice,"N/A"); v.usetime=(char*)malloc(10); strcpy(v.usetime,"N/A"); //自动生成车辆编号sprintf(tmpID, "%d", id); while(select_vehicle(L,tmpID) != NULL){id++; sprintf(tmpID, "%d", id); //将id转为字符串存储到tmpID中} v.ver_id=(char*)malloc(sizeof(char)*(strlen(tmpID)+1)); //分配内存空间strcpy(v.ver_id,tmpID); //将tmpID拷贝到车辆信息结构中if(select_vehicle(L,v.ver_no) == NULL){//检查是否已有该车牌号p=(VehicleList)malloc(sizeof(VNode)); //创建新的车辆节点p->data=https://www.it610.com/article/v; q->next=p; //连接新的车辆节点q=p; //将q指针移至最后节点q->next=NULL; //将最后一个节点的next设为NULLprintf("\n成功登记牌号为:%s的车辆,车辆编号为:%s!\n\n",v.ver_no,v.ver_id); }else{printf("\n已存在该车辆!\n\n"); }return L; } //定义功能6--车辆注销void delete_vehicle(VehicleList L, char * key){VehicleList p,q; p=L; if ((p=select_vehicle(L,key)) != NULL){q=p->next; p->next=q->next; //将节点p连接到下下一个节点,即删除找到的节点free(q); printf("\n已注销编号为%s的车辆!\n\n",key); }else{printf("\n没有找到符合条件的车辆!\n\n"); } system("pause"); } //定义功能7--退出系统函数int quit(){char *temp; temp=getS(); //接受用户输入 if(strcmp("Y",temp)==0){return 1; //返回1,为确实退出}else if(strcmp("N",temp)==0){return 0; //返回0,则不退出,并清屏,加载主菜单}else{return 2; //返回2,说明输入错误,任意键返回主菜单} return 2; //默认返回2 } int main(){VehicleList L1,tmpL; char *vehicle_key,*lender_name,*lender_id,*return_time,*section,*purpose,*usedate,*useprice,*usetime; char *filename="vehicle.txt"; //设置数据文件GetLocalTime(&sys); sprintf(now_date,"%4d-%02d-%02d",sys.wYear,sys.wMonth,sys.wDay); L1=(VehicleList)malloc(sizeof(VNode)); L1=InitList(L1); L1=LoadData(L1,"vehicle.txt"); main_menu://SaveData(L1,filename); //每完成一个操作都保存数据到文件,默认选择7程序退出时才保存数据main_menu(); //加载主菜单switch (feature){case 1:{int i; printf("请输入租车人姓名: "); lender_name = getS(); printf("请输入租车人身份证号: "); lender_id = getS(); printf("请输入用车单位: "); section = getS(); printf("请输入出车用途: "); purpose = getS(); printf("请输入出车时长(格式N天): "); usetime = getS(); printf("请输入出车日期(格式YYYY-MM-DD,如 %s): ",now_date); usedate = getS(); printf("请输入计划归还时间(格式YYYY-MM-DD,如 %s): ",now_date); return_time = getS(); useprice = "200"; if (datecmp(now_date,return_time) > 0){printf("\n归还时间输入错误,最少需要租一天,即时间应该大于等于 %s\n\n",now_date); system("pause"); system("cls"); goto main_menu; }i=dispatch(L1,lender_name,lender_id,return_time,section,purpose,usedate,useprice,usetime); //调用车辆调度函数if (i==0){system("cls"); goto main_menu; }else{system("pause"); system("cls"); goto main_menu; }} case 2:{printf("请输入汽车编号: "); vehicle_key = getS(); printf("请输入租车人姓名: "); lender_name = getS(); printf("请输入租车人身份证号: "); lender_id = getS(); back_vehicle(L1,vehicle_key,lender_name,lender_id); //调用车辆归还函数system("pause"); system("cls"); goto main_menu; } case 3:{list_all(L1); //调用车辆总览函数system("cls"); goto main_menu; } case 4:{printf("请输入汽车编号或车牌号: "); vehicle_key=getS(); tmpL=select_vehicle(L1,vehicle_key); //调用车辆查找函数if (tmpL != NULL)//返回不为空,说明找到了{printf("\n*****************************找到了符合条件的车辆信息****************************\n"); printf("\n车辆编号车 牌 号载重量车 牌车 型车辆状态上次出车时间租车人姓名计划归还时间\n"); printf("\n%6s %10s %6s %8s %4s %10s %10s %18s %14s\n",tmpL->next->data.ver_id,tmpL->next->data.ver_no,tmpL->next->data.weight,tmpL->next->data.ver_trand,tmpL->next->data.ver_type,tmpL->next->data.ver_status,tmpL->next->data.ver_last_date,tmpL->next->data.lender_name,tmpL->next->data.return_time); system("pause"); }else{printf("\n\n没有找到符合条件的车辆信息!\n\n"); system("pause"); //按任意键继续}system("cls"); goto main_menu; } case 5:{L1=register_vehicle(L1); //调用车辆登记函数system("pause"); system("cls"); goto main_menu; } case 6:{vehicle_key = "0008"; printf("请输入需要注销的车辆编号: "); vehicle_key=getS(); delete_vehicle(L1,vehicle_key); //调用车辆注销函数system("cls"); goto main_menu; } case 7:{int temp; printf("您确定要退出系统?输入Y确定,输入N返回主菜单\n"); temp = quit(); if(1 == temp){SaveData(L1,filename); //保存数据return 0; }else if(0 == temp){system("cls"); goto main_menu; //如果返回真则退出,否则返回主菜单}else{printf("输入错误!"); system("pause"); }} default:{system("cls"); getchar(); goto main_menu; //如果输入不在1-7内,则返回主菜单} } return 0; }

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

    推荐阅读