对象管理器

<pre name="code" class="cpp"><pre name="code" class="cpp">//ObjMgt.h
 
#ifndef __OBJMGT_H__
#define __OBJMGT_H__


int  AddObject (unsigned int key1, unsigned int key2, unsigned int key3);
void  DeleteObject (unsigned int key1, unsigned int key2, unsigned int key3);
int  IsObjectExist (unsigned int key1, unsigned int key2, unsigned int key3);
void Clear(void);
#endif


 
 
 
//ObjMgt.cpp
#include "ObjMgt.h"
#include <vector>
#include <iostream>
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[i].key1 == key1 && allKey[i].key2 == key2 && allKey[i].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 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[i].key1 == key1 && allKey[i].key2 == key2 && allKey[i].key3 == key3)
      return 1;
  return 0;
}

/******************************************************************************************************
Description	 清空所有对象
Prototype	   void Clear();
Input Param	 无
Output Param	无
Return Value	无

********************************************************************************************************/
void Clear(void)
{
  /*在这里实现功能*/
  allKey.clear();
  return;
}


int main()
{
	cout<<AddObject(1, 2, 3)<<endl;    
	cout<<AddObject(1, 2, 4)<<endl;    
	cout<<AddObject(1, 5, 1)<<endl;    
	cout<<AddObject(1, 5, 3)<<endl;    
	cout<<AddObject(2, 3, 4)<<endl;    
	cout<<AddObject(2, 3, 5)<<endl;    
	cout<<AddObject(2, 4, 5)<<endl;    
	cout<<AddObject(2, 3, 6)<<endl;    
	cout<<AddObject(2, 3, 6)<<endl;
	int result =IsObjectExist(1, 5, 3);
	cout<<result<<endl;
	return 0;
}
相关文章
相关标签/搜索