Qt+C/C++文章小说人物关系分析

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

 程序示例精选

Qt+C++文章小说人物关系分析

如需安装运行环境或远程调试见文章底部微信名片由专业技术人员远程协助

前言

这篇博客针对<<Qt+C/C++文章小说人物关系分析>>编写代码代码整洁规则易读。 学习与应用推荐首选。


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 创建元素操作函数

                3. 运行结果

         三在线协助


一、所需工具软件

          1. Visual Stuido

          2. C++

二、使用步骤

1.引入库

代码如下示例

#include "QtGuiApplication1.h"

#include<qDebug>
#include<QFile>

#include <fstream> 
#include<iostream>
using namespace std;

#include <sstream>
#include <unordered_map>

2.创建元素操作函数

代码如下示例

//读取人物名称
void QtGuiApplication1::readOnButton()
{
	//打开关键词文件
	ifstream ifs;
	ifs.open("name.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "打开文件失败" << endl;
		return;
	}

	//一行一行读取到数组中
	string buf;
	while (getline(ifs, buf))
	{
		cout << buf << endl;
		QString name = QString::buf);
		gNameList << name; 
	}
	ifs.close();

	ui.textEdit->setPlainText(gNameList);

}

//出现次数
void QtGuiApplication1::timesOnButton()
{
	qDebug() << QString::fromLocal8Bit("人名次数统计");

	//统计归零
	if (gNameList.size() == 0)
	{
		std::cout << "请倒入人名列表后再试" << std::endl;
	}

	gNameCounter.clear();

	for (auto name : gNameList)
	{
		std::string key = name.toLocal8Bit();
		gNameCounter[key] = 0;
	}

	//一行行读入小说
	ifstream ifs;
	ifs.open("天龙八部.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "打开文件失败" << endl;
		return;
	}

	//清空统计用的TABLE的数据
	gTable.clear();

	//统计结果显示到界面
	QStringList result;
	for (auto name : gNameList)
	{
		std::string keyword = name.toLocal8Bit();
		QString tmp = name + ":" + QString::number(gNameCounter[keyword]);
		result << tmp;

		std::cout << keyword << " " << gNameCounter[keyword] << std::endl;

	}
	ui.textEdit_3->setText(result.join("\n"));
}

//篇幅跨度
void QtGuiApplication1::rangeOnButton()
{
	ui.textEdit_3->clear();
	for (auto name : gNameList)
	{
		std::string keyword = name.toLocal8Bit();

		int firstLineNum = -1;
		int lastLineNum = -1;
		for (int i = 0; i < gTable.size(); i++)
		{
			auto& row = gTable.at(i);
			//std::cout << "行号:" << i << " ";

			int KeyWordCounts = row[keyword];
			if (KeyWordCounts > 0)
			{
				if (firstLineNum == -1)
				{
					firstLineNum = i;
				}

				lastLineNum = i;
			}
		}


		ui.textEdit_3->append(QString::number(lastLineNum - firstLineNum));
	}
}

//关系最紧密两人
void QtGuiApplication1::relationGoodOnButton()
{
	ui.textEdit_3->clear();
	qDebug() << "test";

	//关系紧密算法原理每个人名在第几行出现的行数集合和另一个人名行数集合求距离取最小值为它的精密度
	//值越小的那个为此人和另一个人的最精密度然后同样的方法计算出此人与其它人的精密度最终取得
	//谁和这个人最紧密

	std::map < std::string, std::vector<int>> DataContainer;
	//正在对比的两个选手
	std::string player1, player2;
	int theMinDistance = 9999999999999999;
	for (auto name : gNameList)
	{
		std::string keyword = name.toLocal8Bit();
		std::vector<int> rowNums;

		for (int i = 0; i < gTable.size(); i++)
		{
			auto& row = gTable.at(i);
			//std::cout << "行号:" << i << " ";
			int KeyWordCounts = row[keyword];
			if (KeyWordCounts > 0)
			{
				rowNums.push_back(i);
			}
		}
		DataContainer[keyword] = rowNums;
	}



	ui.textEdit_3->append(QString::number(theMinDistance)+ QStringLiteral("行"));

}



3.运行结果如下 

三、在线协助 

如需安装运行环境或远程调试见文章底部微信名片由专业技术人员远程协助

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: c++