我不是高手,自己试着来做了一下,还大家多多指教。
其中有一部分是测试用例。时间我直接用数字表示( 8:30 == 830 )
-------------------头文件 newspaper.h
#ifndef NEWS_PAPER_H_
#define NEWS_PAPER_H_
enum { entertament, political, economic, science };//将类别作为枚举类型(娱乐,政治,经济,科学)
typedef struct EVENT
{
unsigned int eventID; //事件ID
unsigned int eventTime;//事件发生的时间(我在这里用简单的数字来表示,如8:30就是830)
unsigned char eventClass;//事件的类别
}EVENT;
typedef struct NEWSPAPERS
{
unsigned int newsPaperID;//报纸的ID
EVENT *eventHead;//所包含的事件
struct NEWSPAPERS *nextNewsPaper;
}NEWSPAPERS;
void Init();
void Clear();
unsigned int QuaryEvent( unsigned char eventClass, unsigned int startTime,
unsigned int getMaxEventNum, unsigned int *peventID );
#endif
----------------------------------------------------------------------------------------
---------------------------------主文件newspaper.c
#include <stdio.h>
#include "newspaper.h"
unsigned int outPutEventID[100] = { 0 };//存储满足条件事件ID的数组
//以下这部分为建立一个测试用的链表,以便后面的测试用例来测试
NEWSPAPERS *phead = NULL; //链表的头指针
//链表中的四个节点
NEWSPAPERS news1;
NEWSPAPERS news2;
NEWSPAPERS news3;
NEWSPAPERS news4;
//每个节点上对应的事件数组
EVENT eventArrFir[20] = { 0 };
EVENT eventArrSec[20] = { 0 };
EVENT eventArrThi[20] = { 0 };
EVENT eventArrFou[20] = { 0 };
//初始化链表
void Init()
{
eventArrFir[0].eventID = 0;
eventArrFir[0].eventClass = political;
eventArrFir[0].eventTime = 830;
eventArrFir[1].eventID = 1;
eventArrFir[1].eventClass = entertament;
eventArrFir[1].eventTime = 940;
eventArrSec[0].eventID = 3;
eventArrSec[0].eventClass = entertament;
eventArrSec[0].eventTime = 820;
eventArrSec[1].eventID = 4;
eventArrSec[1].eventClass = economic;
eventArrSec[1].eventTime = 1000;
eventArrThi[0].eventID = 5;
eventArrThi[0].eventClass = entertament;
eventArrThi[0].eventTime = 820;
eventArrThi[1].eventID = 6;
eventArrThi[1].eventClass = political;
eventArrThi[1].eventTime = 900;
eventArrFou[0].eventID = 9;
eventArrFou[0].eventClass = science;
eventArrFou[0].eventTime = 820;
eventArrFou[1].eventID = 10;
eventArrFou[1].eventClass = political;
eventArrFou[1].eventTime = 900;
news1.newsPaperID = 0;
news1.eventHead = eventArrFir;
news1.nextNewsPaper = &news2;
news2.newsPaperID = 1;
news2.eventHead = eventArrSec;
news2.nextNewsPaper = &news3;
news3.newsPaperID = 2;
news3.eventHead = eventArrThi;
news3.nextNewsPaper = &news4;
news4.newsPaperID = 1;
news4.eventHead = eventArrFou;
news4.nextNewsPaper = NULL;
phead = &news1;
}
//Clear 清除上一次满足条件的事件ID
void Clear()
{
int i = 0;
for ( i = 0; i < 100; i++ )
{
outPutEventID[i] = 0;
}
}
//所要求的函数
unsigned int QuaryEvent( unsigned char eventClass, unsigned int startTime,
unsigned int getMaxEventNum, unsigned int *peventID )
{
int i = 0;
unsigned int tmpEventClass = 0;//链表中事件的类别
unsigned int tmpStarTime = 0;//链表中的时间
unsigned int count = 0;
NEWSPAPERS *pnewspaper = phead;
while ( pnewspaper != NULL )
{
for ( i = 0; i < 2; i++ )
{
tmpEventClass = pnewspaper->eventHead[i].eventClass;
tmpStarTime = pnewspaper->eventHead[i].eventTime;
if ( ( tmpEventClass == eventClass ) && ( tmpStarTime >= startTime ) && ( count < getMaxEventNum ) )
{
peventID[count++] = pnewspaper->eventHead[i].eventID;
if ( count == getMaxEventNum )
{
phead = pnewspaper->nextNewsPaper;
return count;
}
}
}
pnewspaper = pnewspaper->nextNewsPaper;
}
return count;
}
----------------------------------------------------------------
--------------------------测试文件testnewspaper.c
#include <stdio.h>
#include <assert.h>
#include "newspaper.h"
extern NEWSPAPERS news1;
extern NEWSPAPERS news2;
extern NEWSPAPERS news3;
extern NEWSPAPERS news4;
extern EVENT eventArrFir[20];
extern EVENT eventArrSec[20];
extern EVENT eventArrThi[20];
extern EVENT eventArrFou[20];
extern unsigned int outPutEventID[100];
extern NEWSPAPERS *phead;
Test_InitEvent()//测试初始化函数
{
Init();
assert( eventArrFou[1].eventID == 10 );
assert( news4.eventHead->eventID == 9 );
assert( news1.nextNewsPaper->nextNewsPaper->newsPaperID == 2 );
assert( news1.nextNewsPaper->nextNewsPaper->eventHead[1].eventClass == political );
}
void Test_QuaryEvent()
{
unsigned int num = 0;
Init();
num = QuaryEvent( entertament, 800, 2, outPutEventID );//按要求得到返回值
assert( num == 2 );
assert( outPutEventID[0] == 1 );
assert( outPutEventID[1] == 3 );
assert( outPutEventID[2] == 0 );//由于存储了满足条件的2个ID,所以第3个以后均为0;
Clear();//清空前面存储的ID
num = QuaryEvent( entertament, 800, 2, outPutEventID );
assert( num == 1 );
assert( outPutEventID[0] == 5 );
assert( outPutEventID[1] == 0 );//由于存储了满足条件的2个ID,所以第2个以后均为0;
assert( outPutEventID[2] == 0 );
}
void Test_QuaryEvent1()
{
unsigned int num = 0;
Clear();
Init();
num = QuaryEvent( political, 830, 2, outPutEventID );
assert( num == 2 );
assert( outPutEventID[0] == 0 );
assert( outPutEventID[1] == 6 );
assert( outPutEventID[2] == 0 );
Clear();
num = QuaryEvent( political, 830, 2, outPutEventID );
assert( num == 1 );
assert( outPutEventID[0] == 10 );
assert( outPutEventID[1] == 0 );
assert( outPutEventID[2] == 0 );
}
int main()
{
Test_InitEvent();
Test_QuaryEvent();
Test_QuaryEvent1();
return 0;
}
--------------------------------------------------------------------------------