日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++從文件中提取英文單詞的實現方法_C 語言

作者:想吃讀研的苦 ? 更新時間: 2022-08-07 編程語言

首先,要準備好words.txt(英文文章)置于工程目錄下

思路:

1.打開文件

2.讀取每一行

3.找到特殊的標點符號的位置,進行刪除。

4.根據空格截取單詞 find(" ");

5.將拿到的每一個單詞放在鏈表中

一:讀取一行,去除該行標點符號

#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
 
int main()
{
	test_word_split();
	return 0;
}
 
void test_word_split()
{
	fstream fs;
	char filename[20] = {0};
	cout<<"請輸入打開的文件名:";
	cin>>filename;
	//打開文件
	fs.open(filename);
	cout<<"打開成功"<<filename<<endl;
	char buf[1024] = {0};
	fs.getline(buf,1024);//讀取每一行
	cout<<buf<<endl;
	size_t pos;   //找到位置
	string line;  //接替buf職責
	line = buf;
	pos = line.find_first_of(",.;:'?!()/\"");  //找特殊的標點符號
	while(pos!=string::npos)
	{   //刪除單個字符
		line.erase(pos,1);
		//再找下一個單個的字符
	   pos = line.find_first_of(",.;:'?!()/\""); 
	}
	cout<<line.c_str()<<endl; //string 轉char
}
 

二:截取單詞

#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
 
int main()
{
	test_word_split();
	return 0;
}
 
void test_word_split()
{
	fstream fs;
	char filename[20] = {0};
	cout<<"請輸入打開的文件名:";
	cin>>filename;
	//打開文件
	fs.open(filename);
	cout<<"打開成功"<<filename<<endl;
	char buf[1024] = {0};
	fs.getline(buf,1024);//讀取每一行
	cout<<buf<<endl;
	size_t pos;
	string line,word;
	line = buf;
	pos = line.find_first_of(",.;:'?!()/\"");  //找特殊的標點符號
	while(pos!=string::npos)
	{   //刪除單個字符
		line.erase(pos,1);   //從什么位置開始刪除多長的字符
		//再找下一個單個的字符
	    pos = line.find_first_of(",.;:'?!()/\""); 
	}
	cout<<line.c_str()<<endl; //string 轉char
	//根據空格截取單詞 find("")  111 222 333
	pos = line.find(" ");
	while(pos!=string::npos)
	{
		//截取單詞
		word = line.substr(0,pos);//從0開始,一直截到空格所在位置
		cout<<word<<endl;     
		//把第一個單詞以及空格刪除
		line.erase(0,pos+1);  //從什么位置開始刪除多長的字符(如刪111 )因此pos+1
		pos = line.find(" "); //尋找下一個空格
	}
}
 

三:將拿到的每一個單詞都放在鏈表中

#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
 
int main()
{
	test_word_split();
	return 0;
}
 
void test_word_split()
{
	list<string> wordList;//鏈表
	fstream fs;
	char filename[20] = {0};
	cout<<"請輸入打開的文件名:";
	cin>>filename;
	fs.open(filename);
	cout<<"打開成功"<<filename<<endl;
	char buf[1024] = {0};
	string line,word;  //初始化定義
	while(fs.getline(buf, 1024))//讀取每一行
	{
		size_t pos;  //找到位置
		line = buf;  //接替buf職責
		pos = line.find_first_of(",.;:'?!()/\"");
		while(pos!=string::npos)//!=npos就找到
		{
			line.erase(pos,1);  //從什么位置開始刪除多長字符
			pos = line.find_first_of(",.;:'?!()/\"");//尋找下一個標點符號
		}
		pos = line.find(" ");  //尋找空格所在位置
		while(pos!=string::npos)
		{
			word = line.substr(0,pos);//從0開始,一直截到空格所在位置
			wordList.push_back(word); //拿到的單詞放在鏈表中
			//把第一個單詞以及空格刪除
			line.erase(0, pos+1);//從什么位置開始刪除多長的字符(如刪111 )因此pos+1
			pos = line.find(" ");//尋找下一個空格
		}
	}
	cout<<"驗證一下"<<endl;
	list<string>::iterator it;
	for(it = wordList.begin();it!=wordList.end();it++)
	{
		cout<<(*it).c_str()<<endl;
	}
	cout<<"總的個數:"<<wordList.size();
	fs.close();
}

最后的結果:

原文鏈接:https://blog.csdn.net/m0_56051805/article/details/124395841

欄目分類
最近更新