C++primer第六章《学习》

6.7 前面已经实现的统计元音的程序存在一个问题:不能统计大写的元音字母。编写程序统计大写小写的元音字母,也就是说,你的程序计算出来的aCnt,既包括’a’也包括’A’出现的次数,其他四个元音也一样。

int main()
{
	ifstream infile;
	infile.open("test.txt");
	char ch;
	int aCnt(0);
	while (!infile.eof()){
		infile >> ch;
		switch (ch){
		case 'a':
			++aCnt;
			break;
		case 'e':
			++aCnt;
			break;
		case 'i':
			++aCnt;
			break;
		case 'o':
			++aCnt;
			break;
		case 'u':
			++aCnt;
			break;
		case 'A':
			++aCnt;
			break;
		case 'E':
			++aCnt;
			break;
		case 'I':
			++aCnt;
			break;
		case 'O':
			++aCnt;
			break;
		case 'U':
			++aCnt;
			break;
		}
	}
	cout << "元音字母数目为:";
	cout << aCnt << endl;
	infile.close();
	return 0;
}

6.8 修改元音统计程序使其可以统计出读入的空格、制表符、换行符的个数。

int main()
{
	ifstream infile;
	infile.open("test.txt");
	char ch;
	int bCnt(0), eCnt(0), tCnt(0);
	infile >> noskipws;
	while (!infile.eof()){
		infile >> ch;
		switch (ch){
		case '\n':
			++eCnt;
		case '\t':
			++tCnt;
		case ' ':
			++bCnt;
		}
	}
	cout << "空格符的个数为:" << bCnt << endl;
	cout << "换行符的个数为:" << eCnt << endl;
	cout << "制表符的个数为:" << tCnt << endl;
	infile.close();
	return 0;
}

6.9 修改元音统计程序使其可以统计出以下双字符序列出现的次数:ff、fl以及fi。《转》

int main()
{
	ifstream infile;
	infile.open("test.txt");
	char chL('\0'),chC('\0'),ch;
	int fCnt(0), lCnt(0), iCnt(0);
	infile >> noskipws;
	infile >> chL;
	while (!infile.eof()){
		infile >> ch;
		chL = chC;
		chC = ch;
		if (chL == 'f'){
			switch (chC){
			case 'f':
				++fCnt;
			case 'l':
				++lCnt;
			case 'i':
				++iCnt;
			}
		}
	}
	cout << "the number of ff is :" << fCnt << endl;
	cout << "the number of fl is :" << lCnt << endl;
	cout << "the number of fi is :" << iCnt << endl;
	infile.close();
	return 0;
}

6.12 编写一个小程序,从标准输入读入一系列string对象,寻找连续重复出现的单词。程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现单词本身。跟踪重复次数最多的单词及其重复次数。输出重复次数的最大值,若没有单词重复则输出说明信息。例如,如果输入是:how, now now now brown cow cow 则输出应表明now这个单词出现了三次。


int main()
{
	string prestr, Curstr;
	string Mstr;
	int Mnum(1),num(0);
	while (cin >> Curstr){
		if (Curstr == prestr){
			++num;
		}
		else{
			if (num > Mnum){
				Mnum = num;
				Mstr = prestr;
			}
			num = 1;
		}
		prestr = Curstr;
	
	}
	if (num > Mnum){
		Mnum = num;
		Mstr = prestr;
	}
	if (Mnum != 1){
		cout << '"' << Mstr << '"' << "出现次数最多,出现了" << Mnum << "次" << endl;
	}
	else
		cout << "没有单词重复出现" << endl;
	return 0;
}


6.16 给出两个int型的vector对象,编写程序判断一个对象是否是另一个对象的前缀。如果两个vector对象的长度不相同,假设较短的vector对象长度为n,则只对这两个对象的前面n个元素做比较。例如,对于(0112)和(0112358)这两个vector,你的程序应该返回true

int main()
{
	vector<int> ivec1, ivec2;
	ivec1 = { 0, 1, 1, 2 };
	ivec2 = { 0, 1, 1, 2, 3, 5, 8 };
	size_t i1 = ivec1.size();
	size_t i2 = ivec2.size();
	if (i1 < i2){
		size_t i = 0;
		for (; i < ivec1.size(); ++i){
			if (ivec1[i] != ivec2[i]){
				break;
			}
		}
		if (i == ivec1.size()){
			cout << "字符串1是字符串2 的 前缀!" << endl;
		}
	}
	else{
		if (i1>i2){
			size_t i = 0;
			for (; i < ivec2.size(); ++i){
				if (ivec2[i] != ivec1[i]){
					break;
				}
			}
			if (i == ivec2.size()){
				cout << "字符串2是字符串1 的 前缀!" << endl;
			}
		}
		else
			cout << "两个字符串相等!" << endl;
	}
	return 0;
}

6.18 编写一个小程序,由用户输入两个string对象,然后报告哪个string对象按字母排列次序而言比较小(也就是说,哪个的字典序靠前)。继续要求用户输入,直到用户请求退出为止。请使用string类型、string类型的小于操作符以及do while循环实现。

int main()
{
	string str,str1, str2;
	
	do{
		cout << "Enter the first string:" << endl;
		cin >> str1;
		cout << "Enter the second string:" << endl;
		cin >> str2;
		if (str1 < str2){
			cout << str1 << "  is smaller one" << endl;
		}
		else
		if (str1>str2){
			cout << str2 << "  is smaller one" << endl;
		}
		else
			cout << str2 << "==" << str1 << endl;
		cout << "  keep on? Enter[yes/no]:  ";
		
		cin >> str;
	} while (str[0] == 'y');
	return 0;
}

6.20 编写程序从标准输入读入一系列string对象,直到同一个单词连续出现两次,或者所有的单词都已经读完,才结束读取。请使用while循环,每次循环读入一个单词。如果连续出现相同的单词,便以break语句结束循环,此时,请输出这个重复出现的单词;否则输出没有任何单词连续重复出现的信息。

int main(){
	int fl = 0;
	string str1, str2;
	while (cin >> str2){
		if (str2 == str1){
			cout << str1 << "重复出现" << endl;
			fl = 1;
			break;
		}
		str1 = str2;
	}
	if (!fl){
		cout << "无重复单词出现" << endl;
	}

	return 0;

}

6.23-6.24 bitset类提供to_ulong操作,如果bitset提供的位数大于unsigned long的长度时,抛出一个overflow_error异常。编写产生这种异常的程序。

int main(){
	bitset<100>b(10101010101010101111);
	try{
		b.to_ulong();
	}
	catch (overflow_error err){
		cout << err.what() << endl;
	}
	return 0;

}

6.25 修改6.11节习题所编写的程序,使其可以有条件的输出运行时的信息。例如,可以输出每一个读入的单词,用来判断循环是否正确地找到第一个连续出现的以大写字母开头的单词。分别在打开和关闭调试器的情况下编译和隐形这个程序。

int main(){
	int fl = 0;
	string str1, str2;
	while (cin >> str2){
#ifndef NDEBUG
		cout <<"调试的单词为"<< str2 << endl;
#endif
		if (str2 == str1&&isupper(str2[0])){
			cout << str1 << "重复出现" << endl;
			fl = 1;
			break;
		}
		str1 = str2;
	}
	if (!fl){
		cout << "无重复单词出现" << endl;
	}

	return 0;

}








猜你喜欢

转载自blog.csdn.net/w_zhao/article/details/79748386
今日推荐