· 4 years ago · Aug 09, 2021, 01:30 AM
1PASTEBIN
2paste
3LOGIN SIGN UP
4
5Ad by Valueimpression
6SHARE
7TWEET
8Guest User
9string_util
10A GUEST
11AUG 8TH, 2021
1259
13NEVER
14Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
15 3.46 KB
16
17//string_util.h
18
19//Header providing string utilities delcarations
20#include <iostream>
21#include <string>
22#include <map>
23
24namespace CharlesLib{
25
26 class string_util
27 {
28 public:
29 //Converts a string number into a int(five -> 5)
30 static int textToInt(std::string text);
31
32 //Converts a string number into a int(five -> 5)
33 static int textToInt(std::string text,int a);
34 };
35}
36
37
38
39
40
41//string_util.cpp
42//CPP providing string utilities defintions
43
44//For command line build:
45// string_util.cpp
46// compile with: cl /c /EHsc string_util.cpp
47// post-build command: lib string_util.obj
48
49#include "string_util.h"
50namespace CharlesLib {
51
52 //map of hundreds
53 std::map<std::string, unsigned int> hundreds{ {"one hundred", 100},{"two hundred", 200},{"three hundred", 300},{"four hundred", 400},{"five hundred",500},{"six hundred", 600},
54 {"seven hundred",700},{"eight hundred", 800},{"nine hundred", 900} };
55
56 //map of ones; also includes exceptions, namely: eleven and 12
57 std::map<std::string, unsigned int> ones{ {"one", 1},{"two", 2},{"three", 3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9},{"eleven",11},{"twelve",12} };
58
59 //map of tens
60 std::map<std::string, unsigned int> tens{ {"ten", 10},{"twenty",20},{"thirty",30},{"forty",40},{"fifty",50},{"sixty",60},{"seventy",70},{"eighty",80},{"ninty",90} };
61
62 int string_util::textToInt(std::string text) {
63 int sum = 0;//store number as unt
64 if (text.find("hundred") != std::string::npos) {//see if there is hundred
65 for (auto const& [key, value] : hundreds) {
66 if (text.find(key) != std::string::npos) {//find the hundred
67 sum += value;
68 std::string::size_type i = text.find(key);
69
70 if (i != std::string::npos)
71 {
72 text.erase(i, key.length());//remove hundred
73 }
74 break;
75 }
76 }
77 }
78
79 for (auto const& [key, value] : tens) {
80 if (text.find(key) != std::string::npos) {//find the 10s
81 sum += value;
82 std::string::size_type i = text.find(key);
83
84 if (i != std::string::npos)
85 {
86 text.erase(i, key.length());//remove tens
87 }
88 break;
89 }
90 }
91
92 for (auto const& [key, value] : ones) {
93 if (text.find(key) != std::string::npos) {
94 sum += value;
95 std::string::size_type i = key.find(key);//find ones
96
97 if (i != std::string::npos)
98 {
99 text.erase(i, text.length());//remove ones
100 }
101 break;
102 }
103 }
104 return sum;
105 }
106
107
108 int string_util::textToInt(std::string text, int a) {
109 int sum = 0;//store number as unt
110 if (text.find("hundred") != std::string::npos) {//see if there is hundred
111 for (auto co nst& [key, value] : hundreds) {
112 if (text.find(key) != std::string::npos) {//find the hundred
113 sum += value;
114 std::string::size_type i = text.find(key);
115
116 if (i != std::string::npos)
117 {
118 text.erase(i, key.length());//remove hundred
119 }
120 break;
121 }
122 }
123 }
124
125 for (auto const& [key, value] : tens) {
126 if (text.find(key) != std::string::npos) {//find the 10s
127 sum += value;
128 std::string::size_type i = text.find(key);
129
130 if (i != std::string::npos)
131 {
132 text.erase(i, key.length());//remove tens
133 }
134 break;
135 }
136 }
137
138 for (auto const& [key, value] : ones) {
139 if (text.find(key) != std::string::npos) {
140 sum += value;
141 std::string::size_type i = key.find(key);//find ones
142
143 if (i != std::string::npos)
144 {
145 text.erase(i, text.length());//remove ones
146 }
147 break;
148 }
149 }
150 return sum;
151 }
152}
153
154
155RAW Paste Data
156//string_util.h
157
158//Header providing string utilities delcarations
159#include <iostream>
160#include <string>
161#include <map>
162
163namespace CharlesLib{
164
165 class string_util
166 {
167 public:
168 //Converts a string number into a int(five -> 5)
169 static int textToInt(std::string text);
170
171 //Converts a string number into a int(five -> 5)
172 static int textToInt(std::string text,int a);
173 };
174}
175
176
177
178
179
180//string_util.cpp
181//CPP providing string utilities defintions
182
183//For command line build:
184// string_util.cpp
185// compile with: cl /c /EHsc string_util.cpp
186// post-build command: lib string_util.obj
187
188#include "string_util.h"
189namespace CharlesLib {
190
191 //map of hundreds
192 std::map<std::string, unsigned int> hundreds{ {"one hundred", 100},{"two hundred", 200},{"three hundred", 300},{"four hundred", 400},{"five hundred",500},{"six hundred", 600},
193 {"seven hundred",700},{"eight hundred", 800},{"nine hundred", 900} };
194
195 //map of ones; also includes exceptions, namely: eleven and 12
196 std::map<std::string, unsigned int> ones{ {"one", 1},{"two", 2},{"three", 3},{"four",4},{"five",5},{"six",6},{"seven",7},{"eight",8},{"nine",9},{"eleven",11},{"twelve",12} };
197
198 //map of tens
199 std::map<std::string, unsigned int> tens{ {"ten", 10},{"twenty",20},{"thirty",30},{"forty",40},{"fifty",50},{"sixty",60},{"seventy",70},{"eighty",80},{"ninty",90} };
200
201 int string_util::textToInt(std::string text) {
202 int sum = 0;//store number as unt
203 if (text.find("hundred") != std::string::npos) {//see if there is hundred
204 for (auto const& [key, value] : hundreds) {
205 if (text.find(key) != std::string::npos) {//find the hundred
206 sum += value;
207 std::string::size_type i = text.find(key);
208
209 if (i != std::string::npos)
210 {
211 text.erase(i, key.length());//remove hundred
212 }
213 break;
214 }
215 }
216 }
217
218 for (auto const& [key, value] : tens) {
219 if (text.find(key) != std::string::npos) {//find the 10s
220 sum += value;
221 std::string::size_type i = text.find(key);
222
223 if (i != std::string::npos)
224 {
225 text.erase(i, key.length());//remove tens
226 }
227 break;
228 }
229 }
230
231 for (auto const& [key, value] : ones) {
232 if (text.find(key) != std::string::npos) {
233 sum += value;
234 std::string::size_type i = key.find(key);//find ones
235
236 if (i != std::string::npos)
237 {
238 text.erase(i, text.length());//remove ones
239 }
240 break;
241 }
242 }
243 return sum;
244 }
245
246
247 int string_util::textToInt(std::string text, int a) {
248 int sum = 0;//store number as unt
249 if (text.find("hundred") != std::string::npos) {//see if there is hundred
250 for (auto const& [key, value] : hundreds) {
251 if (text.find(key) != std::string::npos) {//find the hundred
252 sum += value;
253 std::string::size_type i = text.find(key);
254
255 if (i != std::string::npos)
256 {
257 text.erase(i, key.length());//remove hundred
258 }
259 break;
260 }
261 }
262 }
263
264 for (auto const& [key, value] : tens) {
265 if (text.find(key) != std::string::npos) {//find the 10s
266 sum += value;
267 std::string::size_type i = text.find(key);
268
269 if (i != std::string::npos)
270 {
271 text.erase(i, key.length());//remove tens
272 }
273 break;
274 }
275 }
276
277 for (auto const& [key, value] : ones) {
278 if (text.find(key) != std::string::npos) {
279 sum += value;
280 std::string::size_type i = key.find(key);//find ones
281
282 if (i != std::string::npos)
283 {
284 text.erase(i, text.length());//remove ones
285 }
286 break;
287 }
288 }
289 return sum;
290 }
291}
292create new paste / syntax languages / archive / faq / tools / night mode / api / scraping api / news / pro
293privacy statement / cookies policy / terms of serviceupdated / security disclosure / dmca / report abuse / contact
294
295By using Pastebin.com you agree to our cookies policy to enhance your experience.
296Site design & logo © 2021 Pastebin
297We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
298Not a member of Pastebin yet?
299Sign Up, it unlocks many cool features!
300