结构推理 编写一个简单的事件处理表。用户可以输入和保存一系列事件;当一个事件处理完毕后,它就会从事件处理表中删除;还可以查询事件处理表中剩余的事件。
   算法的主要思路是:被处理事件的数目限定在100以内,并用宏MAXSIZE来表示。函数enter()用来输入事件,调用函数Add_Queue()将事件字符串指针保存到事件队列中;函数review()用来显示还没有处理的事件;函数delete()将处理完毕的事件从事件队列中删除,并释放事件内容的存储空间,其中删除事件调用函数Del Queue()完成。下面只介绍了循环队列实现的算法,还可以采用链队列实现。这种方法作为实验内容,请读者自己设计。将循环队列的基本操作写在头文件“seqqueue.h”中。具体算法实现如下。
【正确答案】#define datatype  char*
   #define MAXSIZE 100    /*队列的最大容量*/
   typedef  struct
   {datatype data[MAXSIZE];  /*队列的存储空间*/
       int rear,front;    /*队头队尾指针*/
   }SEQQUEUE;
   #include"stdlib.h"
   #include"stdio.h"
   #include"string.h"
   #include"ctype.h"
   #include"seqqueue.h"
   SEQQUEUE*q;
   void enter()
   {  char s[64],*p;
       int len;
       while(1)
       {printf("enter event/%d:",q->rear+1);
           gets(s);
           len=strlen(s);
           if(len==0)break;    /*没有事件*/
           p=malloc(len+1);
           if(!p)
           {printf("memory not available.\n");
               return;}
           strcpy(p,s);
           Add_Queue(q,p);
           if((q->rear+1)  /%MAXSIZE==q->front)
           break;
       }
   }
   void review()
   {  int i=0,pos=q->front;
       while(i!=QueueLength(q))
       {pos=(pos+1)/%MAXSIZE;
           printf("/%d./%s\n",i+1,q->data[pos]);
           i++;
       }
   }
   void delete()
   {  char*p;
       p=Del_Queue(q);
       if(p)
       {printf("/%s\n",p);
           free(p);}
   }
   metin()
   {  char ch;
       Init_Queue(q);    /*创建一个窄队列*/
       do
       {printf("  1--Enter,2--List,3--Remove,4--quit:  ");
           oh=getchar();
           getchar();
           switch(ch)
           {case  '1':enter();break;
               case  '2':revieW()jbreak;
               case  '3':deiete();break;
           }
       }while(ch!='4');
   }
   程序运行实例如下:
   1--Enter,2--List,3--Remove,4--Quit:1
   enter event 1:Harry have a math at;8:00.
   enter event 2:Harry will learn dancing at 1:00 pm.
   enter event 3:Marry will watch TV at 6:30 pm.
   enter event 4:<cr>
   1--Enter,2--List,3--Remove,4--Quit:2
   1.Harry have a math at8:00.
   2.Harry will learn dancing at 1:00 pm.
   3.Harry will watch TV at 6:30 pm.
   1--Enter,2--List,3--Remove,4--Quit:3
   Narry have a math at 8:00.
   1--Enter,2--List,3--Remove,4-Quit:2
   1.Harry will learn dancing at 1:00 pm.
   2.Narry will watch TV at 6:30 pm.
   1--Enter,2--List,3--Remove,4-Quit:4
【答案解析】