/
exercise5.cpp
78 lines (62 loc) · 1.99 KB
/
exercise5.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
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 5 - Exercise 5
(*1.5) Use a regex to find all decimal numbers in a file.
*/
// https://github.com/l-paz91/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
void createFile(const string& pFileName)
{
ofstream ofs(pFileName);
if (!ofs)
{
error("Error creating file ", pFileName);
}
ofs << "This is a test file with some numbers in it.\n";
ofs << "The cat is 3.5 years old and in 2 years it will be 5.5 years old.\n";
ofs << "The car costs 10000.99 dollars.\n";
ofs << "The house is 250000.00 dollars.\n";
ofs << "There are 5.5 dogs lying in the sun.\n";
ofs << "The computer is 1500.00 dollars.\n";
ofs << "The phone is 500.00 dollars.\n";
ofs << "The book is 20.00 dollars.\n";
ofs << "3.67 pineapples can be used to make a pineapple pie.\n";
}
// -----------------------------------------------------------------------------
void findNumbers(const string& pFileName)
{
ifstream ifs(pFileName);
if (!ifs)
{
error("Error opening file ", pFileName);
}
regex pattern(R"(\d+\.\d+|\d+)");
int lineNumber = 0;
for (string line; getline(ifs, line); )
{
++lineNumber;
smatch matches;
while (regex_search(line, matches, pattern))
{
cout << "Line " << lineNumber << ": " << matches[0] << endl;
line = matches.suffix().str();
}
}
}
// -----------------------------------------------------------------------------
int main()
{
const string fileName = "Chapter5Exercise5.txt";
createFile(fileName);
findNumbers(fileName);
keep_window_open();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------