CS106B-07课上(按照课上来的)

版本一大概是按照课上来的,版本二是添加了一个处理同时出现多个最大的情况

版本一:

#include   <iostream> 
#include   <fstream> 
#include   <string>
#include   <map>
#include   <set>
using  namespace std; 

/*SwapChar and newSequenc are used to get a new string whose chars are put in alphabetical order.
使字符串中的字符按照字母表顺序排列= v=
*/
void SwapChar(char &first, char &second){
	char temp = second;
	second = first;
	first = temp;
}

string newSequence(string word){

	for(int i = 0; i < word.size(); i++){
		for(int j = i + 1 ; j< word.size(); j++ )
			if(word[j] < word[i]) 	{
				SwapChar(word[j], word[i] );	
			}
	}
	return word;
}

//打印分别有什么单词
void theMaxWord(set<string> s){
	set<string> :: iterator it;
	cout<<"他们分别是:" ;
	for(it = s.begin(); it != s.end(); ++it){
		cout << *it  << endl;
	}
}


void readFile(ifstream &in, map<string, set<string> > &m){
	while(true){
		string word;
		in >> word;
		if(in.fail())break;

		string newWord = newSequence(word);
		m[newWord].insert(word);//这就相当于在map里面增加新的pair了
		if(in.eof()) break;
	}

	int maxSize = 0;
	string maxKey;
	int theNextMax;
	string theNextMxString;

	cout  << "一共有" << m.size()  << "组单词" << endl;
	map<string, set<string> > :: iterator itr;
	for(itr = m.begin(); itr != m.end(); ++itr){
		string key = itr -> first;
		cout << "有" << key <<  "字母的单词一共" << m[key].size() << "个"<< endl;
		if(m[key].size() > maxSize){
			maxSize = m[key].size();
			maxKey = key;
		}
		if(m[key].size() == maxSize){
			theNextMax = m[key].size();
			theNextMxString = key;
		}		
	}

	cout << "出现最多的字母顺序为" << maxKey << endl;
	theMaxWord(m[maxKey]);
	cout << "字母长度为" << maxKey.size()<< endl;

	if(theNextMxString != ""){
		cout << "出现最多的字母顺序同时还有" << theNextMxString << endl;
		theMaxWord(m[theNextMxString]);
		cout << "字母长度为" << theNextMxString.size()<< endl;
	}

}

int main(){
	//int is the length of the word
	map<string, set<string> > m;

	ifstream in;
	string txtName;
	cin >> txtName;
	in.open(txtName);

	readFile(in, m);

	return 0;
}



版本二:

#include   <iostream> 
#include   <fstream> 
#include   <string>
#include   <map>
#include   <set>
using  namespace std; 

/*SwapChar and newSequenc are used to get a new string whose chars are put in alphabetical order.
使字符串中的字符按照字母表顺序排列= v=
*/
void SwapChar(char &first, char &second){
	char temp = second;
	second = first;
	first = temp;
}

string newSequence(string word){

	for(int i = 0; i < word.size(); i++){
		for(int j = i + 1 ; j< word.size(); j++ )
			if(word[j] < word[i]) 	{
				SwapChar(word[j], word[i] );	
			}
	}
	return word;
}

bool findtheNext(int a, int b){
	bool findnext;

	if(a <= b){
		findnext = true;
	}else{
		findnext = false;
	}

	return findnext;
}

//打印分别有什么单词
void theMaxWord(set<string> s){
	set<string> :: iterator it;
	cout<<"他们分别是:" ;
	for(it = s.begin(); it != s.end(); ++it){
		cout << *it  <<" ";
	}
}

//theNextInt和theNextWord是用来寻找下一个最大值的
int theNextInt(map<string, set<string> > m, int maxSize, string maxKey){	
	m.erase(maxKey);

	int maxS = 0;
	string maxK;

	map<string, set<string> > :: iterator itr;
	for(itr = m.begin(); itr != m.end(); ++itr){
		string key = itr -> first;		
		if(m[key].size() > maxS){
			maxS = m[key].size();
			maxK = key;
		}
	}

	return maxS;
}

string theNextWord(map<string, set<string> > m, int maxSize, string maxKey){	
	m.erase(maxKey);

	int maxS = 0;
	string maxK = maxKey;

	map<string, set<string> > :: iterator itr;
	for(itr = m.begin(); itr != m.end(); ++itr){
		string key = itr -> first;		
		if(m[key].size() > maxS){
			maxS = m[key].size();
			maxK = key;
		}
	}

	return maxK;
}

//删除副本map中的已打印的最大值
void eraseTheMax(map<string, set<string> > &m, string maxKey){
	m.erase(maxKey);
}

void readFile(ifstream &in, map<string, set<string> > &m){
	while(true){
		string word;
		in >> word;
		if(in.fail())break;

		string newWord = newSequence(word);
		m[newWord].insert(word);//这就相当于在map里面增加新的pair了
		if(in.eof()) break;
	}

	int maxSize = 0;
	string maxKey;

	cout  << "一共有" << m.size()  << "组单词" << endl;
	map<string, set<string> > :: iterator itr;
	for(itr = m.begin(); itr != m.end(); ++itr){
		string key = itr -> first;
		cout << "有" << key <<  "字母的单词一共" << m[key].size() << "个"<< endl;
		if(m[key].size() > maxSize){
			maxSize = m[key].size();
			maxKey = key;
		}
	}

	cout << "出现最多的字母顺序为" << maxKey << endl;
	theMaxWord(m[maxKey]);
	cout << "字母长度为" << maxKey.size()<< endl;

	map<string, set<string> > copyM = m;
	eraseTheMax(copyM, maxKey);
	int Time = 1;
	
	while(findtheNext(maxSize, theNextInt(copyM, maxSize, maxKey))){
		string theNext = theNextWord(copyM, maxSize, maxKey);
		int theNextNum = theNextInt(copyM, maxSize, maxKey);


		cout << "出现最多的字母顺序同时还有" << theNext << endl;
		theMaxWord(copyM[theNext]);
		cout << "字母长度为" << theNext.size()<< endl;	
		eraseTheMax(copyM, theNext);

		Time++;
	}

	cout << "一共有" << Time << "个最大值" << endl;

}

int main(){
	//int is the length of the word
	map<string, set<string> > m;

	ifstream in;
	string txtName;
	cin >> txtName;
	in.open(txtName);

	readFile(in, m);

	return 0;
}



转载于:https://my.oschina.net/u/1763504/blog/296577

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值