Question
Trying to fix my code to pass these unit tests CPP FILE (only allowed to edit this file) #include balancer.h using namespace std; void Balancer::tag(std::string
Trying to fix my code to pass these unit tests
CPP FILE (only allowed to edit this file)
#include "balancer.h"
using namespace std;
void Balancer::tag(std::string aTag) { // Check if it is a closing tag if (aTag[1] == '/') { // Extract the tag name from the closing tag std::string tagName = aTag.substr(2, aTag.length() - 3);
// Check if the tag stack is empty if (tagStack.empty()) { error = true; return; }
// Compare the tag name with the top of the stack std::string topTag = tagStack.top(); if (topTag != tagName) { error = true; return; }
// Pop the top of the stack if it matches tagStack.pop(); } else { // Extract the tag name from the opening tag std::string tagName = aTag.substr(1, aTag.length() - 2);
// Push the tag name to the stack tagStack.push(tagName); } }
int Balancer::status() const { if (error) { return -1; } if (tagStack.empty()) { return 1; } return 0; }
HEADER FILE
#ifndef BALANCER_H #define BALANCER_H
#include #include
/** * XHTML Balancer class */ class Balancer { public: /** * Create a new balancer. */ Balancer();
/** * Process a new tag. * * @param aTag the lexeme (full string text) of a tag. */ void tag (std::string aTag);
/** * Inquire as to the balance status of the tags seen so far. * * @return 1 if all opening tags seen so far have been properly matched by * a closing tag * * 0 if no mismatches have been detected, but at least one opening tag * seen so far has not been properly matched by a closing tag * * -1 if we have seen at least one instance of a closing tag that * did not match the most recently added and unmatched opening tag. * Note that once a -1 has been returned, all subsequent calls to * status() must return -1, no matter what additional tags are added. */ int status () const;
private: bool error; /// > tagStack; };
#endif
3. You are given a driver for the XHTML validator. The code you are provided with will read an XHTML file and scan for any tags. It passes each tag it encounters to a Balancer whose job is to make sure that the tags are occurring in properly balanced pairs. The driver is designed to take the path to the file to be checked as a command line parameter. . /xmlcheck test0.html 4. You must implement the Balancer class. This class uses a std: : stack to track unmatched tags. The two most critical operations the Balancer class must support are - tag (string aTag): indicates that the driver has encountered a tag. The full text of the tag is passed as the parameter atag. This may be an opening tag, a closing tag, or a singleton tag. - status ( ): returns a single int indicating the status of what has been observed so far: - 1 indicates that an error has been detected. - 0 indicates that no error has been detected, but that one or more opening tags have yet to be matched - 1 indicates that no error has been detected and there are no opening tags that have yet to be matched against a closing tag
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started