添加查找算法和SingleProblem

This commit is contained in:
huihut
2018-02-11 01:08:43 +08:00
parent 4667db6985
commit 8a3d4f0e08
29 changed files with 1212 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
// 蛮力字符串匹配
int BruteForceStringMatch(vector<char>& Total, vector<char>& Part)
{
int i, j;
for (i = 0; i < Total.size() - Part.size(); ++i) {
j = 0;
while (j < Part.size() && Part[j] == Total[i + j]) {
++j;
if (j == Part.size())
return i;
}
}
return -1;
}

View File

@@ -0,0 +1,20 @@
## 文件(文本)查找
### 代码
[文件(文本)查找代码](search.cpp)
### 功能说明
本程序实现对英文文本中关键字的查找
返回关键字出现的位置(第几个词)
### 代码简述
`output.txt`文件读入数据(英文文本)到 `vector<string>` 中存储
通过用户输入关键字(`keyword`)进行查找
输出查找到的关键字出现的位置(第几个词)

View File

@@ -0,0 +1,5 @@
Just arrived in Kathmandu tonight and needed to eat,
we checked Trip advisor and came here because it was close to hotel.
Certainly deserves its number 1 rating.
Food is so tasty and the atmosphere is very relaxed.
I would definitely recommend.

View File

@@ -0,0 +1,105 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#define OK 0
#define ERROR -1
#define INPUTNAME "input.txt"
// 查找
int search(vector<string>& v_data, string keyword) {
int i = 0;
// 遍历每个元素直到找到关键字
for (; i < v_data.size(); i++) {
// 比较关键字与每个词是否相等
if (0 == v_data[i].compare(keyword)){
// 相等则返回关键字的位序
return i;
}
}
return ERROR;
}
// 文件读取
int FileRead(vector<string>& v_data) {
ifstream f_in(INPUTNAME);
//判断读取失败
if (!f_in) {
cout << "文件读取失败!" << endl;
system("pause");
return ERROR;
}
string word;
// 文件逐个读取
while (!f_in.eof()) {
f_in >> word;
v_data.push_back(word);
}
// 关闭文件
f_in.close();
return OK;
}
// 界面
void Interface(string& keyword) {
cout << "-------------------- 文件查找 --------------------" << endl;
cout << "【说明】:本程序实现对文件内容的查找" << endl;
cout << "即从input.txt读入查找关键字的首次出现位置" << endl;
cout << "--------------------------------------------------" << endl;
cout << "请输入关键字:" << endl;
cout << "--------------------------------------------------" << endl;
cin >> keyword;
cout << "--------------------------------------------------" << endl;
}
int main() {
vector<string> v_data;
// 文件读取
if (ERROR == FileRead(v_data))
return ERROR;
int index = -1;
string keyword;
// 界面
Interface(keyword);
// 合法性检测
if (keyword.empty()) {
cout << "请输入合法的关键字!" << endl;
system("pause");
return ERROR;
}
// 查找
index = search(v_data, keyword);
//未找到输出
if (ERROR == index) {
cout << "未找到此关键字!" << endl;
system("pause");
return ERROR;
}
//输出找到的关键字索引
cout << "此关键字位于第 " << index + 1 << " 个词的位置!" << endl;
system("pause");
return OK;
}

View File

