寻找兄弟数字

来源:互联网 发布:java webservice 例子 编辑:程序博客网 时间:2024/06/10 03:59
#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;//生成Keystring 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();}



0 0
原创粉丝点击