#include "graphics.h"
#include <malloc.h>
#include "time.h"
#include "stdio.h"
#define M 10000
#define MAXNUM 1000
#define NIL 0
typedef struct SLNode
{
int Data;
struct SLNode *Next;
} slnodetype;
typedef struct /* 定义带头结点队列链表*/
{ slnodetype *Head;
slnodetype *Rear;
} slqtype;
int bankmoney=M;
int InitiateSLQueue(slqtype *client);
int AppendSLQueue(slqtype *client, int money);
int DeleteSLQueue(slqtype *client);
int inmoney(int money,int bankmoney);
int takemoney(int money, int bankmoney);
int main()
{ int i=0,n,flag;
slqtype client1,client2;
int money;
double var,avertime,time,time1;
clock_t start,end,begin;
InitiateSLQueue(&client1); /*初始化队列1*/
InitiateSLQueue(&client2); /*初始化队列2*/
printf("Now the bank has 10000 yuan!\n");
loop:
printf("Do you want to input money or take money? input 0 or 1?\n "); /*0为存款,1为取款*/
scanf("%d",&flag);
start=clock();
printf("please input your money.\n");
scanf("%d",&money);
i++;
n=i;
AppendSLQueue(&client1,money); /*进入队列1中*/
if(flag==0) /* 存款程序 */
{
bankmoney=inmoney(money,bankmoney); /*调用存款函数*/
DeleteSLQueue(&client1); /*删除此客户*/
end=clock();
/*time1=time1+(double)(end-start)/18.2;*/
printf("The client takes %6.3f seconds!\n", (double)((end-start)/18.2));
printf("Now the bank has %d yuan.\n",bankmoney);
if(1) /* 如果队列2不为空队列,检查队列2中的有没有满足条件的客户*/
DeleteSLQueue(&client2);
printf("窗口2中的人请稍侯!\n");
goto loop;/*检查完转向询问条件*/
}
if (flag==1) /*取款程序*/
{
if (bankmoney<=money) /*取钱数大于银行数,排到队列2中去*/
{
printf("Now the bank has %d yuan.\n",bankmoney);
printf("Sorry!Now the bank has not enough money,please wait for a moment!\n");
AppendSLQueue(&client2, money);
goto loop; /*转向询问状态*/
}
if(bankmoney>money) /*取钱数小于银行数,直接取钱并删除客户记录*/
{ bankmoney=takemoney(money,bankmoney);
DeleteSLQueue(&client1);
end=clock();
/* time1=time1+(double)(end-start)/18.2;*/
printf("The client takes %6.3f seconds.\n", (double)((end-start)/18.2));
printf("Now the bank has %d yuan.\n",bankmoney);
}
}
goto loop;
return 0;
}
int InitiateSLQueue(slqtype *client) /*初始化队列*/
{
if ((client->Head=(slnodetype *)malloc(sizeof(slnodetype)))==NULL)
{
printf("\ failed!");
return 0;
}
client->Rear=client->Head;
client->Head->Next=NULL;
return 1;
}
int AppendSLQueue(slqtype *client, int money) /*入队列函数*/
{
slnodetype *p;
if((p=(slnodetype *)malloc(sizeof(slnodetype)))==NULL)
{ printf("\n failed!");
return 0;
}
p->Data=money;
p->Next=NULL;
client->Rear->Next=p;
client->Rear=p; /*修改尾指针*/
return 1;
}
int DeleteSLQueue(slqtype *client) /*出队列函数*/
{
slnodetype *p,*s;
if((p=(slnodetype *)malloc(sizeof(slnodetype)))==NULL)
{ printf("\n failed!");
return 0;
}
if((s=(slnodetype *)malloc(sizeof(slnodetype)))==NULL)
{ printf("\n failed!");
return 0;
}
if(client->Head->Next==NULL) return NIL; /*空队列*/
if(client->Head->Next==client->Rear) /*只有一个数据的队列*/
{ p=client->Rear;
free(p);
client->Rear=client->Head;
client->Head->Next=NULL;
}
if(client->Head->Next!=client->Rear) /*多个数据的队列*/
{
for(p=client->Head->Next;p!=client->Rear;p=p->Next) /*队列2中的第一个客户指针给client,检查到最后一个*/
{
if(p->Data<bankmoney) /*客户取款*/
{ bankmoney=takemoney(p->Data,bankmoney); /*调用取款函数*/
s=p;
free(s);
/* end=clock();
time1=time1+(double)(end-start)/18.2;
printf("\1:takes %6.3f seconds.\n", (double)((end-start)/18.2)); */
printf("Now the bank has %d yuan.\n",bankmoney);
}
}
}
}
int inmoney(int money,int bankmoney) /*存款函数*/
{ int a=bankmoney,b=money;
a=a+b;
return(a);
}
int takemoney(int money, int bankmoney) /*取款函数*/
{
int c=bankmoney,d=money;
c=c-d;
return (c);
}