-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
189 lines (157 loc) · 4.32 KB
/
utils.h
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
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
template <typename T> T abs(T n) { return (n < 0) ? -n : n; }
std::string ascii(const std::string &input) {
std::string result;
for (char c : input) {
if (static_cast<unsigned char>(c) > 127) {
// Replace non-ASCII character with escape sequence
result += "\\x" + std::to_string(static_cast<unsigned char>(c));
} else {
result += c;
}
}
return result;
}
template <typename T>
T input(const std::string &inputString, bool safe = false) {
T input;
if (safe) {
bool isValidInput = false;
while (!isValidInput) {
std::cout << inputString;
std::cin >> input;
if (std::cin.fail()) {
std::cout << "Invalid input. Please try again."
<< "\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
isValidInput = true;
}
}
} else {
std::cout << inputString;
std::cin >> input;
}
return input;
}
std::string input(const std::string &inputString, bool safe = false) {
std::string input;
if (safe) {
bool isValidInput = false;
while (!isValidInput) {
std::cout << inputString;
std::getline(std::cin >> std::ws, input);
if (std::cin.fail()) {
std::cout << "Invalid input. Please try again."
<< "\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
isValidInput = true;
}
}
} else {
std::cout << inputString;
std::cin >> input;
}
return input;
}
template <typename T> std::string to_string(const T &obj) {
std::ostringstream oss;
oss << obj;
return oss.str();
}
class PrintOptions {
public:
PrintOptions &separator(const std::string &sep) {
separator_ = sep;
return *this;
}
PrintOptions &end(const std::string &e) {
end_ = e;
return *this;
}
PrintOptions &flush(const bool &f = true) {
flush_ = f;
return *this;
}
std::string separator() const { return separator_; }
std::string end() const { return end_; }
private:
std::string separator_ = " ";
std::string end_ = "\n";
bool flush_ = false;
};
template <typename... Args> void print(const Args &...args) {
std::ostringstream oss;
((oss << to_string(args) << PrintOptions().separator()), ...);
// Add the end string to the output
oss << PrintOptions().end();
std::cout << oss.str();
}
template <typename... Args>
void print(const PrintOptions &options, const Args &...args) {
std::ostringstream oss;
((oss << to_string(args) << options.separator()), ...);
// Add the end string to the output
oss << options.end();
std::cout << oss.str();
}
long long stringToInt(const std::string &str) {
long long result = 0;
bool isNegative = false;
size_t startIndex = 0;
// Check if the string is empty
if (str.empty()) {
// Handle the error case appropriately
throw std::invalid_argument(
"Empty string cannot be converted to an integer.");
}
// Check if the string represents a negative number
if (str[0] == '-') {
isNegative = true;
startIndex = 1;
}
// Convert each digit character to its corresponding integer value
for (size_t i = startIndex; i < str.length(); i++) {
if (str[i] >= '0' && str[i] <= '9') {
long long digitValue = str[i] - '0'; // Convert character to integer value
// Multiply the result by 10 and add the digit value
result = result * 10 + digitValue;
} else {
// Handle the error case for invalid characters in the string
throw std::invalid_argument("Invalid character found in the string.");
}
}
// Apply the negative sign if necessary
if (isNegative) {
result = -result;
}
return result;
}
std::string intToString(long long number) {
std::ostringstream oss;
oss << number;
return oss.str();
}
long long floatToInt(long double number) {
return static_cast<long long>(number);
}
std::string floatToString(long double number) {
std::ostringstream oss;
oss << number;
return oss.str();
}
long double intToFloat(long long number) {
return static_cast<long double>(number);
}
long double stringToFloat(const std::string& str) {
std::istringstream iss(str);
long double number;
iss >> number;
return number;
}