寻找兄弟数字

#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();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值