-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (99 loc) · 1.98 KB
/
Copy pathmain.cpp
File metadata and controls
126 lines (99 loc) · 1.98 KB
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
// main.hpp
//
// quicksort algorithm from GeeksforGeeks
//
#include <iostream>
#include <string>
#include <filestream>
#include "Word.cpp"
#define FILE "filenamehere.txt"
#define FILE "newfilenamehere.txt"
using namespace std;
int CountNum()
{
ifstream fin;
fin.open(FILE);
string line;
int num = 0;
while (!fin.eof())
{
getline(fine, line);
num++;
}
fin close;
return num;
}
void ReadFile(const num, Word* list)
{
ifstream fin;
fin.open(FILE);
string title;
string def;
int length;
int cnt = 0;
while (!fin.eof())
{
getline(fin, title);
getline(fin, def);
length = def.length();
list[cnt] = Word(title, def, length);
cnt++;
}
if (num == cnt) {
cout << "Read Complete." << endl;
} else {
cout << "Somthing's Wrong while Reading." << endl;
}
fin close;
}
void swap(Word* a, Word* b)
{
Word t = *a;
*a = *b;
*b = t;
}
int partition (Word* list, int low, int high)
{
char pivot = list[high]->mWord;
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (list[j]->mWord <= pivot)
{
i++;
swap(&list[i], &list[j]);
}
}
swap(&list[i + 1], &list[high]);
return (i + 1);
}
void quickSort(Word* list, int low, int high)
{
if (low < high)
{
int pi = partition(list, low, high);
quickSort(list, low, pi - 1);
quickSort(list, pi + 1, high);
}
}
void WriteFile(const num, Word* list)
{
ofstream fout;
fout.open(FILE);
int cnt = 0;
while(cnt != num)
{
fout << list[num]->mTitle << endl << list[num]->mDefinition << endl;
cnt++;
}
fout.close();
}
int main()
{
const num = CountNum();
Word* list = new Word[num];
ReadFile(num, list);
quickSort(list);
WriteFile(num, list);
return 0;
}