回 帖 发 新 帖 刷新版面

主题:求助!!!高手请进  银行模拟问题




/*    2010.2.16  编译成功  但运行时出错 
      0x00401394  指令引用的  0x004012f8  内存不能  written   */

typedef    struct
{

    int    time;                                                /*    type=1 入列1, type=2 出列1, type=3 出列2    */
    int    type;

}  Event;                                                  /*    事件类型    */










typedef    struct    LNode
{

    Event   data;
    struct  LNode   *next;

}  LNode,  *ListPtr;                                            /*    结点类型,  指针类型    */













typedef    struct
{

    ListPtr    head;

}  LinkList;                                                       /*    链表类型, 链表有头结点   */











typedef    struct
{

    int    ArriveTime;
    int    ServiceTime;
    int    Cash;

}  Customer;                                                         /*   顾客类型    */








typedef    struct    QNode
{

    Customer    data;
    struct  QNode    * next;

}  QNode,  *QueuePtr;                                                 /*    结点和指针    */










typedef    struct
{

    QueuePtr    front;
    QueuePtr    rear;
    int    length;
                                                                      /*    队列类型    */
}  LinkQueue;






#if !defined( NULL ) 
#define NULL ( 0 ) 
#endif
#include <time.h> 
#include <stdlib.h>
#include <stdio.h>





Event   event;
LinkList    eventlist;
LinkQueue   q[2];
int    TotalTime,  CustomerNum;
int    TotalCash = 10000;
int    CloseTime = 600;
                                                                         /*     全局变量    */








/*    以下是链表函数实现    */




void    InitList( LinkList  &L )                                             /*    初始化链表   */
{
    
     L.head=( ListPtr )malloc( sizeof( LNode ) );
    if( !L.head )     exit( -1 );
    L.head->next = NULL;

}













void    OrderInsert( LinkList  &L, Event  e )                                /*    根据事件发生的时间把事件插入事件表   */
{

    ListPtr  p,q,pre;
    p = ( ListPtr )malloc( sizeof( LNode ) );
    if( !p )    exit( -1 );
    p->data = e;
    q = L.head->next;
    while( q != NULL &&  q->data. time < p->data .time )
    {

        pre = q;
        q = q->next;

    }
    p->next = q;
    pre->next = p;


}















void    DelFirst( LinkList  &L, Event  &e )                          /*    把正发生事件从事件表中删除   */
{

    ListPtr  p;
    p = L.head->next;
    if( p == NULL )    exit( -1 );
    L.head->next = p->next;
    e = p->data;
    free( p );

}















/*    以下是队列函数的实现    */








                                                                               /*   初始化队列  */
void    InitQueue( LinkQueue  &Q )
{

    Q.front = Q.rear = ( QueuePtr )malloc( sizeof( QNode ) );
    if( !Q.front )    exit( -1 );
    Q.front->next = NULL;
    Q.length = 0;


}















void    EnQueue( LinkQueue  &Q,  Customer  e )                              /*    插入队列  */
{

    QueuePtr  p;
    p = ( QueuePtr )malloc( sizeof( QNode ) );
    if( !p )    exit( -1 );
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    Q.length++;


}















void    DeQueue( LinkQueue  &Q,  Customer  &e )                          /*    元素出列    */
{

    QueuePtr  p;
    if( Q.length == 0 )    exit( -1 );
    p = Q.front->next;
    Q.front->next = p->next;
    Q.length--;
    e = p->data;
    if( p == Q.rear )   Q.rear = Q.front;
    free( p );


}














void    GetHead( LinkQueue  &Q,  Customer  &e )                          /*    取头元素    */
{

    QueuePtr  p;
    if( Q.length == 0 )   exit( -1 );
    p = Q.front->next;
    e = p->data;


}


















void    OpenForDay()                                                    /*    银行模拟初始化    */
{

    int  i;
    TotalTime = 0;
    CustomerNum = 0;
    event.time = 0;
    event.type = 1;
    InitList( eventlist );
    OrderInsert( eventlist,  event );
    for( i = 0; i <= 1; i++ )
    {

        InitQueue( q[i] );

    }


}














