前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++统计英文文章的单词数,花式输出!

c++统计英文文章的单词数,花式输出!

作者头像
废江_小江
发布2022-09-05 13:35:47
4390
发布2022-09-05 13:35:47
举报
文章被收录于专栏:总栏目

英文文章单词统计

功能 统计一篇英文文章,按单词出现次数输入,按单词字典序输出,按单词逆序输出

思路

先拿到文件中的单词,利用split函数分隔,原理是string的find_of_first函数,第二个参数很好用,可以根据分号逗号等等任意分隔。将分隔的一个一个单词存储到vector中。 然后遍历vector,存储在multimpa中,使用键值对方式,健是string,值是int,使用find函数,如果map中有则值加一,没有则放入。multimap中的会按照第一个元素排序输出, 即string的字典序输出。如何按照int从大到小输出?将map的值放到vector中,使用自定义排序写一个cmp函数,即可、

代码

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <cstring>
#include<fstream>
#include <vector>
#include  <map>
using namespace std;
 
 
//存储被切割后的string的容器
vector<string> res;
//
vector<pair<string,int>> vec;
 
	
//匹配逗号空格等等!正则表达式 
string Delema=" ,.\"";
 
fstream  datafile;
string filename = "a.txt",wordline;
 
 
typedef multimap<string,int> map_std;
map_std::iterator it;
map_std mp;
 
 //分割字符函数,不用细看 
 void split(string& s,vector<string>&res,string& delema1){
    string::size_type start=s.find_first_not_of(delema1,0);//找到第一个不为逗号的下标
    
    string::size_type pose=s.find_first_of(delema1,start);//找到第一个逗号的下标
    
    while(string::npos!=start||string::npos!=pose){//当即没有逗号也没有字符的时候结束
    
        res.push_back(s.substr(start,pose-start));
        start=s.find_first_not_of(delema1,pose);//更新start 从pose开始
        
        pose=s.find_first_of(delema1,start);//更新pos,从start开始
        
    }
}
 
void get_file(){
	cout<<"请输入要处理的英文附件!"<<endl;
	cin>>filename; 
	datafile.open(filename.c_str(),fstream::in);
	
	if(datafile.is_open() == false){
		cout<<"文件打开失败!"<<endl;
	}else{
		
		while(getline(datafile,wordline)){
			
			split(wordline,res,Delema);
		}
		
	}
	
//		for(auto& i:res)
//			cout<<i<<endl;
 
	
	//给res的单词放在map中 
	vector<string>::iterator first = res.begin();
	vector<string>::iterator last = res.end();	
	
	int num =1;
	string word="a";
	while(first!=last){
		word = *first;
		//现在mp中找找有没有word 
//		cout<<*first++<<endl;
		first++;
		it = mp.find(word);
		
		if(it == mp.end()){
			//mp中没有这个单词 
			mp.insert(make_pair(word,num)); 
		}else{
			
			//mp中有这个单词!
			it->second++;
		}
		
	}
}
 
bool cmp(pair<string,int> a,pair<string,int> b){
	
	return a.second > b.second;
}
 
void task1(){
	
	for(map_std::iterator p=mp.begin();p!=mp.end();p++){
		vec.push_back(make_pair(p->first,p->second));
		
	}
	
	sort(vec.begin(), vec.end(), cmp);
    for (auto i = 0; i < vec.size(); ++i)
        cout << vec[i].first << " " << vec[i].second << endl;
	
}
void task2(){
	
	map_std::iterator p = mp.begin();
	
	for(p = mp.begin();p!=mp.end();p++){
		cout<<p->first<<" "<<p->second<<endl;
	}
	
}
void task3(){
	
//	for(auto& i:res)
//			cout<<i<<endl;
 
	vector<string>::iterator first = res.begin();
	vector<string>::iterator last = res.end();	
	while(first !=last)
		cout<<*--last<<endl;
	
}
 
 
 
 
void print(){
	printf("\t1.按单词个数输出\n");
	printf("\t2.按单词顺序输出\n");
	printf("\t3.逆序输出\n");
}
void menu(){
	
	system("pause");
 
	system("cls");
	
	system("color 4E");//黑框界面颜色 
	printf("************欢迎进入系统***********\n");
	
	
}
int main() {
 
	get_file();//先拿到txt文件中的单词
	 
			
//	int num;
//	string word;
//	
//	cin>>word;
//	num=1;
//	mp.insert(make_pair(word,num));
//	cin>>word;
//	mp.insert(make_pair(word,num));
//	cin>>word;
//	mp.insert(make_pair(word,num));
//	
//	it = mp.find("aa");
//	if(it == mp.end()){
//		cout<<"0000000000"<<endl;
//	}else{
//		cout<<"zhaodao le !!!\n";
//		cout<<it->first<<" "<<it->second<<endl;
//		it->second++;
//		cout<<it->first<<" "<<it->second<<endl;
//	}
//	
//	map_std::iterator p = mp.begin();
//	
//	for(p = mp.begin();p!=mp.end();p++){
//		cout<<p->first<<" "<<p->second<<endl;
//	}
//	
	
	
	while(1){
		
		menu();
		print();
		int choice ;
		scanf("%d",&choice);
		
		if(choice == 1){
			task1();
		}else if(choice ==2){
			task2();
		}else if(choice ==3){
			task3();
		}else if(choice ==4){
			break;
		}else{
			cout<<"输入有误,请重新输入!"<<endl;
		}
		
	}
    return 0;
}

效果图

Snipaste_2021-05-30_23-23-10.png
Snipaste_2021-05-30_23-23-10.png

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:c++统计英文文章的单词数,花式输出!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-30),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 英文文章单词统计
  • 功能 统计一篇英文文章,按单词出现次数输入,按单词字典序输出,按单词逆序输出
  • 思路
  • 代码
  • 效果图
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档