Untitled
unknown
c_cpp
a month ago
2.0 kB
4
Indexable
Never
#include <iostream> #include <fstream> #include <string> int countKeywords = 0; int countOperators = 0; int countSymbols = 0; int countConstants = 0; int countIdentifiers = 0; using namespace std; int isOperator(string word) { string operators[] = {"+", "-", "*", "/", "%", "="}; for (int i = 0; i < 6; i++) { if (operators[i] == word) { return 1; // It's an operator } } return 0; // It's not an operator } int isKeyword(string word) { string keywords[] = {"include", "iostream", "int", "main", "using", "namespace", "std", "cout", "endl", "return"}; for (int i = 0; i < 10; i++) { if (keywords[i] == word) { return 1; // It's a keyword } } return 0; // It's not a keyword } int isSymbol(string word){ string symbols[] = {";", "#", "<", ">", "{", "}", "(", ")","<<", ">>"}; for (int i = 0; i < 9; i++) { if (symbols[i] == word) { return 1; // It's a keyword } } return 0; // It's not a keyword } int main() { string myText; ifstream readMyFile("/Users/jeth/Documents/School Files/CPP/Prog Technique/LEX MP/text.txt"); // Replace "your_file.txt" with the actual file path if (!readMyFile.is_open()) { cerr << "Error opening the file." << endl; return 1; } while (readMyFile >> myText) { if (isKeyword(myText) == 1) { cout << myText << " is a Keyword." << endl; countKeywords++; } else if (isOperator(myText) == 1) { cout << myText << " is an Operator." << endl; countOperators++; } else if (isSymbol(myText) == 1){ cout << myText << " is a Symbol." << endl; countSymbols++; } } readMyFile.close(); cout << "TOTAL KEYWORDS = " << countKeywords << endl; cout << "TOTAL OPERATORS = " << countOperators << endl; cout << "TOTAL SYMBOLS = " << countSymbols << endl; return 0; }