-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_check_21BCE3982.cpp
61 lines (49 loc) · 1.56 KB
/
operator_check_21BCE3982.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
#include <bits/stdc++.h>
using namespace std;
bool isValidOperator(const string& token) {
// List of valid operators
const string operators[] = {
"+", "-", "*", "/", "=", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "!",
"&", "|", "^", "~", "<<", ">>", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=",
"&=", "|=", "^=", "++", "--"
};
// Check if the token is a valid operator
for (const string& op : operators) {
if (token == op)
return true;
}
return false;
}
int main() {
ifstream fs(R"(C:\Users\khatu\github\CPPCode\operator_file.txt)", ios::in);
if (!fs) {
cerr << "Failed to open the file." << endl;
return 1;
}
string line;
int lineNumber = 1;
while (getline(fs, line)) {
string token;
size_t pos = 0;
while ((pos = line.find_first_of(" \t", pos)) != string::npos) {
if (pos > 0) {
token = line.substr(0, pos);
if (isValidOperator(token))
cout << "Valid operator " << lineNumber << ": " << token << endl;
else
cout << "Not a valid operator " <<": " << token << endl;
}
line.erase(0, pos + 1);
pos = 0;
}
if (!line.empty()) {
if (isValidOperator(line))
cout << "Valid operator "<<": " << line << endl;
else
cout << "Not a valid operator " <<": " << line << endl;
}
lineNumber++;
}
fs.close();
return 0;
}