@@ -0,0 +1,24 @@
## 文件(文本)排序
### 代码
[文件(文本)排序代码](sort.cpp)
### 功能说明
本程序实现选择排序和冒泡排序两个排序算法
并且有从小到大和从大到小两种排序方式
用户可进行选择需要的方式
### 代码简述
`output.txt` 文件读入数据(数字)到 `vector<int>` 中存储
通过用户输入的排序算法(`i_algorithm`)和排序方式(`i_mode`
选择对于的选择排序(`SelectSort()`)或者冒泡排序(`BubbleSort()`)进行排序
排序后输出 `vector<int>``output.txt`

View File

@@ -0,0 +1 @@
33 22 66 99 11 68 39 89 107 749 20 6

View File

@@ -0,0 +1 @@
6 11 20 22 33 39 66 68 89 99 107 749

194
Algorithm/FileSort/sort.cpp Normal file
View File

@@ -0,0 +1,194 @@
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define OK 0
#define ERROR -1
#define INPUTNAME "input.txt"
#define OUTPUTNAME "output.txt"
// 选择排序
int SelectSort(vector<int>& v_data, int b_mode) {
size_t i_num = v_data.size(), temp = 0;
// 从小到大排
if (0 == b_mode) {
size_t min;
for (size_t i = 0; i < i_num - 1; i++) {
min = i;
for (size_t j = i + 1; j < i_num; j++)
if (v_data[min] > v_data[j])
min = j;
if (min != i) {
temp = v_data[min];
v_data[min] = v_data[i];
v_data[i] = temp;
}
}
}
// 从大到小排
else {
size_t max;
for (size_t i = 0; i < i_num - 1; i++) {
max = i;
for (size_t j = i + 1; j < i_num; j++)
if (v_data[max] < v_data[j])
max = j;
if (max != i) {
temp = v_data[max];
v_data[max] = v_data[i];
v_data[i] = temp;
}
}
}
return OK;
}
// 冒泡排序
int BubbleSort(vector<int>& v_data, int b_mode) {
size_t num = v_data.size(), temp = 0;
// 从小到大排
if (0 == b_mode) {
for (size_t i = 0; i < num - 1; i++) {
for (size_t j = 0; j < num - i - 1; j++) {
if (v_data[j] > v_data[j + 1]) {
temp = v_data[j];
v_data[j] = v_data[j + 1];
v_data[j + 1] = temp;
}
}
}
}
// 从大到小排
else {
for (size_t i = 0; i < num - 1; i++) {
for (size_t j = 0; j < num - i - 1; j++) {
if (v_data[j] < v_data[j + 1]) {
temp = v_data[j];
v_data[j] = v_data[j + 1];
v_data[j + 1] = temp;
}
}
}
}
return OK;
}
// 文件读取
int FileRead(vector<int>& v_data) {
ifstream f_in(INPUTNAME);
//判断读取失败
if (!f_in) {
cout << "文件读取失败!" << endl;
system("pause");
return ERROR;
}
int i_temp;
// 文件逐个读取
while (!f_in.eof()) {
f_in >> i_temp;
v_data.push_back(i_temp);
}
// 关闭文件
f_in.close();
return OK;
}
// 文件写入
int FileWrite(vector<int>& v_data) {
ofstream f_out(OUTPUTNAME);
// 判断读取失败
if (!f_out) {
cout << "文件写入失败!" << endl;
return ERROR;
}
// 文件逐个写入
for (int i = 0; i < v_data.size(); i++)
f_out << v_data[i] << " ";
f_out.close();
return OK;
}
// 界面
void Interface(int& i_algorithm, int& i_mode) {
cout << "-------------------- 文件排序 --------------------" << endl;
cout << "【说明】:本程序实现对文件内容的排序" << endl;
cout << "即从input.txt读入排序后写入到output.txt" << endl;
cout << "--------------------------------------------------" << endl;
cout << "请选择排序算法:" << endl;
cout << "【0】选择排序" << endl;
cout << "【1】冒泡排序" << endl;
cout << "--------------------------------------------------" << endl;
cin >> i_algorithm;
cout << "--------------------------------------------------" << endl;
cout << "请选择排序方式:" << endl;
cout << "【0】从小到大" << endl;
cout << "【1】从大到小" << endl;
cout << "--------------------------------------------------" << endl;
cin >> i_mode;
cout << "--------------------------------------------------" << endl;
}
int main() {
vector<int> v_data;
// 文件读取
if (ERROR == FileRead(v_data))
return ERROR;
int i_algorithm, i_mode;
// 界面
Interface(i_algorithm, i_mode);
// 排序算法选择检测
if (0 != i_algorithm && 1 != i_algorithm) {
cout << "排序算法选择错误!" << endl;
system("pause");
return ERROR;
}
// 排序方式选择检测
if (0 != i_mode && 1 != i_mode) {
cout << "排序方式选择错误!" << endl;
system("pause");
return ERROR;
}
// 排序
if (i_algorithm)
BubbleSort(v_data, i_mode);
else
SelectSort(v_data, i_mode);
// 文件写入
if (ERROR == FileWrite(v_data))
return ERROR;
cout << "排序完成,数据已写入:" << OUTPUTNAME << endl;
cout << "--------------------------------------------------" << endl;
system("pause");
return OK;
}

View File

@@ -0,0 +1,9 @@
// 顺序查找
int SequentialSearch(vector<int>& v, int k) {
int i = 0;
for (; i < v.size(); ++i)
if (v[i] == k)
return i;
if (i == v.size())
return -1;
}