Skip to content

Commit 8a3d4f0

Browse files
committed
添加查找算法和SingleProblem
1 parent 4667db6 commit 8a3d4f0

File tree

29 files changed

+1212
-0
lines changed

29 files changed

+1212
-0
lines changed

Algorithm/BruteForceStringMatch.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 蛮力字符串匹配
2+
int BruteForceStringMatch(vector<char>& Total, vector<char>& Part)
3+
{
4+
int i, j;
5+
for (i = 0; i < Total.size() - Part.size(); ++i) {
6+
j = 0;
7+
while (j < Part.size() && Part[j] == Total[i + j]) {
8+
++j;
9+
if (j == Part.size())
10+
return i;
11+
}
12+
}
13+
return -1;
14+
}

Algorithm/FileSearch/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
## 文件(文本)查找
3+
4+
### 代码
5+
6+
[文件(文本)查找代码](search.cpp)
7+
8+
### 功能说明
9+
10+
本程序实现对英文文本中关键字的查找
11+
12+
返回关键字出现的位置(第几个词)
13+
14+
### 代码简述
15+
16+
`output.txt`文件读入数据(英文文本)到 `vector<string>` 中存储
17+
18+
通过用户输入关键字(`keyword`)进行查找
19+
20+
输出查找到的关键字出现的位置(第几个词)

Algorithm/FileSearch/input.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Just arrived in Kathmandu tonight and needed to eat,
2+
we checked Trip advisor and came here because it was close to hotel.
3+
Certainly deserves its number 1 rating.
4+
Food is so tasty and the atmosphere is very relaxed.
5+
I would definitely recommend.

Algorithm/FileSearch/search.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
#include <string>
5+
6+
using namespace std;
7+
8+
#define OK 0
9+
#define ERROR -1
10+
#define INPUTNAME "input.txt"
11+
12+
// 查找
13+
int search(vector<string>& v_data, string keyword) {
14+
int i = 0;
15+
16+
// 遍历每个元素直到找到关键字
17+
for (; i < v_data.size(); i++) {
18+
19+
// 比较关键字与每个词是否相等
20+
if (0 == v_data[i].compare(keyword)){
21+
22+
// 相等则返回关键字的位序
23+
return i;
24+
}
25+
}
26+
return ERROR;
27+
}
28+
29+
30+
// 文件读取
31+
int FileRead(vector<string>& v_data) {
32+
33+
ifstream f_in(INPUTNAME);
34+
35+
//判断读取失败
36+
if (!f_in) {
37+
cout << "文件读取失败!" << endl;
38+
system("pause");
39+
return ERROR;
40+
}
41+
42+
string word;
43+
44+
// 文件逐个读取
45+
while (!f_in.eof()) {
46+
f_in >> word;
47+
v_data.push_back(word);
48+
}
49+
50+
// 关闭文件
51+
f_in.close();
52+
53+
return OK;
54+
}
55+
56+
// 界面
57+
void Interface(string& keyword) {
58+
cout << "-------------------- 文件查找 --------------------" << endl;
59+
cout << "【说明】:本程序实现对文件内容的查找" << endl;
60+
cout << "即从input.txt读入,查找关键字的首次出现位置" << endl;
61+
cout << "--------------------------------------------------" << endl;
62+
cout << "请输入关键字:" << endl;
63+
cout << "--------------------------------------------------" << endl;
64+
cin >> keyword;
65+
cout << "--------------------------------------------------" << endl;
66+
}
67+
68+
int main() {
69+
70+
vector<string> v_data;
71+
72+
// 文件读取
73+
if (ERROR == FileRead(v_data))
74+
return ERROR;
75+
76+
int index = -1;
77+
string keyword;
78+
79+
// 界面
80+
Interface(keyword);
81+
82+
// 合法性检测
83+
if (keyword.empty()) {
84+
cout << "请输入合法的关键字!" << endl;
85+
system("pause");
86+
return ERROR;
87+
}
88+
89+
// 查找
90+
index = search(v_data, keyword);
91+
92+
//未找到输出
93+
if (ERROR == index) {
94+
cout << "未找到此关键字!" << endl;
95+
system("pause");
96+
return ERROR;
97+
}
98+
99+
//输出找到的关键字索引
100+
cout << "此关键字位于第 " << index + 1 << " 个词的位置!" << endl;
101+
102+
system("pause");
103+
104+
return OK;
105+
}

