|
实现对象管理器功能,其中管理的对象有3个外部关键字,要求实现:
增加对象;
删除对象;
判断对象是否存在;
说明: 对象的三个外部关键字分别以KEY1,KEY2,KEY3表示。
#include "ObjMgt.h"
#include <vector>
using namespace std;
typedef struct{
unsigned int key1;
unsigned int key2;
unsigned int key3;
}KEY;
vector <KEY> allKey;
/*************************************************************************
功能:增加单个对象
输入:
key1 外部关键字 KEY1
key2 外部关键字KEY2
key3 外部关键字KEY3
输出:无
返回:
-1 :失败(对象已经存在或者其它异常)
0 :成功
***************************************************************************/
int AddObject (unsigned int key1, unsigned int key2, unsigned int key3)
{
/*请实现*/
if(allKey.size() > 10000)
return -1;
if(key1 > 65535 || key2 > 65535 || key3 > 65535)
return -1;
int i,j = allKey.size();
for(i = 0; i < j;i++)
if(allKey.key1 == key1 && allKey.key2 == key2 && allKey.key3 == key3)
return -1;
KEY k = {key1,key2,key3};
allKey.push_back(k);
return 0;
}
/********************************************************************************
功能:删除一个或多个对象
输入:
key1 外部关键字 KEY1
key2 外部关键字 KEY2
key3 外部关键字 KEY3
输出:无
返回:无
说明:用例保证参数取值为合法值和通配符0xFFFFFFFF, 通配符表示0~65535范围内的任意值;
举例:key1=1,key2=2,key3= 0xFFFFFFFF,表示删除key1=1,key2=2的所有对象;
key1,key2,key3取值全为0xFFFFFFFF时,表示删除所有对象。
*********************************************************************************/
void DeleteObject (unsigned int key1, unsigned int key2, unsigned int key3)
{
/*请实现*/
bool allKey1,allKey2,allKey3;
if(key1 == 0xffffffff)
allKey1 = true;
else
allKey1 = false;
if(key2 == 0xffffffff)
allKey2 = true;
else
allKey2 = false;
if(key3 == 0xffffffff)
allKey3 = true;
else
allKey3 = false;
int i,j = allKey.size();
vector<KEY>::iterator it ;
it = allKey.begin();
while (it != allKey.end())
{
if(allKey1 || it->key1 == key1)
if(allKey2 || it->key2 == key2)
if(allKey3 || it->key3 == key3){
it = allKey.erase(it);
continue;
}
it++;
}
return ;
}
/********************************************************************************
功能:查询单个对象是否存在
输入:
key1 外部关键字 KEY1
key2 外部关键字 KEY2
key3 外部关键字 KEY3
输出:无
返回:
0:不存在
1:存在
**********************************************************************************/
int IsObjectExist (unsigned int key1, unsigned int key2, unsigned int key3)
{
/*请实现*/
if(key1 > 65535 || key2 > 65535 || key3 > 65535)
return 0;
int i,j = allKey.size();
for(i = 0; i < j;i++)
if(allKey.key1 == key1 && allKey.key2 == key2 && allKey.key3 == key3)
return 1;
return 0;
}
/******************************************************************************************************
Description 清空所有对象
Prototype void Clear();
Input Param 无
Output Param 无
Return Value 无
********************************************************************************************************/
void Clear(void)
{
/*在这里实现功能*/
allKey.clear();
return;
} |
|
|