I have written a code to find all matches to the pattern like 23<=34 or 123<>2000 in the given file. More generally, a(sign)b where sign ∈ { =<, =, >, <=, <>, >= } and a,b ∈ N.
Now, I got stuck on extending this code to identify many joint patterns like a(sign)b(sign)c(sign)...(sign)d. This reminds me of finding the gcd of many numbers using only the gcd for two numbers. But I don't really know how to proceed. Assume that we already have the function solved() that works for only one pair (the code below if needed).
Summary of the problem: Creating a function that more or less looks like this:
void solve(string text) {
// using solved(string text) [but optional]
}
Maybe there are other ways, and I would be more than happy to see them!
Below is the code I have:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <regex>
using namespace std;
#define REGEX_SIGN "(=<|=|>|<=|<>|>=)"
#define REGEX_DIGIT "[0-9]"
#define REGEX_NUMBER "[^0]\\d*"
void check(string text) {
regex sign(REGEX_SIGN);
regex digit(REGEX_DIGIT);
regex number(REGEX_NUMBER);
regex relation(REGEX_NUMBER REGEX_SIGN REGEX_NUMBER);
string word = "";
for (int i = 0; i < text.length(); i++) {
if (text[i] == ' ') {
regex_match(word, relation) ? cout << "✔️ " : cout << "☒ ";
cout << word << " ";
cout << endl;
word = "";
} else {
word += text[i];
}
}
}
int main() {
string line, text;
ifstream fin;
fin.open("name.txt");
if (fin.good()) {
while (getline(fin, line)) {
text += line + " ";
}
check(text);
}
return 0;
}