void    CustomerArrived()                                                            /*    客户入队    */
{

    int    intertime;
    
    Customer  e,  cus;
    Event   eve;

    ++CustomerNum;

    srand( time( 0 ) );
    cus. ServiceTime = 3 + rand()%7;
    cus. ArriveTime = event.time;
    cus. Cash = -5000 + rand()%10000;
    intertime = 5 + rand()%10;

    EnQueue( q[0], cus );
    if( q[0].length == 1 )
    {

        if( TotalCash + cus. Cash >= 0 )
        {

            eve. time = event.time + cus. ServiceTime;
            eve. type = 2; 
            OrderInsert( eventlist,  eve );
        }
        else
        {

            DeQueue( q[0], e );
            EnQueue( q[1], e );

        }
    }

    eve. time = event.time + intertime;
    eve. type = 1;
    if( eve. time < CloseTime )
    {
        OrderInsert( eventlist,  eve );

    }


}















void    CustomerDepature_Q1()                                                            /*     从队列1中删除   */
{

    int  TempCash;   
    int  count;
    Event  t;
    Customer  cus,  e,  next_customer;

    DeQueue( q[0],  cus );
    TotalTime += ( event.time - cus.ArriveTime );
    TempCash = TotalCash;
    TotalCash += cus.Cash;
    t. time = event.time;

    if( cus.Cash > 0 )                                                        /*   有人注资, 检查队列2    */
    {

        count = 1;
        while( TotalCash > TempCash && count <= q[1].length )
        {

            GetHead( q[1], e );
            if( TotalCash + e.Cash >= 0 )
            {

                TotalCash += e.Cash;
                srand( time( 0 ) );
                e. ServiceTime = 3 + rand()%7;
                t. time += e. ServiceTime;
                t. type =3;
                OrderInsert( eventlist, t );

            }
            else
            {

                DeQueue( q[1], e );
                EnQueue( q[1], e );

            }
            count++;

       }
    }

    if( q[0].length != 0 )
    {

        GetHead( q[0], next_customer );
        if( TotalCash + next_customer.Cash >= 0 )
        {

            t. time += next_customer.ServiceTime;
            t. type = 2;
            OrderInsert( eventlist,  t );

        }
        else
        {

            DeQueue( q[0], next_customer );
            EnQueue( q[1], next_customer );

        }

    }



}






void    CustomerDepature_Q2()                                                          /*     从队列2中删除   */
{

    Customer  cus;

    DeQueue( q[1],  cus );
    TotalTime += ( event.time - cus.ArriveTime );
    TotalCash += cus.Cash;

}
















int    main()
{

    OpenForDay();
    while( eventlist.head->next != NULL )
    {

        DelFirst( eventlist,  event );
        switch ( event.type )
        {

            case 1 :    CustomerArrived();

            case 2 :    CustomerDepature_Q1();

            case 3 :    CustomerDepature_Q2();

            default :    exit( -1 );

        }

   }
   printf( "Average Time: %f ", ( float ) TotalTime/CustomerNum );
   system( "pause" );
   return  0;

}

        


        

回复列表 (共3个回复)

沙发

#if !defined( NULL ) 
#define NULL ( 0 ) 
#endif
这三行,请恕我见识比较少,没见过第一行的方式。我只会#ifndef这种方式来表达预处理中的“如果未定义”这样的意思

板凳

调试到
void    OrderInsert( LinkList  &L, Event  e )                                /*    根据事件发生的时间把事件插入事件表   */
{

    ListPtr  p,q,pre;
    p = ( ListPtr )malloc( sizeof( LNode ) );
    if( !p )    exit( -1 );
    p->data = e;
    q = L.head->next;
    while( q != NULL &&  q->data. time < p->data .time )
    {

        pre = q;
        q = q->next;

    }
    p->next = q;
    pre->next = p;


}
函数时溢出,原因是你的pre缺少一个默认值,当链表为空的时候pre缺少初值,默认是一个野指针,操作野指针引发错我


另一个显而易见的问题是你居然把结构体类型当成普通变量来直接赋值……结构体赋值的办法有两种,一个是逐成员赋值,另一种是两个相同类型的结构体变量之间memcpy

3 楼

纵览你的程序,没有输出也没有输入,功能架子起来了,但是人机界面啥也没有,呵呵。花点时间写个菜单出来吧lz

我来回复

您尚未登录,请登录后再回复。点此登录或注册