Algorithm/FileSort/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
## 文件(文本)排序
3+
4+
### 代码
5+
6+
[文件(文本)排序代码](sort.cpp)
7+
8+
### 功能说明
9+
10+
本程序实现选择排序和冒泡排序两个排序算法
11+
12+
并且有从小到大和从大到小两种排序方式
13+
14+
用户可进行选择需要的方式
15+
16+
### 代码简述
17+
18+
`output.txt` 文件读入数据(数字)到 `vector<int>` 中存储
19+
20+
通过用户输入的排序算法(`i_algorithm`)和排序方式(`i_mode`
21+
22+
选择对于的选择排序(`SelectSort()`)或者冒泡排序(`BubbleSort()`)进行排序
23+
24+
排序后输出 `vector<int>``output.txt`

Algorithm/FileSort/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
33 22 66 99 11 68 39 89 107 749 20 6

Algorithm/FileSort/output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6 11 20 22 33 39 66 68 89 99 107 749

Algorithm/FileSort/sort.cpp

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
#define OK 0
8+
#define ERROR -1
9+
#define INPUTNAME "input.txt"
10+
#define OUTPUTNAME "output.txt"
11+
12+
// 选择排序
13+
int SelectSort(vector<int>& v_data, int b_mode) {
14+
15+
size_t i_num = v_data.size(), temp = 0;
16+
17+
// 从小到大排
18+
if (0 == b_mode) {
19+
size_t min;
20+
for (size_t i = 0; i < i_num - 1; i++) {
21+
min = i;
22+
for (size_t j = i + 1; j < i_num; j++)
23+
if (v_data[min] > v_data[j])
24+
min = j;
25+
if (min != i) {
26+
temp = v_data[min];
27+
v_data[min] = v_data[i];
28+
v_data[i] = temp;
29+
}
30+
}
31+
}
32+
33+
// 从大到小排
34+
else {
35+
size_t max;
36+
for (size_t i = 0; i < i_num - 1; i++) {
37+
max = i;
38+
for (size_t j = i + 1; j < i_num; j++)
39+
if (v_data[max] < v_data[j])
40+
max = j;
41+
if (max != i) {
42+
temp = v_data[max];
43+
v_data[max] = v_data[i];
44+
v_data[i] = temp;
45+
}
46+
}
47+
}
48+
49+
return OK;
50+
}
51+
52+
// 冒泡排序
53+
int BubbleSort(vector<int>& v_data, int b_mode) {
54+
55+
size_t num = v_data.size(), temp = 0;
56+
57+
// 从小到大排
58+
if (0 == b_mode) {
59+
for (size_t i = 0; i < num - 1; i++) {
60+
for (size_t j = 0; j < num - i - 1; j++) {
61+
if (v_data[j] > v_data[j + 1]) {
62+
temp = v_data[j];
63+
v_data[j] = v_data[j + 1];
64+
v_data[j + 1] = temp;
65+
}
66+
}
67+
}
68+
}
69+
70+
// 从大到小排
71+
else {
72+
for (size_t i = 0; i < num - 1; i++) {
73+
for (size_t j = 0; j < num - i - 1; j++) {
74+
if (v_data[j] < v_data[j + 1]) {
75+
temp = v_data[j];
76+
v_data[j] = v_data[j + 1];
77+
v_data[j + 1] = temp;
78+
}
79+
}
80+
}
81+
}
82+
83+
return OK;
84+
}
85+
86+
// 文件读取
87+
int FileRead(vector<int>& v_data) {
88+
89+
ifstream f_in(INPUTNAME);
90+
91+
//判断读取失败
92+
if (!f_in) {
93+
cout << "文件读取失败!" << endl;
94+
system("pause");
95+
return ERROR;
96+
}
97+
98+
int i_temp;
99+
100+
// 文件逐个读取
101+
while (!f_in.eof()) {
102+
f_in >> i_temp;
103+
v_data.push_back(i_temp);
104+
}
105+
106+
// 关闭文件
107+
f_in.close();
108+
109+
return OK;
110+
}
111+
112+
// 文件写入
113+
int FileWrite(vector<int>& v_data) {
114+
115+
ofstream f_out(OUTPUTNAME);
116+
117+
// 判断读取失败
118+
if (!f_out) {
119+
cout << "文件写入失败!" << endl;
120+
return ERROR;
121+
}
122+
123+
// 文件逐个写入
124+
for (int i = 0; i < v_data.size(); i++)
125+
f_out << v_data[i] << " ";
126+
127+
f_out.close();
128+
return OK;
129+
}
130+
131+
// 界面
132+
void Interface(int& i_algorithm, int& i_mode) {
133+
cout << "-------------------- 文件排序 --------------------" << endl;
134+
cout << "【说明】:本程序实现对文件内容的排序" << endl;
135+
cout << "即从input.txt读入,排序后写入到output.txt" << endl;
136+
cout << "--------------------------------------------------" << endl;
137+
cout << "请选择排序算法:" << endl;
138+
cout << "【0】选择排序" << endl;
139+
cout << "【1】冒泡排序" << endl;
140+
cout << "--------------------------------------------------" << endl;
141+
cin >> i_algorithm;
142+
cout << "--------------------------------------------------" << endl;
143+
cout << "请选择排序方式:" << endl;
144+
cout << "【0】从小到大" << endl;
145+
cout << "【1】从大到小" << endl;
146+
cout << "--------------------------------------------------" << endl;
147+
cin >> i_mode;
148+
cout << "--------------------------------------------------" << endl;
149+
}
150+
151+
int main() {
152+
153+
vector<int> v_data;
154+
155+
// 文件读取
156+
if (ERROR == FileRead(v_data))
157+
return ERROR;
158+
159+
int i_algorithm, i_mode;
160+
161+
// 界面
162+
Interface(i_algorithm, i_mode);
163+
164+
// 排序算法选择检测
165+
if (0 != i_algorithm && 1 != i_algorithm) {
166+
cout << "排序算法选择错误!" << endl;
167+
system("pause");
168+
return ERROR;
169+
}
170+
171+
// 排序方式选择检测
172+
if (0 != i_mode && 1 != i_mode) {
173+
cout << "排序方式选择错误!" << endl;
174+
system("pause");
175+
return ERROR;
176+
}
177+
178+
// 排序
179+
if (i_algorithm)
180+
BubbleSort(v_data, i_mode);
181+
else
182+
SelectSort(v_data, i_mode);
183+
184+
// 文件写入
185+
if (ERROR == FileWrite(v_data))
186+
return ERROR;
187+
188+
cout << "排序完成,数据已写入:" << OUTPUTNAME << endl;
189+
cout << "--------------------------------------------------" << endl;
190+
191+
system("pause");
192+
193+
return OK;
194+
}

Algorithm/SequentialSearch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 顺序查找
2+
int SequentialSearch(vector<int>& v, int k) {
3+
int i = 0;
4+
for (; i < v.size(); ++i)
5+
if (v[i] == k)
6+
return i;
7+
if (i == v.size())
8+
return -1;
9+
}

0 commit comments

Comments
 (0)