1. Visual Stuido
2. C++
代码如下(示例):
1 2 3 4 5 6 7 8 9 10 11 |
#include "QtGuiApplication1.h"
#include<qDebug> #include<QFile>
#include <fstream> #include<iostream> using namespace std;
#include <sstream> #include <unordered_map> |
代码如下(示例):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
//读取人物名称 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("行"));
} |