Question
Need to write the code to fix these problems. Must edit the CPP File. Please do not copy and paste another similar question. CPP FILE
Need to write the code to fix these problems. Must edit the CPP File. Please do not copy and paste another similar question.
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
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