小言_互联网的博客

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

240人阅读  评论(0)

 程序示例精选

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

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

前言

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


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 创建元素操作函数

                3. 运行结果

         三在线协助


一、所需工具软件

          1. Visual Stuido

          2. C++

二、使用步骤

1.引入库

代码如下(示例):


  
  1. #include "QtGuiApplication1.h"
  2. #include<qDebug>
  3. #include<QFile>
  4. #include <fstream>
  5. #include<iostream>
  6. using namespace std;
  7. #include <sstream>
  8. #include <unordered_map>

2.创建元素操作函数

代码如下(示例):


  
  1. //读取人物名称
  2. void QtGuiApplication1::readOnButton ()
  3. {
  4. //打开关键词文件
  5. ifstream ifs;
  6. ifs.open( "name.txt", ios::in);
  7. if (!ifs.is_open())
  8. {
  9. cout << "打开文件失败" << endl;
  10. return;
  11. }
  12. //一行一行读取到数组中
  13. string buf;
  14. while (getline(ifs, buf))
  15. {
  16. cout << buf << endl;
  17. QString name = QString::buf);
  18. gNameList << name;
  19. }
  20. ifs.close();
  21. ui.textEdit->setPlainText(gNameList);
  22. }
  23. //出现次数
  24. void QtGuiApplication1::timesOnButton ()
  25. {
  26. qDebug() << QString::fromLocal8Bit( "人名次数统计");
  27. //统计归零
  28. if (gNameList.size() == 0)
  29. {
  30. std:: cout << "请倒入人名列表后再试!" << std:: endl;
  31. }
  32. gNameCounter.clear();
  33. for ( auto name : gNameList)
  34. {
  35. std:: string key = name.toLocal8Bit();
  36. gNameCounter[key] = 0;
  37. }
  38. //一行行读入小说
  39. ifstream ifs;
  40. ifs.open( "天龙八部.txt", ios::in);
  41. if (!ifs.is_open())
  42. {
  43. cout << "打开文件失败" << endl;
  44. return;
  45. }
  46. //清空统计用的TABLE的数据
  47. gTable.clear();
  48. //统计结果显示到界面
  49. QStringList result;
  50. for ( auto name : gNameList)
  51. {
  52. std:: string keyword = name.toLocal8Bit();
  53. QString tmp = name + ":" + QString::number(gNameCounter[keyword]);
  54. result << tmp;
  55. std:: cout << keyword << " " << gNameCounter[keyword] << std:: endl;
  56. }
  57. ui.textEdit_3->setText(result.join( "\n"));
  58. }
  59. //篇幅跨度
  60. void QtGuiApplication1::rangeOnButton ()
  61. {
  62. ui.textEdit_3->clear();
  63. for ( auto name : gNameList)
  64. {
  65. std:: string keyword = name.toLocal8Bit();
  66. int firstLineNum = -1;
  67. int lastLineNum = -1;
  68. for ( int i = 0; i < gTable.size(); i++)
  69. {
  70. auto& row = gTable.at(i);
  71. //std::cout << "行号:" << i << " ";
  72. int KeyWordCounts = row[keyword];
  73. if (KeyWordCounts > 0)
  74. {
  75. if (firstLineNum == -1)
  76. {
  77. firstLineNum = i;
  78. }
  79. lastLineNum = i;
  80. }
  81. }
  82. ui.textEdit_3->append(QString::number(lastLineNum - firstLineNum));
  83. }
  84. }
  85. //关系最紧密两人
  86. void QtGuiApplication1::relationGoodOnButton ()
  87. {
  88. ui.textEdit_3->clear();
  89. qDebug() << "test";
  90. //关系紧密算法,原理每个人名在第几行出现的行数集合和另一个人名行数集合求距离,取最小值为它的精密度
  91. //值越小的那个为此人和另一个人的最精密度,然后同样的方法计算出此人与其它人的精密度,最终取得
  92. //谁和这个人最紧密
  93. std:: map < std:: string, std:: vector< int>> DataContainer;
  94. //正在对比的两个选手
  95. std:: string player1, player2;
  96. int theMinDistance = 9999999999999999;
  97. for ( auto name : gNameList)
  98. {
  99. std:: string keyword = name.toLocal8Bit();
  100. std:: vector< int> rowNums;
  101. for ( int i = 0; i < gTable.size(); i++)
  102. {
  103. auto& row = gTable.at(i);
  104. //std::cout << "行号:" << i << " ";
  105. int KeyWordCounts = row[keyword];
  106. if (KeyWordCounts > 0)
  107. {
  108. rowNums.push_back(i);
  109. }
  110. }
  111. DataContainer[keyword] = rowNums;
  112. }
  113. ui.textEdit_3->append(QString::number(theMinDistance)+ QStringLiteral( "行"));
  114. }

3.运行结果如下: 

三、在线协助: 

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


转载:https://blog.csdn.net/alicema1111/article/details/128599273
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场