Attribute Parser

  • + 0 comments

    can anyone help me with the code??

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <sstream>
    
    using namespace std;
    
    class Tags {
        vector<string> attributes, values;
    
        int attIndex(string& att) {
            for (int i = 0; i < attributes.size(); i++) {
                if (attributes[i] == att) {
                    return i;
                }
            }
            return -1;
        }
    
    public:
        void pushValues(string& att, string& val) {
            attributes.push_back(att);
            values.push_back(val);
        }
    
        void printQuery(string& att) {
            int index = attIndex(att);
            if (index == -1) {
                cout << "Not Found" << '\n';
            } else {
                cout << values[index] << '\n';
            }
        }
    };
    
    class HRML {
        vector<Tags> ts;
        int n, q;
    
    public:
        void setTags() {
            int equals = 0;
            Tags t;
            char c;
            string input, atts, vals;
            stringstream ss;
            string temp;
            cin >> n >> q;
            ts.resize(n);
    
            // Consume the newline character after reading n and q
            cin.ignore();
    
            for (int i = 0; i < n / 2; i++) {
                getline(cin, input);
                ss.clear();
                ss.str(input);
    
                ss >> temp;
                while (ss >> temp) {
                    if (temp == "=" && equals == 0) {
                        equals = 1;
                        continue;
                    } else if (equals == 1) {
                        vals = temp;
                        ts[i].pushValues(atts, vals);
                        equals = 0;
                    } else if (equals == 0) {
                        atts = temp;
                    }
                }
            }
    
            for (int i = 0; i < n / 2; i++) {
                cin >> temp;
            };
        }
    
        void printQueries() {
            stringstream ss1;
            int tagno = 0;
            string input, temp, tagName;
            for (int i = 0; i < q; i++) {
                getline(cin, input, '~');
                ss1.clear();
                ss1.str(input);
                getline(cin, tagName);
    
                tagno = 0;
                while (ss1 >> temp) {
                    tagno++;
                }
    
                // Check if tagno is within bounds
                if (tagno > 0 && tagno <= n) {
                    ts[tagno - 1].printQuery(tagName); // Adjusted index
                } else {
                    cout << "Not Found" << '\n';
                }
            }
        }
    };
    
    int main() {
        HRML a;
        a.setTags();
        a.printQueries();
    
        return 0;
    }