请大家帮忙看一下如下程序为什么出错(指针赋值的逻辑错误,已标出),非常期待赐教!
大家好!最近写了一个关于银行单队多服务台排队系统的程序,有个地方调试时提示有逻辑错误(具体内容已在程序中用红颜色标出),但不知道该怎样改,还望高手指教!东西急着用,所以非常渴望高手的点拨和赐教!感激感激!!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
//以下为事件和事件表
typedef struct QCuEvent
{
float OccurTime;
int NType;
int servenum;
float ArrivalTime;
float Duration;
struct QCuEvent *next;
}QCuEvent, *EventList;
//以下为窗口前队列元素
typedef struct QCuElem
{
float ArrivalTime;
float Duration;
struct QCuElem *next;
}QCuElem,*QEptr;
//以下为队列指针
typedef struct {
QEptr front;
QEptr rear;
}QCustomerp,*QCupp;
int CustomerNum=0;//累计客户数
int ServedCu=0;//系统中已办理业务的顾客数
float CloseTime; //关门时间
int windowsnum = 0;//服务窗口数
float a=0;//顾客到达的泊松流指标
float b=0;//顾客服务时间分布的负指数分布指标
int *S;//指向窗口编号数组的指针
int SS=0;//窗口服务状态指数的加和
float *k;//统计窗口服务时间的数组
float TotalTime=0;//累计客户逗留时间
float TotalserviceTime=0;//累计客户服务时间
int l;//系统中的排队长
float Ls;//系统中的平均顾客数
float Lq;//平均队长
float T=0;//时间参数
//涉及的主要操作函数
Status OpenForDay(EventList &ev, QCuEvent en, QCupp &q);
Status CustomerArrived(EventList &ev, QCupp &q, QCuEvent en);
Status CustomerDeparture(EventList &ev, QCupp &q, QCuEvent en);
void CloseForDay();
//基本操作函数
Status InitCuQueue(QCupp q);
float randomnum(float x);
Status OrderInser(EventList &ev, QCuEvent en);
Status EnCuQueue(QCupp q,QEptr Q);
int QLength(QCupp q);
Status DelFirstEvent(EventList &ev);
Status GetQhead(QCupp q,QEptr Q);
Status DestoryQueue(QCupp q);
void Print_QStatus(QCupp q);
void Bank_SimulationFunc();
void test(char str[]);
void main() {
QCuEvent en={0,0,0,0,0,NULL};
EventList ev;
QCupp QCu;
int j;
OpenForDay(ev, en, QCu );
while (ev->next)
{
en.NType = ev->next->NType;
en.OccurTime = ev->next->OccurTime;
en.servenum=ev->next->servenum;
en.ArrivalTime=ev->next->ArrivalTime;
en.Duration=ev->next->Duration;
en.next=NULL;
DelFirstEvent(ev);
if (en.NType == 0)
{
CustomerArrived(ev, QCu, en);
Ls+=(en.OccurTime-T)*(CustomerNum-ServedCu-1);
}
else
{
CustomerDeparture(ev, QCu, en);
Ls+=(en.OccurTime-T)*(CustomerNum-ServedCu+1);
}
Lq+=(en.OccurTime-T)*l;
Print_QStatus(QCu);//在这里调用计算队长的函数时有误,见如下 Print_QStatus函数以及int QLength(QCupp q)函数中所提示的逻辑错误,但是在CustomerArrived函数中调用时却是可以的,见如下CustomerArrived函数中的红色标注
printf("Ls为%f\n",Ls);
printf("Lq为%f\n",Lq);
for (j=1;j<=windowsnum;j++)
printf("此时窗口忙闲状态为%d\n",S[j]);
printf("系统中现有顾客人数为%d\n",CustomerNum-ServedCu);
T=en.OccurTime;
}
free(S);
free(ev);
free(QCu);
CloseForDay();
}
Status OpenForDay(EventList &ev, QCuEvent en, QCupp &q)
{ float temp = 0;
int i=0;
printf("请输入顾客到达的泊松流指标a:");
scanf("%f",&a);
printf("请输入顾客服务时间的负指数分布指标b:");
scanf("%f",&b);
printf("请输入营业时间(单位:分钟):");
scanf("%f",&temp);
CloseTime=temp;
en.OccurTime=0;
en.NType=0;
en.ArrivalTime=0;
en.Duration=0;
en.servenum=0;
en.next =NULL;
if((ev=(EventList) malloc(sizeof(QCuEvent)))==NULL)
{
printf("ev内存分配出错!");
exit(1);
}
ev->next = NULL;
OrderInser(ev,en);
printf("请输入办理业务的窗口数i(至少1个):");
scanf("%d", &windowsnum);
if((S=(int* )malloc((windowsnum+1)*sizeof(int)))==NULL)
{
printf("S内存分配出错!");
exit(1);
}
S[0]=1;
for(i=1;i<=windowsnum;i++)
{
S=0;
SS+=S;
}
if((k=(float* )malloc((windowsnum+1)*sizeof(float)))==NULL)
{
printf("k内存分配出错!");
exit(1);
}
for(i=1;i<=windowsnum;i++)
{
k=0;
}
if((q=(QCupp ) malloc(sizeof(QCustomerp)))==NULL)
{
printf("q内存分配出错!");
exit(1);
}
InitCuQueue(q);
return OK; }
Status CustomerArrived(EventList &ev, QCupp &q, QCuEvent en)
{
float intertime;
float durtime;
float t;
int i;
QCuEvent enTemp;
QEptr Q;
test("顾客到达处理<<<<<<<<");
printf("到达时间%f\n", en.OccurTime);
CustomerNum ++;
intertime =randomnum(a); // 到达时间间隔
durtime = randomnum(b); // 服务时间间隔
t = en.OccurTime + intertime;
enTemp.OccurTime = t;
enTemp.NType = 0;
enTemp.servenum=0;
enTemp.ArrivalTime=t;
enTemp.Duration=0;
enTemp.next = NULL;
if (t < CloseTime) OrderInser(ev, enTemp);
printf("下一个顾客到达时间%f\n",t);
if(SS<windowsnum)
{
enTemp.OccurTime = en.OccurTime + durtime;
enTemp.NType = 1;
for(i=0;S;i++)
enTemp.servenum=i+1;
enTemp.ArrivalTime=en.OccurTime;
enTemp.Duration=durtime;
enTemp.next = NULL;
if(enTemp.OccurTime<CloseTime)
{OrderInser(ev, enTemp);
S=1;
SS+=S;
}
}
else
{ Q=(QEptr) malloc(sizeof(QCuElem));
if(Q==NULL)
{
printf("内存分配出错!");
exit(1);
}
Q->ArrivalTime = en.OccurTime;
Q->Duration = durtime;
Q->next = NULL;
EnCuQueue(q,Q);
free(Q);
printf("此时队长为%d.",QLength(q));//在这里调用计算队长的函数时是正常的,没有出现逻辑错误,见如下int QLength(QCupp q)函数
}
return OK;
}
Status CustomerDeparture(EventList &ev, QCupp &q, QCuEvent en)
{ QCuEvent enTemp;
QEptr customer;
test(">>>>>>>>顾客离开处理");
printf("离开时间%f\n",en.OccurTime);
ServedCu++;
printf("系统中已办理业务的人数为%d\n",ServedCu);
printf("此时寻找服务的窗口编号为%d\n", en.servenum);
TotalTime += en.OccurTime - en.ArrivalTime;
TotalserviceTime+=en.Duration;
k[en.servenum]+=en.Duration;
if (QLength(q)>=1)
{ customer=(QEptr) malloc(sizeof(QCuElem));
if(customer==NULL)
{
printf("内存分配出错!");
exit(1);
}
GetQhead (q,customer);//获得队列中的首元素
enTemp.OccurTime = en.OccurTime + customer->Duration;
enTemp.NType = 1;
enTemp.servenum=en.servenum;
enTemp.ArrivalTime=customer->ArrivalTime;
enTemp.Duration=customer->Duration;
enTemp.next=NULL;
if(enTemp.OccurTime>CloseTime)
{ DestoryQueue(q);
S[en.servenum]=0;
}
else
{OrderInser(ev, enTemp);}
free(customer);
}
else
{
S[en.servenum]=0;
SS=SS-1;
}
return OK; }
void CloseForDay()
{
int i;
printf("***************************************\n");
printf("*\n");
printf("* 系统中的所有顾客数为:%d\n", CustomerNum);
printf("* 系统中已办理业务的顾客数为:%d\n", ServedCu);
printf("* 所有顾客在系统中逗留的总时间:%f分钟\n", TotalTime);
printf("* 平均每人在系统中的逗留时间:%f\n",(float)TotalTime/(float)CustomerNum);
printf("* 所有顾客业务办理的总时间:%f分钟\n", TotalserviceTime);
printf("* 平均服务时间:%f\n",(float)TotalserviceTime/(float)CustomerNum);
float m=(float)TotalTime/(float)CustomerNum;
float n=(float)TotalserviceTime/(float)CustomerNum;
printf("* 平均等待时间:%f\n",(m-n));
printf("* 系统中的平均顾客数为:%f\n",(float)Ls/(float)TotalTime);
printf("* 平均排队长为:%f\n", (float)Lq/(float)TotalTime);
for(i=1;i<=windowsnum;i++)
printf("第%d个窗口的繁忙概率为:%f\n",i,(float)k/(float)TotalTime);
printf("*\n");
printf("***************************************\n");
}
float randomnum(float x)//随机数生成函数
{
float z,y;
srand((float)time(NULL));
y=1/x;
z=rand()%1001*0.001;
if((z!=0)&&(z!=1))
return(-y*log(z));
return OK;
}
Status OrderInser(EventList &ev, QCuEvent en)//单链表事件插入
{
EventList entemp,qtemp ;
entemp=(EventList) malloc(sizeof(QCuEvent));
if(entemp==NULL)
{
printf("内存分配出错!");
exit(1);
}
entemp->OccurTime=en.OccurTime;
entemp->NType=en.NType;
entemp->servenum=en.servenum;
entemp->ArrivalTime=en.ArrivalTime;
entemp->Duration=en.Duration;
entemp->next=NULL;
if (!ev->next)
{
ev->next=entemp;
return OK;
}
{
qtemp=ev;
while(qtemp->next&&qtemp->next->OccurTime < en.OccurTime)
{
qtemp=qtemp->next;
}
entemp->next=qtemp->next;
qtemp->next=entemp;
return OK;
}
return OK;
}
Status InitCuQueue(QCupp q)//初始化队列
{
q->front = (QEptr) malloc(sizeof(QCuElem));
if(q->front==NULL)
{
printf("内存分配出错!");
exit(1);
}
q->front->next = NULL;
q->rear = q->front;
return OK;
}
Status EnCuQueue(QCupp q,QEptr Q)//把元素插入队列函数
{
q->rear->next = Q;
q->rear = Q;
return OK;
}
int QLength(QCupp q)//求队长
{
QEptr qtemp;//队列元素指针,指向的元素和到达时间、服务时间和自身结构指针
int i=0;
qtemp = q->front->next;
while(qtemp)
{
qtemp = qtemp->next;//调试时在主函数中调用此函数时提示此处有逻辑错误,为:表达式不能被计算(即Expression cannot be evaluated)
i++;
}
return i;
}
Status DelFirstEvent(EventList &ev)//删除事件表中的第一个结点
{
EventList p;
p = ev->next;
ev->next = p->next;
return OK;
}
Status GetQhead(QCupp q, QEptr Q)//获得队头元素
{
Q->ArrivalTime = q->front->next->ArrivalTime;
Q->Duration = q->front->next->Duration;
Q->next=NULL;
return OK;
}
Status DestoryQueue(QCupp q)//摧毁队列并释放
{ QEptr p;//队列元素指针
while(q->front->next)
{
p = q->front->next;
q->front->next = p->next;
}
q->front->next =NULL;
q->rear = q->front;
return OK;
}
void Print_QStatus(QCupp q)//输出队长
{
l = QLength(q);
printf("队长为:%d", l);
printf("\n");
}
void test(char str[])
{ printf("--%s--\n",str);
}