#ifndef _FIND_SIMILAR_WORD_H_
#define _FIND_SIMILAR_WORD_H_
#include <string>
#include <map>
using namespace std;
/*
* 編程實現單鏈表的排序
*/
class Brother{
public :
Brother(char *n) :data(n) {}
Brother* addWord(char *word);
public :
char *data;
Brother *next;
Brother *pre;
};
int compareSeq(char *srcWord, char *destWord){
char *srcChar=srcWord;
char *destChar=destWord;
while(*srcChar!='\0'){
if (*srcChar>*destChar){
return 1;
}
else if(*srcChar<*destChar){
return -1;
}
else{
srcChar++;
destChar++;
}
}
return 0;
}
Brother* Brother::addWord(char *srcWord){
Brother* currentNode=this;
int greaterThan=1;
Brother *newWord=new Brother(srcWord);
newWord->next=NULL;
newWord->pre=NULL;
//不大於後者
while(greaterThan!=-1){
greaterThan=compareSeq(srcWord,currentNode->data);
if(greaterThan==0){
break;
}
if(greaterThan!=-1){
if(currentNode->next!=NULL){
currentNode=currentNode->next;
}
else{
currentNode->next=newWord;
newWord->pre=currentNode;
break;
}
}
}
if(greaterThan==-1){
if(currentNode->pre==NULL){
newWord->next=currentNode;
currentNode->pre=newWord;
return newWord;
}
else {
currentNode->pre->next=newWord;
newWord->pre=currentNode->pre;
newWord->next=currentNode;
currentNode->pre=newWord;
}
}
return NULL;
}
#endif
/******************************************************************************
Copyright (C), 2001-2012, Huawei Tech. Co., Ltd.
******************************************************************************
File Name : FindSimilarWord.cpp
Version :
Author :
Created : 2012/09
Last Modified :
Description :
Function List :
History :
1.Date : 2012/09
Author :
Modification: Created file
******************************************************************************/
#include "FindSimilarWord.h"
typedef map<string,Brother*>::iterator DicPtrItr;
map<string, Brother*> dic;
//生成Key
string generateKey(char srcWord[]){
int length=(int)strlen(srcWord);
char srcWord2[60]={};
char tempChar;
strcpy(srcWord2,srcWord);
for(int i=0;i<length-1;i++){
for(int j=0;j<length-i-1;j++){
if(srcWord2[j]>srcWord2[j+1]){
tempChar=srcWord2[j];
srcWord2[j]=srcWord2[j+1];
srcWord2[j+1]=tempChar;
}
}
}
return string(srcWord2);
}
/******************************************************************************
原 型:int AddOneWord (char* Word);
功 能:在字典中增加一個單詞
輸入參數:
Word 單詞字符串,調用者保證Word指針不爲空,指向的是合法單詞
輸出參數:
無
返回值:
-1 失敗(單詞在字典中已存在等情況)
0 成功
********************************************************************************/
int AddOneWord (char* Word)
{
/* 在這裏實現功能 */
string s=generateKey(Word);
DicPtrItr dicPtrItr=dic.find(s);
Brother *brother,*head;
if(dicPtrItr!=dic.end()){
brother=dicPtrItr->second;
head=brother->addWord(Word);
if (head!=NULL){
dic[s]=head;
}
}
else{
brother=new Brother(Word);
brother->next=NULL;
brother->pre=NULL;
dic[s]=brother;
}
return 0;
}
/******************************************************************************
原 型:int FindSimilarWordNum (char* Word);
功 能:查找指定單詞在字典中的兄弟單詞個數
輸入參數:
Word 指定單詞字符串,調用者保證Word指針不爲空,指向的是合法單詞
輸出參數:
無
返回值:
返回指定單詞在字典中的兄弟單詞總個數。如果不存在兄弟單詞,返回0
*******************************************************************************/
int FindSimilarWordNum (char* Word)
{
/* 在這裏實現功能 */
int i=0;
string s=generateKey(Word);
DicPtrItr dicPtrItr=dic.find(s);
if(dicPtrItr==dic.end()){
return 0;
}
Brother *tempBro=dicPtrItr->second;
while(tempBro!=NULL){
if(string(tempBro->data)!=string(Word)) {
i++;
}
tempBro=tempBro->next;
}
return i;
}
/******************************************************************************
原 型:int FindOneSimilarWord (char* Word, int Seq, char* SimilarWord);
功 能:查找指定單詞的指定序號的兄弟單詞,指定序號指字典中兄弟單詞按字典順序
排序後的序號(從1開始)
輸入參數:
Word 指定單詞字符串,調用者保證Word指針不爲空,指向的是合法單詞
Seq 指定序號(大於等於1)
輸出參數:
SimilarWord 返回兄弟單詞字符串,指針不爲空,指向的內存由調用者預先分配,
佔51個字節長度,輸出的兄弟單詞必須緊跟’\0’結束。如果不存在指定序號的
兄弟單詞,輸出空串。
返回值:
-1 失敗(如果不存在指定序號的兄弟單詞,返回失敗)
0 成功
*******************************************************************************/
int FindOneSimilarWord (char* Word, int Seq, char* SimilarWord)
{
/* 在這裏實現功能 */
if(FindSimilarWordNum(Word)<Seq){
return -1;
}
int i=0;
string s=generateKey(Word);
DicPtrItr dicPtrItr=dic.find(s);
Brother *tempBro=dicPtrItr->second;
while(tempBro!=NULL){
if(string(tempBro->data)!=string(Word)) {
i++;
if(i==Seq){
strcpy(SimilarWord,tempBro->data);
return 0;
}
}
tempBro=tempBro->next;
}
return -1;
}
/******************************************************************************
原 型:void ClearAllWords(void);
功 能:清空字典中所有單詞
輸入參數:
無
輸出參數:
無
返回值:
無
*******************************************************************************/
void ClearWords(Brother *currentNode){
Brother * pr=NULL;
while(currentNode!=NULL){
pr=currentNode->next;
delete currentNode;
currentNode=pr;
}
}
void ClearAllWords(void)
{
/* 在這裏實現功能 */
DicPtrItr dicPtrItr=dic.begin();
Brother *currentNode;
while(dicPtrItr!=dic.end()){
currentNode=dicPtrItr->second;
ClearWords(currentNode);
dicPtrItr++;
}
dic.clear();
}