文本查询程序

目的:熟悉各种关联容器的使用以及面向具体任务时程序的设计流程。

参考:c++primer 10.6

基本步骤:用户需求 -> 明确任务 -> 数据结构以及相关操作 ->定义相关类 ->根据操作定义类成员函数

任务描述:从已知文档中查询某个单词出现的次数,并列出所在行

任务分工:1.需要有一个函数来读取指定文件,将其存储在向量中并用一个map记录每个单词的所在行,map.first是单词,map.second 是一个由行号组成的set;

  2.需要一个查询函数,对特定的单词返回其行号的set;

  3.需要一个输出查询结果的函数(这个是对外接口,放在主函数中较为合适)。

数据格式的使用:vector非常适合存储和遍历;而map能提供方便的查询接口;set中存储所在行号的集合保证了唯一性和有序性。

具体代码如下:

TextQuery.h

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <stdexcept>
class TextQuery
{
public:
	TextQuery();
	~TextQuery();
	typedef std::vector<std::string>::size_type line_no;//为每一行的索引定义一个别名
	void read_file(std::ifstream & is);//读取文件  
private:
	void store_file(std::ifstream & is);//这两个函数对外不可见,被成员函数read_file调用
	std::vector<std::string> lines_of_text;
private:
	void build_map();//这两个函数对外不可见,被成员函数read_file调用
	std::map<std::string, std::set<line_no> > word_map;
public:
	std::set<line_no> run_query(const std::string & query_word);//查询单词所在的行号
	std::string tex_line(line_no line) const;//显示某行文本内容
};

 TextQuery.cpp

#include "TextQuery.h"


TextQuery::TextQuery()
{
}


TextQuery::~TextQuery()
{
}


void TextQuery::read_file(std::ifstream & is)
{
	store_file(is);
	build_map();
}


void TextQuery::store_file(std::ifstream & is)
{
	std::string textline;
	while (getline(is, textline))
	{
		lines_of_text.push_back(textline);
	}
}


void TextQuery::build_map()
{
	for (line_no line_num = 0; line_num != lines_of_text.size(); line_num++)
	{
		std::istringstream line(lines_of_text.at(line_num));
		std::string word;
		while (line >> word)
		{
			word_map[word].insert(line_num);
		}
	}
}


std::set<TextQuery::line_no> TextQuery::run_query(const std::string & query_word) 
{
	std::map <std::string, std::set<line_no> >::const_iterator loc = word_map.find(query_word);
	if (loc == word_map.end())
	{
		return std::set<line_no>();
	}
	else
	{
		return loc->second;
	}

	
}


std::string TextQuery::tex_line(line_no line) const
{
	if (line < lines_of_text.size())
		return lines_of_text.at(line);
	else
		throw std::out_of_range("line number out of range");
}

 main.cpp

#include "TextQuery.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

ifstream& open_file(ifstream &in, const string &file)
{
	in.close();
	in.clear();
	in.open(file.c_str());
	return in;
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
	return (ctr == 1) ? word : word + ending;//处理字符串中单复数输出的一个函数,7.3.2中有原型
}
void print_results(const set<TextQuery::line_no> & locs, const string & sought, const TextQuery & file)
{
	typedef set<TextQuery::line_no> line_nums;
	line_nums::size_type size = locs.size();
	cout << "\n" << sought << " occurs " << size << " " << make_plural(size, "time", "s") << endl;
	line_nums::const_iterator it = locs.begin();
	for (; it != locs.end(); it++)
	{
		cout << "\t(line" << (*it) << ")" << file.tex_line(*it) << endl;
	}

}
int main(int argc,char ** argv)
{
	ifstream infile;
	if (argc < 2 || !open_file(infile, argv[1]))
	{
		cerr << "no input file" << endl;
		return EXIT_FAILURE;//这是C中的一种抛出错误的处理
	}
	TextQuery tq;
	tq.read_file(infile);
	while (true)
	{
		cout << "enter word to look for ,or	q to quit:";
		string s;
		cin >> s;
		if (!cin || s == "q") break;
		set<TextQuery::line_no> locs = tq.run_query(s);
		print_results(locs, s, tq);
	}
	return 0;
}

 makefile如下:

edit:main.o TextQuery.o
	g++ -o edit main.o TextQuery.o
main.o:main.cpp TextQuery.h
	g++ -c main.cpp
TextQuery.o:TextQuery.cpp TextQuery.h
	g++ -c TextQuery.cpp
clean:
	rm main.o TextQuery.o

 

转载于:https://www.cnblogs.com/simayuhe/p/5865248.html

好的,下面是一个使用Python实现在Excel中快速查询文本程序。我们需要使用openpyxl库来读取和操作Excel文件,具体步骤如下: 1. 安装openpyxl库。在命令行中输入以下命令:`pip install openpyxl` 2. 导入openpyxl库和re库(用于正则表达式匹配)。 ```python import openpyxl import re ``` 3. 定义一个函数,接收Excel文件路径、工作表名称和要查询文本作为参数。在函数中打开Excel文件,选中指定的工作表,并遍历所有单元格,使用正则表达式查找匹配的文本。如果找到了匹配的单元格,打印出单元格的位置和值。 ```python def search_text_in_excel(filename, sheetname, searchtext): # 打开Excel文件 workbook = openpyxl.load_workbook(filename) # 选中指定的工作表 worksheet = workbook[sheetname] # 遍历所有单元格 for row in worksheet.iter_rows(): for cell in row: # 使用正则表达式查找匹配的文本 if re.search(searchtext, str(cell.value)): # 打印出单元格的位置和值 print(f"匹配到文本'{searchtext}',位置为({cell.row}, {cell.column}),值为{cell.value}") ``` 4. 调用函数并传入参数,即可执行查询操作。例如,查询文件名为data.xlsx、工作表名为Sheet1、要查询文本为"Python"的程序如下: ```python search_text_in_excel("data.xlsx", "Sheet1", "Python") ``` 注意:在实际使用中,需要根据Excel文件的具体格式和内容,调整正则表达式的匹配规则,以确保准确匹配到要查询文本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值