【C++——文件操作案例】

效果展示

实现步骤:

word类

头文件
cpp 复制代码
#pragma once
using namespace std;
#include<iostream>
#include<string>

class word
{
public:
	word(string s);    //构造函数,用来初始化单词(完整传入,不拆分)
	string getWord()const;  //获得当前对象存储的字符串,加const表示只读操作
	bool operator<(const word& a)const; //作用是完成内部排序,相同字符串相邻方便后续统计个数
	bool operator==(string s); //辅助统计(相同单词数量)和去重(避免重复打印)

private:
	string s;
};
源文件
cpp 复制代码
#include "word.h"

// 构造函数:类名::构造函数名(构造函数名=类名)
word::word(string s) {
	this->s = s;
}
//getWord是只读成员函数,返回字符串中存储的完整内容
string word::getWord()const {
	return s; 
}
//第一个const约束函数的对象a不被修改,第二个约数参数对象不被修改
bool word::operator<(const word& a)const {
	return s < a.getWord();
}

bool word::operator==(string s) {
	return this->s == s;
}

WordSet类

头文件
cpp 复制代码
#pragma once
#include<iostream>
#include"word.h"
using namespace std;
#include<set> //用set:自动排序和去重(用来管理有哪些单词)

class WordSet
{
private:
	set<word> set_1;
public:
	bool wordset_add(string s);
	void show();
};
源文件
cpp 复制代码
#include "WordSet.h"

bool WordSet::wordset_add(string s) {
	set_1.insert(word(s)); //将s完整插入到set_1中
	return 1;
}
void WordSet::show() {
	for (set<word>::iterator it = set_1.begin(); it != set_1.end(); it++) {  //或auto it
	//cout << it->getWord() << " ";   整体打印出来
	  cout << (*it).getWord() << " ";
	}
}

WordMap类

头文件
cpp 复制代码
#pragma once
#include<iostream>
using namespace std;
#include"word.h"
#include<map> //用map的键值对来存储单词出现次数
class WordMap
{
private:
	map<word,int> map_1;
public:
	bool wordmap_add(string s);
	void show();
};
源文件
cpp 复制代码
#include "WordMap.h"

bool WordMap::wordmap_add(string s) {
	map<word, int>::iterator it = map_1.find(s); //整体
	//核对的内容并非单个词,而是一整行字符串
	if (it == map_1.end()) { //迭代到最后说明没找到
		pair<word, int> p(word(s), 1); //没找到就'包装'成次数为1的word类对象添入map_1里
		map_1.insert(p);    
	}  //后面会进行空格及符号处理使其统计单个词的出现次数
	else {
		it->second++; //若迭代器没到最后,说明找到了相同数据,直接让表示次数的数据++
	}
	return 1;
}

void WordMap::show() {
	for (auto it = map_1.begin(); it != map_1.end(); it++) {
		cout << it->first.getWord() << ":出现" << it->second << "次" << endl;
	}
}

功能调用

头文件
cpp 复制代码
#pragma once
#include<iostream>
#include<sstream>
using namespace std;
#include"word.h"
#include"WordMap.h"
#include"WordSet.h"
#include<fstream>
#include<string>
#include<algorithm>
#include<numeric>
#include<iterator>

void dataProcessing(); //处理空格和符号
void showData();
源文件
cpp 复制代码
#include"myFunc.h"

//建立集合和映射的实例对象
WordSet set_111;
WordMap map_111;
void showData() {
	cout << endl;
	cout << "单词集合为:" << endl;
	set_111.show();
	cout << endl;
	cout << "单词集合及出现次数为:" << endl;
	map_111.show();
	cout << endl;
}

void dataProcessing() {
	//打开文件,读取文本
	int pos = 0;
	string s = " ";
	string delimet = ",.?";
	ifstream in1("file.txt");
	char c = 0;
	//提前检查下一个元素是否为结束符(后面是否还有元素)
	while (in1.peek() != EOF) { //后面有元素就读取当前元素并输出
		in1.read(&c, 1);  //最后一个元素会在peek遇到EOF之前read
		cout << c; //整个while就是在展示in1写入的所有字符,方便与下面去空格和字符的结果对比
	}
	in1.close();

	ifstream in("file.txt");
	while (!in.eof()) {
		getline(in, s);
		if (s == " ") {
			continue;
		}
		pos = 0;
		//npos表不存在的位置(没找到目标内容)
		//发现的第一个符号的位置不是不存在的(找到了符号)
		while ((pos = s.find_first_of(delimet, pos) != string::npos)){
			s.replace(pos, 1, " "); //替换该符号为空字符(相当于删除)
	}
	//把处理后的行转为字符串流(可逐个读取单词)
		string word;
		istringstream istream(s);
	while (istream >> word) { //从流里逐个读取单词直到结束
		if (word.empty()) {  //跳过空单词
			continue;
		}
		set_111.wordset_add(word);  //自动去重
		map_111.wordmap_add(word);  //加到映射里统计次数
	}
}
	in.close();
}

文件内容:

编译结果:

小插曲

虽然文件处理部分对符号进行了处理,但加入符号后产生了乱码。

下期见~

相关推荐
旖旎夜光3 小时前
C++(17)
c++·学习
Larry_Yanan4 小时前
Qt多进程(三)QLocalSocket
开发语言·c++·qt·ui
醒过来摸鱼4 小时前
Java classloader
java·开发语言·python
superman超哥4 小时前
仓颉语言中元组的使用:深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
小鸡吃米…4 小时前
Python - 继承
开发语言·python
JIngJaneIL4 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
祁思妙想4 小时前
Python中的FastAPI框架的设计特点和性能优势
开发语言·python·fastapi
唐装鼠5 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Lucas555555555 小时前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
源代码•宸5 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang