-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgo.cpp
216 lines (190 loc) · 4.5 KB
/
algo.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "algo.h"
/* Remove extra blank space from a string */
string TrimSpace(string str) {
str.erase(remove(str.begin(),str.end(),' '),str.end());
return str;
}
/* Read weather data from a file */
vector<string> ReadFile(string name) {
fstream inputFile;
string aLine;
vector<string> result;
cout << name << " ... ";
inputFile.open(name.c_str());
while (inputFile) {
getline (inputFile, aLine);
if (aLine == "") {
continue;
}
result.push_back(aLine);
}
inputFile.close();
cout << "done!" << endl;
return result;
}
/* Get data from a string with seperator (a/b/c -> a b c)*/
vector<string> ProcessString(string input, string splitby) {
size_t pos = 0;
string element;
vector<string> result;
while ((pos = input.find(splitby)) != string::npos) {
element = input.substr(0, pos);
result.push_back (element);
input.erase(0, pos + splitby.length());
}
//Get result
result.push_back (input);
return (result);
}
/* Convert a string to integer using sstream */
int ConvertStringToInteger(string s) {
int x;
stringstream ss;
ss.clear();
ss.str(s);
ss >> x;
return x;
}
/* Generate 2d string array */
void GenerateStringMap(string** arr, int width, int height) {
for (int i = 0; i < height; i++) {
arr[i] = new string[width];
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = " ";
}
}
}
/* Generate 2d int array */
void GenerateIntegerMap(int** arr, int width, int height) {
for (int i = 0; i < height; i++) {
arr[i] = new int[width];
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
arr[i][j] = 0;
}
}
}
/* Get coordinate data inside bracket ([3, 4] -> 3 4) */
string ProcessCoordinate(string str) {
char chars[] = "[],";
for (unsigned int i = 0; i < sizeof(chars); ++i) {
str.erase (std::remove(str.begin(), str.end(), chars[i]), str.end());
}
return str;
}
/* Print 2d map to screen */
void Render(string** arr, int width, int height) {
string border = " ";
for(int i=0; i< width + 2; i++) {
border += "# ";
}
cout << " " << " " << border << endl;
for (int i = 0; i < height; i++) {
cout<< setw(2) << height - i - 1 << " " << "#" << " ";
for (int j = 0; j < width; j++) {
cout << arr[i][j] << " ";
}
cout << "#";
cout << endl;
}
cout << " " << " " << border << endl;
cout << setw(6) << " ";
for(int i=0; i< width; i++) {
cout << setw(2) << i << " ";
}
cout << "\n" << endl;
}
/* Freeze the screen until press enter */
void Freeze() {
string chh = "1";
cout << "Press <enter> to go back to main menu ..." << endl;
cin.ignore();
do {
getline(cin, chh);
} while(chh.length() != 0);
}
/* Reclaim memory */
void DeleteStringMap(string** arr, int height) {
for(int i=0;i<height;i++) {
delete [] arr[i];
}
delete [] arr;
}
void DeleteIntMap(int** arr, int height) {
for(int i=0;i<height;i++) {
delete [] arr[i];
}
delete [] arr;
}
/* Check neighbor to see which city it belongs to */
string SurroundCheck(string **map, int x, int y, int tableWidth, int tableHeight) {
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (dx == 0 && dy == 0) {
continue; // Skip the current position
}
int newX = x + dx;
int newY = y + dy;
if (newX >= 0 && newX < tableWidth && newY >= 0 && newY < tableHeight) {
const std::string& value = map[newX][newY];
if (value != " ") {
return value;
}
}
}
}
return "";
}
/* Convert data to LMH */
string ConvertToLMH(int val) {
string value;
switch(val) {
case 0 ... 34:
value = "L";
break;
case 35 ... 64:
value = "M";
break;
default:
value = "H";
break;
}
return value;
}
string ConvertToIndex(string val) {
string value;
if (TrimSpace(val).size() == 1) {
value = "0";
} else {
value = val.substr(0, 1);
}
return value;
}
/* Sleep function that can run on both window and unix */
void MySleep(int amount) {
#ifdef WIN32
Sleep(amount * 1000);
#define window
#else
sleep(amount);
#define unix
#endif
}
/* Clear screen function that can run on both window and unix */
void ClearScreen() {
#ifdef WIN32
system("cls");
#define window
#else
system("clear");
#define unix
#endif
}