?? menu3.cpp
字號:
tmp=tmp->next;
}
return head;
}
/*-----------------------------------------------------------------*
*函數: sort() *
*參數: 整型,表結構數組 *
*作用: 排序成績記錄 *
*返回: 無 *
*-----------------------------------------------------------------*/
void sort(int table,sys sort_grade[100])
{
int i,j,mid,low,high;
sys temp;
if(table==9)
{
for(i=0;sort_grade[i].schead!=NULL;i++)
{
temp=sort_grade[i];
low=0;high=i;
//查找插入點
do
{
mid=(low+high)/2;
if(sort_grade[mid].schead->SC_GRADE>temp.schead->SC_GRADE)
{
low=mid+1;//步進
}
else
{
high=mid-1;//步退
}
}while(low<=high);
for(j=i;j>low;j--)
{
sort_grade[j]=sort_grade[j-1];
}
sort_grade[low]=temp;
}
}
if(table==11)
{
for(i=0;sort_grade[i].joinactivityhead!=NULL;i++)
{
temp=sort_grade[i];
low=0;high=i;
//查找插入點
do
{
mid=(low+high)/2;
if(sort_grade[mid].joinactivityhead->J_GRADE<temp.joinactivityhead->J_GRADE)
{
low=mid+1;//步進
}
else
{
high=mid-1;//步退
}
}while(low<=high);
for(j=i;j>low;j--)
{
sort_grade[j]=sort_grade[j-1];
}
sort_grade[low]=temp;
}
}
}
QUEUE *createqueue()
//創建窗口隊列
{
QUEUE *queuehead;
queuehead=(QUEUE *)malloc(sizeof(QUEUE));
STUINFO *point;
point=(STUINFO *)malloc(sizeof(STUINFO));
point->next=NULL; //結點及隊列初始化
queuehead->rear=point;
queuehead->front=point;
queuehead->nodeno=0;
return queuehead;
}
int selectqueue(QUEUE *a[],int i)
//參數為窗口頭數組名及窗口個數,返回最少人數窗口,無窗口返回0
{
int temp=a[0]->nodeno,k=0;
if(a==NULL)
return 0;
for(int j=1;j<i;j++)
{
if(temp>a[j]->nodeno)
{
temp=a[j]->nodeno;
k=j;
}
}
return k;
}
int isempty(QUEUE *q)
//判斷鏈隊列q是否為空,為空返回1,否則返回0
{
if (q->front==q->rear)
return(1);
else
return(0);
}
STUINFO *front(QUEUE *q)
//返回鏈隊列q的隊頭元素的地址
{
if (isempty(q))
{
printf("queue is empty\n");
return NULL;
}
else
return(q->front->next);
}
int dequeue(QUEUE *q)
//將鏈隊列q的隊頭元素出隊列,成功返回1,失敗返回0
{ STUINFO *s;
if (isempty(q))
{
printf("queue is empty\n");
return 0;
}
else
{
s=q->front;
q->front=q->front->next;
q->nodeno--;
free(s);
return 1;
}
}
TM addtime(TM x,int y)
//處理時間的加法函數 ,y限制為分鐘數
{
int temp;
x.tm_min=x.tm_min+y;
temp=x.tm_min/60;
x.tm_min=x.tm_min%60;
x.tm_hour=x.tm_hour+temp;
return x;
}
int gethour()
{
int temp;
scanf("%d",&temp);
while(temp<=0||temp>=24)
{
printf("時間不合法,請重新輸入!\n");
scanf("%d",&temp);
}
return temp;
}
int getmin()
{
int temp;
scanf("%d",&temp);
while(temp<=0||temp>=60)
{
printf("時間不合法,請重新輸入!\n");
scanf("%d",&temp);
}
return temp;
}
TM getwaitime(TM arrtime,TM logtime)
//獲取等待時間
{
TM restime;
restime.tm_hour=logtime.tm_hour-arrtime.tm_hour;
if(logtime.tm_min<arrtime.tm_min)
{
restime.tm_hour-=1;
logtime.tm_min+=60;
}
restime.tm_min=logtime.tm_min-arrtime.tm_min;
return restime;
}
int cmptime(TM x,TM y)
//對兩個時間進行比較,如果左邊大于右邊返回1,否則返回0
{
if(x.tm_hour>y.tm_hour)
return 1;
else if(x.tm_hour<y.tm_hour)
return 0;
else if(x.tm_min>y.tm_min)
return 1;
else return 0;
}
void dequeueall(QUEUE *a[],TM y,int i,int j)
{
int k;
extern int registtime;
extern TRANSA action[MAXSIZE];
extern int actnum;
for(k=0;k<j;k++)
{
if(!isempty(a[k])&&!cmptime(a[k]->front->next->logtime,y))
{
action[actnum].acttype=2;
action[actnum].haptime=a[k]->front->next->logtime;
strcpy(action[actnum].obj,a[k]->front->next->SNO);
action[actnum].num=k+1;
action[actnum].nodeno=a[k]->nodeno-1;
action[actnum].waitime=a[k]->front->next->waitime;
actnum++;
dequeue(a[k]);
}
}
}
int enqueue(QUEUE *a[],int i,int j,STUINFO x)
//將元素x放入鏈隊列q的隊尾,成功返回1,失敗返回0
{
QUEUE *q;
q=a[i];
extern int registtime;
extern TRANSA action[MAXSIZE];
extern int actnum;
dequeueall(a,x.arrtime,i,j); //將所有此時應該出隊的節點出隊并保留事件
if(isempty(q))
{
if((q->rear->next=(STUINFO *)malloc(sizeof(STUINFO)))==0)
return 0;
q->rear=q->rear->next;
strcpy(q->rear->SNO,x.SNO);
q->rear->arrtime=x.arrtime;
q->rear->winnum=i+1;
q->rear->next=NULL;
q->rear->logtime=addtime(q->rear->arrtime,registtime);
q->rear->waitime.tm_hour=0;
q->rear->waitime.tm_min=registtime;
q->nodeno++;
action[actnum].acttype=1;
action[actnum].haptime=x.arrtime;
strcpy(action[actnum].obj,x.SNO);
action[actnum].num=i+1;
action[actnum].nodeno=q->nodeno;
actnum++;
}
else
{
if((q->rear->next=(STUINFO *)malloc(sizeof(STUINFO)))==0)
return 0;
q->rear->next->logtime=addtime(q->rear->logtime,registtime);
q->rear=q->rear->next;
strcpy(q->rear->SNO,x.SNO);
q->rear->arrtime=x.arrtime;
q->rear->winnum=i+1;
q->rear->next=NULL;
q->rear->waitime=getwaitime(q->rear->arrtime,q->rear->logtime);
q->nodeno++;
action[actnum].acttype=1;
action[actnum].haptime=x.arrtime;
strcpy(action[actnum].obj,x.SNO);
action[actnum].num=i+1;
action[actnum].nodeno=q->nodeno;
actnum++;
}
return 1;
}
int setnull(QUEUE *a[],int k)
//初始化隊列
{
int i=0;
extern int registtime;
extern TRANSA action[MAXSIZE];
extern int actnum;
while(!isempty(a[k]))
{
action[actnum].acttype=2;
action[actnum].haptime=a[k]->front->next->logtime;
strcpy(action[actnum].obj,a[k]->front->next->SNO);
action[actnum].num=k+1;
action[actnum].waitime=a[k]->front->next->waitime;
action[actnum].nodeno=a[k]->nodeno-1;
actnum++;
dequeue(a[k]);
}
i=1;
return i;
}
void swap()
//將事件按時間進行排序
{
int i,j,swaped;
TRANSA temp;
extern int registtime;
extern TRANSA action[MAXSIZE];
extern int actnum;
for(i=1;i<actnum;i++)
{
swaped=true;
for(j=0;j<actnum-i-1;j++)
if(cmptime(action[j].haptime,action[j+1].haptime)==1)
{
temp=action[j+1];
action[j+1]=action[j];
action[j]=temp;
swaped=0;
}
if(swaped) break;
}
}
int registmenu(sys *all)
{
STUINFO temp_node,*head,*headpoint;
int i,j,flag_en,k,temp,temp6,registed=0;
QUEUE *array[10];
TM T;
extern int registtime;
extern TRANSA action[MAXSIZE];
extern int actnum;
for(i=0;i<5;i++) //初始化所有窗口
array[i]=NULL;
printf("\n請輸入窗口個數(窗口個數小于10)\n");
scanf("%d",&i);
printf("請選擇注冊時間(單位:分)\n");
registtime=Force();
for(k=0;k<i;k++) //創建相應個數窗口
{
array[k]=createqueue();
}
headpoint=transport(all);
for(;;)
{
system("cls");
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");
j=Force();
while(j<0||j>5)
{
printf("沒有相應的選項,請重新輸入!\n");
j=Force();
}
switch(j)
{
case 1:
temp=selectqueue(array,i);
printf("請到第%d號窗口排隊!\n",temp+1);
system("pause");
break;
case 2:
actnum=1; //1,2句為防止重復按2
registed=0;
head=headpoint->next;
while(head->next!=NULL)
{
temp=selectqueue(array,i);
strcpy(temp_node.SNO,head->SNO);
temp_node.arrtime.tm_hour=head->arrtime.tm_hour;
temp_node.arrtime.tm_min=head->arrtime.tm_min;
flag_en=enqueue(array,temp,i,temp_node);
if(flag_en==1)
printf("enqueue succeeded!\n");
else printf("enqueue failed!\n");
head=head->next;
}
for(temp6=0;temp6<i;temp6++)
setnull(array,temp6);
registed=1;
getch();
break;
case 3:
if(registed==0)
{
printf("還未開始報名注冊,請先選擇報名注冊!\n");
break;
}
swap();
for(temp6=1;temp6<actnum;temp6++)
{
if(action[temp6].acttype==1)
{
printf("%d:%d ",action[temp6].haptime.tm_hour,action[temp6].haptime.tm_min);
printf("%s到達窗口%d\t",action[temp6].obj,action[temp6].num);
printf("此時節點個數為%d\n",action[temp6].nodeno);
}
else
if(action[temp6].acttype==2)
{
printf("%d:%d ",action[temp6].haptime.tm_hour,action[temp6].haptime.tm_min);
printf("%s在窗口%d完成\t",action[temp6].obj,action[temp6].num);
printf("此時節點個數為%d\n",action[temp6].nodeno);
printf("等待時間為%d:%d\n",action[temp6].waitime.tm_hour,action[temp6].waitime.tm_min);
}
else
printf("unknown transaction!\n");
}
getch();
break;
case 4:
actnum=1;
printf("請輸入您要查詢的時刻:\n");
printf("時:");
T.tm_hour=gethour();
printf("分");
T.tm_min=getmin();
printf("\n");
head=headpoint->next;
while(!cmptime(head->arrtime,T)&&head->next!=NULL)
{
temp=selectqueue(array,i);
strcpy(temp_node.SNO,head->SNO);
temp_node.arrtime.tm_hour=head->arrtime.tm_hour;
temp_node.arrtime.tm_min=head->arrtime.tm_min;
flag_en=enqueue(array,temp,i,temp_node);
if(flag_en==1)
printf("enqueue succeeded!\n");
else printf("enqueue failed!\n");
head=head->next;
}
dequeueall(array,T,1,i);
registed=1;
system("cls");
swap();
for(temp6=1;temp6<actnum;temp6++)
{
if(action[temp6].acttype==1)
{
printf("%d:%d ",action[temp6].haptime.tm_hour,action[temp6].haptime.tm_min);
printf("%s到達窗口%d\t",action[temp6].obj,action[temp6].num);
printf("此時該隊學生個數為%d\n",action[temp6].nodeno);
}
else
if(action[temp6].acttype==2)
{
printf("%d:%d ",action[temp6].haptime.tm_hour,action[temp6].haptime.tm_min);
printf("%s在窗口%d完成\t",action[temp6].obj,action[temp6].num);
printf("此時該隊學生個數為%d\n",action[temp6].nodeno);
}
else
printf("unknown transaction!\n");
}
getch();
break;
case 5:
actnum=1;
registed=0;
printf("完成初始化!\n");
getch();
break;
case 6:
head=headpoint->next;
while(head->next!=NULL)
{
printf("%d:",head->arrtime.tm_hour);
printf("%d\n",head->arrtime.tm_min);
head=head->next;
}
getch();
case 0:
printf("\n離開新生報名系統!\n");
return 0;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -