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.

Need to write the code to fix these problems. Must edit the

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 #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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!