c语言实习报告机房机位预约模拟
C语言课程设计院系:工程学院姓名:杨文和学号:20051003679班号:05205222指导教师:王老师日期:2007年9月 机房机位预约模拟 一.题目要求20台机器,从早八点到晚八点,每两个小时一个时间段.需要实现的功能:(1) 查询,根据输入的时间,输出机位信息.(2) 机位预定,根据输入的日期和时间段查询是否有空机位,若有则预约,若无则提供最近空机时间段.另:若用户要求在非空时间上机,则将用户信息插入该时间段的等待列表.(3) 退出预定,根据输入的时间,撤消该时间的饿预定.(4) 查询是否有等待信息,若有则按顺序显示联系方式,若无则显示提示信息.二.需求分析根据题目要求,需要提供机位信息和预约信息,应该用链表来存储,应提供指针的操作:在程序中,需要查询是否有空机位和等待者和处理预约和取消预约问题,应提供查询,显示,预定,删除,修改等操作;另外还要提供键盘式选择菜单实现功能选择.三.总体设计现在分析整个一下整个系统,根据上面的需求分析,可以将这个系统的设计分为如下六大模块:查询我的预约状态,查询空位,预约,取消预约,排队,查询等待信息.机房机位模拟系统我的预约状态查询空位预约取消预约排队查询等带信息四.详细设计1宏定义#include<stdio.h>#include<stdlib.h>#include<string.h>#define LENGTH 6/*总时段数*/#define MAX 2 #define S(r) (r-8)/2/*计算在哪个时段数*/ struct node int locat; char data10;/*学号,假设为联系方式*/ struct node *next;struct node *head;struct cell int CNum;/*连接在该时段头结点的总机器数目*/ struct node *first;/*指向整个队列的开头*/ struct node *middle;/*指向等待预约队列*/ struct node *last;/*指向整个队列的结尾*/TimeQueueLENGTH;2.主函数输入n,判断n是否是0-6显示一系列功能信息主函数一般设计的比较简洁,只提供输入,处理和输出部分的函数调用.其中个功能模块用菜单方式选择。NY根据n的值调用各功能模块函数【程序】 main() /*主函数*/ int i; for(i=0;i<LENGTH;i+)/*初始化*/ TimeQueuei.CNum=0; TimeQueuei.first=NULL; TimeQueuei.middle=NULL; TimeQueuei.last=NULL; while(1) printf("Please choice your number!:n"); printf("1.Search my booking state 2.Search empty place 3.Book 4.Cancel booking 5.Waiting 6.Search waitingn"); scanf("%d",&i);/*选择功能*/ switch(i) case 1:Inquir();break; /*查询我的状态模块*/ case 2:inquir();break; /*查询空位模块*/ case 3:booking();break; /*预约模块*/ case 4:cancel();break; /*取消预约模块*/ case 5:waiting();break; /*排队模块*/ case 6:inquir_waiting();break; /*查询等待信息模块*/ default:printf("errorn"); 3各功能模块设计(1)查询我的状态模块数据结构我的原始数据是预约者预约的信息,将它储存在用指针处理的链表中。【分析】根据输入的时间来判断应该查询的结构体,把其首地址赋给Rem,然后通过指针的移动,比较输入的学号和存储在该结构体中的Rem->data,来判断我的预约状态。 流程图输入要查询的时间n,判断n是否820YNn=s(n)根据的n值和输入的学号来查找链表中是否有该值和其的位置,从而达到查询出我的状态的目的提示:输入错误,请重新输入【程序】void Inquir() int n; int i; char Infor10; struct node *q; struct node *Rem; printf("Please input the time you want to search!(24 xiao shi zhi between8 and 20 oclock,include 8oclock)n"); scanf("%d",&n); if(n>=8&&n<20) n=S(n); printf("Please input your school-number!n"); scanf("%s",Infor); Rem=TimeQueuen.first; q=Rem; for(i=1;q=Rem,Rem=Rem->next,i+) if(strcmp(Rem->data,Infor)=0)break; if(Rem->locat!=0) printf("The computer number you have during this period period is %dn",Rem->locat); else printf("Sorry,you are still in the waiting queue!");else printf("Error.Please input again.n")(2)查询空机位模块分析根据输入的时间确定n值,然后根据结构体中TimeQueue中的TimeQueuen->Cnum的值,求出最大值与该值的差即为空机位数。流程图输入要查询的时间n判断n是否820Yns(n)N根据n的值判断TimeQueuen->Cnum的值是否小于最大值MAXYN空机位数=MAXTimeQueuen->CNum输出:没有空机位提示:输入错误,请重新输入【程序】 void inquir() int n; printf("Please input the time you want to search.(24 hours 824 oclock,include 8 clock)n"); scanf("%d",&n); if(n>=8&&n<20) n=S(n); if(TimeQueuen.CNum<MAX) printf("There are %d emoty computer.n",MAX-TimeQueuen.CNum); else printf("Sorry!There is no empty computer!n"); else printf("Error.Please input again.n"); (3)预约模块数据结构定义两个指向结构体的指针,用强制类型转换使他们指向动态分配的存储空间的首地址,把预约者的信息储存在其中一个结构体中。分析 在该模块的设计中,要让预约者按预约的顺序储存在结构体中。当预约者的总数小于最大值时,就要用到通过指针的移动来把预约者的信息按顺序储存起来,用两个不同的指针分别指向链表的开始,链表的结尾处。流程图N输入要查询的时间n判断n是否820Y提示:输入错误请重新输入ns(n)N根据n的值判断TimeQueuen->Cnum的值是否小于最大值MAX提示:已经没有空机位输入预约者的学号TimeQueuen.first是否为空YN把预约者的信息插入到链表的表尾把预约者信息储存在链表的第一个结点【程序】void booking()/*/ int n; char Infor10; struct node *Rem; struct node *p; printf("Please input the time you want to book!n"); scanf("%d",&n); if(n>=8&&n<20) n=S(n); if(TimeQueuen.CNum<MAX) printf("Please input your No.!n"); scanf("%s",Infor); if(TimeQueuen.first=NULL) Rem=(struct node *)malloc(sizeof(struct node); Rem->locat=1; strcpy(Rem->data,Infor); Rem->next=NULL; TimeQueuen.first=Rem; TimeQueuen.last=Rem; TimeQueuen.CNum+; printf("Succeed to book!n"); else Rem=(struct node *)malloc(sizeof(struct node); strcpy(Rem->data,Infor); Rem->next=NULL; p=TimeQueuen.last; Rem->locat=TimeQueuen.CNum+1; printf("%d",Rem->locat); TimeQueuen.last=Rem; p->next=Rem; TimeQueuen.CNum+; printf("Succeed to book!n"); else printf("There is no empty computer!n"); else printf("Error.Please input again.n"); (4)排队模块 分析该模块主要是针对是否要排队和如何排队设计的。当预约者总数小于最大值时提示不用排队。当预约者的总数大于最大值时就需要排队。把排队者的信息储存在由指向结构体的指针指向的动态分配的存储区域,然后连接到TimeQueuen的表尾。用指向等待队列的指针指向排队的第一人,通过指针的移动,用指向表尾的指针指向真个链表的结尾。流程图N输入要查询的时间n判断n是否820Y提示:输入错误请重新输入ns(n)根据n的值判断TimeQueuen->Cnum的值是否小于最大值MAXNYNY提示:还有空机位,不需要排队。Cnum是否MAX把排队者的信息连接到链表的结尾处,即排队的第一个把排队者的信息联接到队尾处。【程序】 void waiting()/*/ int n; char Infor10; struct node *Rem; struct node *p; printf("Please input the time you want to wait!n"); scanf("%d",&n); if(n>=8&&n<20) n=S(n); if(TimeQueuen.CNum>=MAX) printf("Please input your No!n"); scanf("%s",Infor); if(TimeQueuen.CNum)=MAX) Rem=(struct node *)malloc(sizeof(struct node); strcpy(Rem->data,Infor); Rem->next=NULL; Rem->locat=0; p=TimeQueuen.last; TimeQueuen.last=Rem; p->next=Rem; TimeQueuen.middle=Rem; TimeQueuen.CNum+; printf("Succeed to in the queue。n"); else Rem=(struct node *)malloc(sizeof(struct node); strcpy(Rem->data,Infor); Rem->next=NULL; Rem->locat=0; p=TimeQueuen.last; TimeQueuen.last=Rem; p->next=Rem; TimeQueuen.CNum+; printf("Succeed to in the queue。n"); else printf("There is still have empty computer。No wait!"); else printf("Error.Please input again.n"); (5)取消预约模块分析 要取消预约,有两种情况,第一是从有机位的人要取消预约,在该种情况下,排队的第一人的位置就插入到该取消者的位置。而指向等待队列首地址的指针就指向原来排队的第二人。第二种情况就是取消预约的人本来就在等待队列中,只需要直接删除该结点即可。流程图N输入要查询的时间n判断n是否820 Y提示:输入错误请重新输入ns(n)输入学号,查找该链表中该信息的位置i,i是否不大于MAXNY退出预约,排队者的第一人插入该预约者的位置。 退出预约【程序】void cancel() int n; int i;/*记载删除点的队列位置*/ char Infor10; struct node *Rem; struct node *q; struct node *p; printf("Please input the time you have booked.n); scanf("%d",&n); if(n>=8&&n<20) printf("Please input your No.!n"); scanf("%s",Infor); n=S(n); Rem=TimeQueuen.first; q=Rem; for(i=1;q=Rem,Rem=Rem->next,i+) if(strcmp(Rem->data,Infor)=0)break; if(i>MAX)/*注意退出预定的情况包括退出在等待队列中预定的情况!*/ if(Rem->next=NULL) q->next=NULL; TimeQueuen.last=q; free(Rem); TimeQueuen.CNum-; printf(“Succeed to out the queue!n"); else q->next=Rem->next; free(Rem); TimeQueuen.CNum-; printf("Succeed to out the queue!n"); else if(TimeQueuen.CNum>MAX) TimeQueuen.middle->locat=Rem->locat; TimeQueuen.middle=TimeQueuen.middle->next; if(i=1) TimeQueuen.first=Rem->next; else q->next=Rem->next; free(Rem); TimeQueuen.CNum-; printf("Succeed to out the queue!n"); else printf("Error.Please input again.n"); (6)查询等待信息模块分析通过输入的时间,判断储存该时间段的结构体链表,通过比较TimeQueuen中Cnum的值与最大值的大小关系就可以得到是否有等待者的信息。流程图N输入要查询的时间n判断n是否820Yns(n)提示:输入错误请重新输入根据n的值判断TimeQueuen->Cnum的值是否小于最大值MAX YYN有等待者没有等待者【程序】void inquir_waiting() int n; struct node *q; printf("Please input the time you want to search the waiter.n"); scanf("%d",&n); if(n>=8&&n<20) n=S(n); if(TimeQueuen.CNum>MAX) printf("The waiter are:n"); q=TimeQueuen.middle; for(;q->next!=NULL;q=q->next) printf("%sn",q->data); printf("%sn",TimeQueuen.last->data); else printf("There is no waiter during this period!n"); else printf("Error.Please input again.n"); 五上机操作: (1)主菜单如下: (2)选3,进入预约模块: (3)选1,进入“查询我的预约状态”模块: (4)选2,进入查询空机位模块: (5)选4,进入取消预约模块: (6)选5,进入排队等待模块: (7)选6,进入查询等待信息模块: (8)选0,结束程序: 按任意键,则结束运行,返回源程序。六总结通过这一周的C语言实习,我在上机操作这一方面有了很大的进步,对C的理论知识的掌握也更加牢固。学会了把所学的知识用于实践。