题目
代码
/*---------------------------------------
* 日期:2015-07-05
* 作者:SJF0115
* 题目:对象管理器
* 来源:华为机试练习题
-----------------------------------------*/
#include <iostream>
#include "ObjMgt.h"
#include <vector>
using namespace std;
struct Object{
unsigned int key1;
unsigned int key2;
unsigned int key3;
Object(unsigned int a,unsigned int b,unsigned int c){
key1 = a;
key2 = b;
key3 = c;
}
Object(){}
};
vector<Object> container;
/*------------------------------------------------------------------------------
功能:添加单个对象
输入:
key1 外部关键字 KEY1
key2 外部关键字KEY2
key3 外部关键字KEY3
输出:无
返回:
-1 :失败(对象已经存在或者其他异常)
0 :成功
-------------------------------------------------------------------------------*/
int AddObject (unsigned int key1, unsigned int key2, unsigned int key3){
int isExits = IsObjectExist(key1,key2,key3);
// 对象已经存在
if(isExits){
return -1;
}//if
Object o(key1,key2,key3);
container.push_back(o);
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){
Object o;
vector<Object>::iterator ite = container.begin();
while(ite != container.end()){
o = *ite;
if((o.key1 == key1 || key1 == 0xFFFFFFFF) && (o.key2 == key2 || key2 == 0xFFFFFFFF) && (o.key3 == key3 || key3 == 0xFFFFFFFF)){
ite = container.erase(ite);
}//if
else{
++ite;
}//else
}//while
return ;
}
/*-----------------------------------------------------------------------------------
功能:查询单个对象是否存在
输入:
key1 外部关键字 KEY1
key2 外部关键字 KEY2
key3 外部关键字 KEY3
输出:无
返回:
0:不存在
1:存在
-----------------------------------------------------------------------------------*/
int IsObjectExist (unsigned int key1, unsigned int key2, unsigned int key3){
int size = container.size();
Object o;
for(int i = 0;i < size;++i){
o = container[i];
if(o.key1 == key1 && o.key2 == key2 && o.key3 == key3){
return 1;
}//if
}//for
return 0;
}
/*----------------------------------------------------------------------------------
Description 清空全部对象
Prototype void Clear();
Input Param 无
Output Param 无
Return Value 无
-----------------------------------------------------------------------------------*/
void Clear(void){
container.clear();
return;
}