// serch.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "goqomessage.h"
#define MAX_CS_NUM 50 //CS的数量
#define MAX_CLIENT_NUM 5000 //NS的数量
int HalfSearch(CLIENTNODE s[],int a,int b,DWORD key)
/* s为有序数列,a、b分别为查找区间的起点和终点,key为查找关键字 */
{
int mid;
if(a==b)
{
if(key==s[a].id)
return (a);
else
return (-1);
}
else
{
mid=(a+b)/2;
if(key<s[mid].id)
{
return(HalfSearch(s,a,mid,key));
}
if(key>s[mid].id)
{
return(HalfSearch(s,mid+1,b,key));
}
if(key==s[mid].id)
{
return (mid);
}
}
}
int SearchClient(CLIENTLIST AllCliet,CLIENTNODE Client)
{
int iResult=-1;
int a=0;
int b=AllCliet.iClientNum;
iResult = HalfSearch(AllCliet.ClientList,a,b,Client.id);
return iResult;
}
bool AddClient(CLIENTLIST &AllCliet,CLIENTNODE Client)
{
//如果满,则退出,出错
if (AllCliet.iClientNum > MAX_CLIENT_NUM)
{
return false;
}
//刚开始时
if (AllCliet.iClientNum == 0)
{
AllCliet.ClientList[0]=Client;
++AllCliet.iClientNum;
}
return true;
}
bool DeleteClient(CLIENTLIST &AllCliet,CLIENTNODE Client)
{
int pos;
pos = SearchClient(AllCliet,Client);
if (pos < 0)
{
return true;
}
for (int i=pos; i<AllCliet.iClientNum;++i)
{
AllCliet.ClientList[i]=AllCliet.ClientList[i+1];
}
--AllCliet.iClientNum;
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
CLIENTLIST AllClient;
CLIENTNODE Client;
Client.id=125;
AllClient.ClientList[0].id = 123;
AllClient.ClientList[1].id = 124;
AllClient.ClientList[2].id = 125;
AllClient.ClientList[3].id = 126;
AllClient.ClientList[4].id = 127;
AllClient.iClientNum=5;
printf("%d\n",AllClient.ClientList[2].id);
int i;
i=SearchClient(AllClient,Client);
printf("%d\n",i);
Sleep(10000);
return 0;
}