C++ Primer第五版第十六章编程练习(string类、STL、算法)

//palindrome.cpp
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

bool revs(string &s);

int main()
{
	string temp;
	cout << "Enter a scentence or words (quit to exit) : ";
	getline(cin, temp);
	while(temp != "quit")
	{
		if(revs(temp))
		{
			cout << "Yes, it's palindrome . " << endl;
		}
		else
		{
			cout << "No, it's not a palindrome . " << endl;
		}
		cout << "Enter the new scentence or words (quit to exit) : ";
		getline(cin, temp);
	}
	cout << "done . " << endl;
	return 0;
}

bool revs(string &s)
{
	string temp = s;
	reverse(temp.begin(), temp.end());
	return (temp == s);
}

 

#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>

using namespace std;

bool revs(string &s);

int main()
{
	string temp;
	cout << "Enter a scentence or words(quit to exit) : ";
	getline(cin, temp);
	while(temp != "quit")
	{
		if(revs(temp))
		{
			cout << "Yes, it's a palindrome ." << endl;
		}
		else
		{
			cout << "No, it's not a palindrome ." << endl;
		}
		cout << "Enter new scentence or words(quit to exit) : ";
		getline(cin, temp);
	}
	return 0;
}

bool revs(string &s)
{
	string temp;
	int num = s.size();
	for(int i = 0; i < num; i++)
	{
		if(isalpha(s[i]))
		{
			temp += tolower(s[i]);
		}
	}
	string temp1 = temp;
	reverse(temp1.begin(), temp1.end());
	return (temp1 == temp);
}

//vec_file.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<iterator>
#include<algorithm>
#include<cstdlib>
#include<cctype>

using namespace std;

char toLower(char ch){return tolower(ch);};
string & ToLower(string &s);
void show(string &s);

int main()
{
	ifstream fin;
	fin.open("readme.txt");
	if(!fin.is_open())
	{
		cout << "Sorry, dose not exist the file ! ";
		exit(EXIT_FAILURE);
	}
	
	vector<string> fv;
	string temp;
	fin >> temp;
	while(fin)
	{
		fv.push_back(temp);
		fin >> temp;
	}
	
	for_each(fv.begin(), fv.end(), show);
	cout << endl;
	
	set<string> fs;
	transform(fv.begin(), fv.end(), insert_iterator<set<string> >(fs, fs.begin()), ToLower);
	set<string>::iterator fi;
	map<string, int> fm;
	for(fi = fs.begin(); fi != fs.end(); ++fi)
	{
		fm[*fi] = count(fv.begin(), fv.end(), *fi);
	}
	
	for(fi = fs.begin(); fi != fs.end(); ++fi)
	{
		cout << *fi << " : " << fm[*fi] << endl; 
	}
	
	fin.close();
	cout << "done . " << endl;
	
	return 0;
}

string & ToLower(string &s)
{
	transform(s.begin(), s.end(), s.begin(), toLower);
	return s;
}

void show(string &s)
{
	cout << s << " ";
}

 

//reduce.cpp
#include<iostream>
#include<list>
#include<ctime>
#include<cstdlib>

using namespace std;

const int LIM = 10;
void show(long a[], int n = LIM);
int reduce(long ar[], int n);


int main()
{
	srand(time(0));
	long data[LIM];
	long temp;
	for(int i = 0; i < LIM; i++)
	{
		data[i] = rand() % 5;
	}
	cout << "# Original data #" << endl;
	show(data, LIM);	
	cout << "# After reduce data #" <<endl;
	cout << "The data contains " << reduce(data, LIM) << " different values. " << endl;
	
	cout << "done . " << endl;
	return 0;
}

void show(long a[], int n)
{
	for(int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

int reduce(long a[], int n)
{
	list<long> ll;
	for(int i = 0; i < n; i++)
	{
		ll.push_back(a[i]);
	}
	ll.sort();
	ll.unique();
	list<long>::iterator li;
	for(li = ll.begin(); li != ll.end(); ++li)
	{
		cout << *li << " ";
	}
	cout << endl;
	return ll.size();
}

 

//tep_reduce.cpp
#include<iostream>
#include<ctime>
#include<string>
#include<list>
#include<iterator>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int LIM = 10;
const int SLIM = 5;

template<class T>
void show(T a[], int n);

template<class T>
int reduce(T a[], int n);

int main()
{
	srand(time(0));
	long ldata[LIM];
	string sdata[5] = {"C++", "c++", "c++", "C++", "g++"};
	for(int i = 0; i < LIM; i++)
	{
		ldata[i] = rand() % 5;
	}
	cout << "# Original long data #" << endl;
	show(ldata, LIM);
	show(sdata, SLIM);
	cout << "# After reduce long date #" << endl;
	cout << "long data contains " << reduce(ldata, LIM) << " different values " << endl;
	cout << "string data contains "<< reduce(sdata, SLIM) << " different strings " << endl;
	
	cout << "done . " << endl;
	return 0;
}

template<class T>
void show(T a[], int n)
{
	for(int i = 0; i < n; ++i)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}


template<class T>
int reduce(T a[], int n)
{
	list<T> lt;
	for(int i = 0; i < n; ++i)
	{
		lt.push_back(a[i]);
	}
	lt.sort();
	lt.unique();
	typename list<T>::iterator li;
	for(li = lt.begin(); li != lt.end(); ++li)
	{
		cout << *li << " ";
	}
	cout << endl;
	return lt.size();
}

 

//lotto.cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
#include<cstdlib>

using namespace std;

void show(int n){ cout << n << " ";};
vector<int> lotto(int max, int n);

int main()
{
	srand(time(0));
	vector<int> winner;
	winner = lotto(51, 6);
	for_each(winner.begin(), winner.end(), show);
	cout << endl;
	cout << "done . " << endl;
	return 0;
}

vector<int> lotto(int max, int n)
{
	vector<int> temp;
	vector<int> result;
	for(int i = 0; i < max; ++i)
	{
		temp.push_back(i + 1);
	}
	for(int i = 0; i < n; ++i)
	{
		random_shuffle(temp.begin(), temp.end());
		result.push_back(*temp.begin());
	}
	sort(result.begin(), result.end());
	return result;
}

 

//party.cpp
#include<iostream>
#include<set>
#include<string>
#include<algorithm>

using namespace std;

void show(string name){cout << name <<endl;};

int main()
{
	set<string> Mat;
	set<string> Pat;
	set<string> Party;
	string temp;
	cout << "Enter the name in Mat's name list(quit to exit) " << endl;
	getline(cin, temp);
	while(temp != "quit")
	{
		Mat.insert(temp);
		cout << "Enter the new name(quit to exit) " << endl;
		getline(cin, temp);
	}
	cout << "# Mat info #" << endl;
	for_each(Mat.begin(), Mat.end(), show);
	
	cout << "Enter the name in Pat's name list(quit to exit) " << endl;
	getline(cin, temp);
	while(temp != "quit")
	{
		Pat.insert(temp);
		cout << "Enter the new name(quit to exit) " << endl;
		getline(cin, temp);
	}
	cout << "# Pat info #" << endl;
	for_each(Pat.begin(), Pat.end(), show);
	
	cout << "# Party info #" << endl;
	Party.insert(Mat.begin(), Mat.end());
	Party.insert(Pat.begin(), Pat.end());
	for_each(Party.begin(), Party.end(), show);
	
	cout << "done . " << endl;
	return 0;
}

practice makes perfect!

猜你喜欢

转载自blog.csdn.net/qq_37172182/article/details/85409821