Untitled

 avatar
unknown
c_cpp
a year ago
2.9 kB
3
Indexable
#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 isConstant(string word){
    for(auto x : word){
        if(isdigit(x)){
            return 1;
        }
    }
    return 0;
}


void showProgress(int keywords, int identifiers, int operators, int symbols, int contants) {
    cout << "=====================" << endl;
    cout << "---Progress---" << endl;
    cout << "Keywords : " << keywords << endl;
    cout << "Identifiers: " << identifiers << endl;
    cout << "Operators : " << operators << endl;
    cout << "Symbols : " << symbols << endl;
    cout << "Constants : " << contants << endl << endl;
}





int main() {
    string myText;

    ifstream readMyFile("text.txt");

    if (!readMyFile.is_open()) {
        cerr << "Error opening the file." << endl;
        return 1;
    }

    while (readMyFile >> myText) {
        if (isSymbol(myText) == 1) {
            cout << myText << " is a Symbol." << endl;
            countSymbols++;
        }else 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 (isConstant(myText) == 1) {
            cout << myText << " is a Constant." << endl;
            countConstants++;
        } else{
            cout << myText << " is an Identifier." << endl;
            countIdentifiers++;
        }

        showProgress(countKeywords,countIdentifiers,countOperators,countSymbols, countConstants);
    }

    readMyFile.close();

    cout << "\n\nTOTAL KEYWORDS = " << countKeywords << endl;
    cout << "TOTAL IDENTIFIERS = " << countIdentifiers << endl;
    cout << "TOTAL OPERATORS = " << countOperators << endl;
    cout << "TOTAL SYMBOLS = " << countSymbols << endl;
    cout << "TOTAL CONSTANTS = " << countConstants << endl;

    return 0;
}