非标答,只通过一个测试用例,之后再改进
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <vector>
#include <list>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
typedef basic_string<char>::size_type S_T;
int iMyListCount = 0;
struct MyList
{
int index;
string strDominName;
std::stack<string> ListSonDomin;
};
std::list<MyList> m_list;
int add_SonDomin(MyList &myList);
bool MySort(const MyList &list1, const MyList &list2); //排序方法
int add_host_name(const char* host_name);
int get_host_name(int serial_number, int host_name_max_length, char* host_name);
void clear(void);
bool MySort(const MyList &list1, const MyList &list2) //排序方法
{
std::stack<string> tempList1;
std::stack<string> tempList2;
const char * chList1;
const char * chList2;
tempList1 = list1.ListSonDomin;
tempList2 = list2.ListSonDomin;
while((!tempList1.empty()) && (!tempList2.empty()))
{
chList1 = tempList1.top().c_str();
chList2 = tempList2.top().c_str();
while((*chList1 == *chList2)&&(*chList1!='\0'&& *chList2!='\0'))
{
chList1++;
chList2++;
}
if((*chList1) < (*chList2))
{
return true; //tempList1 排在前
}
if((*chList1) > (*chList2))
{
return false; //tempList2 排在后
}
tempList1.pop();
tempList2.pop();
}
if(tempList1.empty())
{
return true; //tempList1排在前
}
if(tempList2.empty())
{
return false;
}
}
int add_SonDomin(MyList &myList)
{
S_T ulStartIndex = 0;
S_T ulFindIndex = 0;
S_T len = 0;
bool bGo = true;
do
{
ulFindIndex = myList.strDominName.find('.',ulStartIndex);
if(ulFindIndex == string::npos)
{
bGo = false;
}
len = ulFindIndex - ulStartIndex;
if(len < 0)
{
cout<<"ulFindIndex is error, please check out\n"<<endl;
return -1;
}
myList.ListSonDomin.push(myList.strDominName.substr(ulStartIndex,len));
ulStartIndex = ulFindIndex+1;
} while (bGo);
return 0;
}
/*****************************************************************************
Description : 添加主机名
Input Param : host_name 主机名字符串,非空串
Output Param : 无
Return Value : 成功返回0,失败返回-1
*****************************************************************************/
int add_host_name(const char* host_name)
{
/* 在这里实现功能 */
MyList tempList;
tempList.index = iMyListCount;
tempList.strDominName = host_name;
add_SonDomin(tempList);
m_list.push_back(tempList);
//cout<<tempList.strDominName.c_str()<<endl;
//tempList.strDominName.fin
//string::npos;
return 0;
}
/*****************************************************************************
Description : 获取主机名
Input Param : serial_number 排序后的序列号,从1开始
host_name_max_length host_name的最大长度,包括'\0'
Output Param : host_name 主机名字符串,必须包括’\0’,内存由调用者分配和释放
Return Value : 成功返回0,失败返回-1(如:serial_number超出范围、最大长度不够)
*****************************************************************************/
int get_host_name(int serial_number, int host_name_max_length, char* host_name)
{
/* 在这里实现功能 */
m_list.sort(MySort);
if(m_list.size() < serial_number)
{
cout<<"Input serial_number is wrong"<<endl;
return -1;
}
list<MyList>::iterator my_iter = m_list.begin();
list<MyList>::iterator iter_end = m_list.end();
int i = 0;
while(i<(serial_number-1))
{
my_iter++;
i++;
}
char * temp_host_name = (char *)malloc(host_name_max_length);
if(NULL == temp_host_name)
{
cout<<"malloc temp_host_name failed\n"<<endl;
return -1;
}
//strcpy(temp_host_name,(*my_iter).strDominName.c_str());
memcpy(host_name,(const char *)(*my_iter).strDominName.c_str(),host_name_max_length);
//*(temp_host_name + host_name_max_length) = '\0';
//strcpy(host_name,temp_host_name);
//host_name = temp_host_name;
return 0;
}
/*****************************************************************************
Description : 清空所有主机名
Input Param : 无
Output Param : 无
Return Value : 无
*****************************************************************************/
void clear(void)
{
m_list.clear();
/* 在这里实现功能 */
}
//主函数是个测试用例
int main()
{
if(0 == add_host_name("mail.huawei.com"))
{
cout<<"add mail.huawei.com successfully"<<endl;
}
if(0 == add_host_name("huawei.com"))
{
cout<<"add huawei.com successfully"<<endl;
}
if(0 == add_host_name("teltalk.org"))
{
cout<<"add teltalk.org successfully"<<endl;
}
if(0 == add_host_name("google.com.hk"))
{
cout<<"add google.com.hk successfully"<<endl;
}
if(0 == add_host_name("imail.huawei.com"))
{
cout<<"add imail.huawei.com successfully"<<endl;
}
char out_str[20];
if(0 == get_host_name(4, sizeof(out_str), out_str))
{
cout<<"get successfully\n"<<out_str<<"\n"<<endl;
}
char a;
scanf(&a);
return 1;
}
写的比较着急,注释比较少,以后再补吧。