⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10_04.cpp

📁 一些C++的课件和实验源代码
💻 CPP
字号:
// 10_04.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector<string>	vecWords;	// 单词数组

bool less_than(const string& str1, const string& str2) {
	return str1.size() < str2.size();
}

class LessThan{
public:
	bool operator()(const string& str1, const string& str2) {
		return str1.size() < str2.size();
	}
};

class GreaterThan{
protected:	int _size;
public:
	GreaterThan(int size = 6) {_size = size;}
	bool operator()(const string& str) {
		return str.size() > _size;
	}
};


void Input(char* file) 
{
	ifstream fi(file);
	if (!fi) {	// 打开文件失败
		cout << "open file error." << endl;
	}
	else {
		while (!fi.eof()) {	// 文件未结束
			// 读入一个单词(空格结尾),并保存到数组中
			string strWord;
			fi >> strWord;
			vecWords.push_back(strWord);
		}
		fi.close();	// 关闭文件

		cout << "read " << vecWords.size() << " words from file '" << file << "':" << endl;
	}
}

void Output()
{
	// 将所有单词输出到屏幕
	for (int i = 0; i < vecWords.size(); i++) 
		cout << vecWords[i] << " ";
	cout << endl;
}

void RemoveArticle()
{
	// 删除一些无义冠词
	vector<string>::iterator itLast;
	itLast = remove(vecWords.begin(), vecWords.end(), "a");
	vecWords.resize(itLast - vecWords.begin());	// 删除之后需要重新计算数组大小

	itLast = remove(vecWords.begin(), vecWords.end(), "an");
	vecWords.resize(itLast - vecWords.begin());

	itLast = remove(vecWords.begin(), vecWords.end(), "the");
	vecWords.resize(itLast - vecWords.begin());

	itLast = remove(vecWords.begin(), vecWords.end(), "one");
	vecWords.resize(itLast - vecWords.begin());

	// 输出删除冠词后的文本
	cout << "\nThere are " << vecWords.size() << " words after remove articles:" << endl;
	Output();
}

void RemoveRepeat()
{
	vector<string>::iterator itLast = unique(vecWords.begin(), vecWords.end());
	vecWords.resize(itLast - vecWords.begin());	// 删除之后需要重新计算数组大小

	// 输出删除重复单词后的文本
	cout << "\nThere are " << vecWords.size() << " words after remove repeat:" << endl;
	Output();
}

void Sort()
{
	sort(vecWords.begin(), vecWords.end());

	// 输出排序后的单词
	cout << "\nThere are " << vecWords.size() << " words after sort:" << endl;
	Output();
}

void SortByLen()
{
//	stable_sort(vecWords.begin(), vecWords.end(), less_than);
	stable_sort(vecWords.begin(), vecWords.end(), LessThan());

	// 输出排序后的单词
	cout << "\nThere are " << vecWords.size() << " words after sort by length:" << endl;
	Output();
}

void Count()
{
	int num = count_if(vecWords.begin(), vecWords.end(), GreaterThan());

	// 输出长度大于6的单词数量
	cout << "\n\nThere are " << num << " words of which length is greater than 6." << endl;
}

int main(int argc, char* argv[])
{
	Input("srcfile.txt");
	Output();

	getch();
	RemoveArticle();

	getch();
	Sort();
	
	getch();
	RemoveRepeat();

	getch();
	SortByLen();

	getch();
	Count();

	getch();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -