Question
Here is my code: #include #includestack.h #include #include #include using namespace std; string xml(string shuru); int main() { string input; cout < < Enter the
Here is my code:
#include
#include"stack.h"
#include
#include
#include
using namespace std;
string xml(string shuru);
int main() {
string input;
cout << "Enter the input" << endl;
getline(cin, input);
cout << "Output:" < } string xml(string shuru) { char token=' ',topToken1=' ',topToken2=' '; Stack opStack; Stack erStack; string out1; string out2; string out3; const string BLANK = " "; for(int j=0;j while (token != '\0') { token = shuru[j]; string tag; string endtag; topToken1 = opStack.top(); topToken2 = erStack.top(); if (shuru[j] == '<'&&shuru[j + 1] != '/') { while (topToken1 != '>') { opStack.push(token); } } else if (shuru[j] == '>') { if (!opStack.empty()) { topToken1 = opStack.top(); opStack.pop(); if (topToken1 == '<') { out1.append(BLANK + topToken1); } } } if (shuru[j] == '<'&&shuru[j + 1] == '/') { while (topToken2 != '>') { erStack.push(token); } } else if (shuru[j] == '>') { if (!erStack.empty()) { topToken2 = erStack.top(); erStack.pop(); if (topToken2 == '<') { out2.append(BLANK + topToken2); } } } if (opStack.empty()) { cout << "INVALID" << endl; } else if (out1 != out2) { cout << "INVALID" << endl; } else { cout << "VALID" << endl; out3 = out1 + out2; return out3; } } } } Here is the code for "stack.h": stack.h: /*-- DStack.h --------------------------------------------------- This header file defines a Stack data type. Basic operations: constructor: Constructs an empty stack empty: Checks if a stack is empty push: Modifies a stack by adding a value at the top top: Accesses the top stack value; leaves stack unchanged pop: Modifies stack by removing the value at the top display: Displays all the stack elements Class Invariant: 1. The stack elements (if any) are stored in positions 0, 1, . . ., myTop of myArray. 2. -1 <= myTop < myCapacity --------------------------------------------------------------*/ #include #include using namespace std; class Stack { public: /***** Function Members *****/ /***** Constructors *****/ Stack(int numElements = 1024); /*---------------------------------------------------------- Construct a Stack object. Precondition: None. Postcondition: An empty Stack object has been constructed (myTop is initialized to -1 and myArray is an array with numElements (default 128) elements of type StackElement). -----------------------------------------------------------*/ Stack(const Stack & original); /*---------------------------------------------------------- Copy Constructor Precondition: original is the stack to be copied and is received as a const reference parameter. Postcondition: A copy of original has been constructed. -----------------------------------------------------------*/ /***** Destructor *****/ ~Stack(); /*---------------------------------------------------------- Class destructor Precondition: None Postcondition: The dynamic array in the stack has been deallocated. -----------------------------------------------------------*/ /***** Assignment *****/ Stack & operator= (const Stack & original); /*---------------------------------------------------------- Assignment Operator Precondition: original is the stack to be assigned and is received as a const reference parameter. Postcondition: The current stack becomes a copy of original and a reference to it is returned. -----------------------------------------------------------*/ bool empty() const; /*----------------------------------------------------------- Check if stack is empty. Precondition: None Postcondition: Returns true if stack is empty and false otherwise. -----------------------------------------------------------*/ void push(char & value); /*----------------------------------------------------------- Add a value to a stack. Precondition: value is to be added to this stack Postcondition: value is added at top of stack provided there is space; otherwise, a stack-full message is displayed and execution is terminated. -----------------------------------------------------------*/ void display(ostream & out) const; /*----------------------------------------------------------- Display values stored in the stack. Precondition: ostream out is open. Postcondition: Stack's contents, from top down, have been output to out. -----------------------------------------------------------*/ char top() const; /*----------------------------------------------------------- Retrieve value at top of stack (if any). Precondition: Stack is nonempty Postcondition: Value at top of stack is returned, unless the stack is empty; in that case, an error message is displayed and a "garbage value" is returned. ----------------------------------------------------------*/ void pop(); /*----------------------------------------------------------- Remove value at top of stack (if any). Precondition: Stack is nonempty. Postcondition: Value at top of stack has been removed, unless the stack is empty; in that case, an error message is displayed and execution allowed to proceed. ----------------------------------------------------------*/ private: /***** Data Members *****/ int myCapacity; // capacity of stack char myTop; // top of stack string * myArray; // dynamic array to store elements }; // end of class declaration stack.cpp: /*-- DStack.cpp---------------------------------------------------------- This file implements Stack member functions. -------------------------------------------------------------------------*/ #include #include #include "stack.h" #include using namespace std; //--- Definition of Stack constructor Stack::Stack(int numElements) { myCapacity = numElements; // set stack capacity // allocate array of this capacity myArray = new string[myCapacity]; if (myArray != 0) // memory available myTop =' '; else { cerr << "Inadequate memory to allocate stack " " -- terminating execution "; exit(1); } // or assert(myArray != 0); } //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new string[myCapacity]; // allocate array in copy if (myArray != 0) // check if memory available // copy original's array member into this new array for (int pos = 0; pos < myCapacity; pos++) myArray[pos] = original.myArray[pos]; else { cerr << "*Inadequate memory to allocate stack *** "; exit(1); } } //--- Definition of Stack destructor Stack::~Stack() { delete[] myArray; } //--- Definition of assignment operator Stack & Stack::operator=(const Stack & original) { if (this != &original) // check that not st = st { //-- Allocate a new array if necessary if (myCapacity != original.myCapacity) { delete[] myArray; // destroy previous array myCapacity = original.myCapacity; // copy myCapacity myArray = new string[myCapacity]; if (myArray == 0) // check if memory available { cerr << "*** Inadequate memory *** "; exit(1); } } //--- Copy original's array into this myArray for (int pos = 0; pos < myCapacity; pos++) myArray[pos] = original.myArray[pos]; myTop = original.myTop; // copy myTop member } return *this; } //--- Definition of empty() bool Stack::empty() const { return (myTop == -1); } //--- Definition of push() void Stack::push(char & value) { if (myTop < myCapacity - 1) //Preserve stack invariant { ++myTop; myArray[myTop] = value; } else { cerr << "*** Stack full -- can't add new value *** " "Must increase value of STACK_CAPACITY in Stack.h "; exit(1); } } //--- Definition of display() void Stack::display(ostream & out) const { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; } //--- Definition of top() char Stack::top() const { if (!empty()) return (myTop); else { cerr << "*** Stack is empty " " -- returning garbage value *** "; return *(new char); } } //--- Definition of pop() void Stack::pop() { if (!empty()) myTop--; else cerr << "*** Stack is empty -- " "can't remove a value *** "; } The compiler can compile my code without errors, but if i run it, it will crash and stop working, i hope the code can 1. Read the input until the beginning of a tag is detected. (i.e. tags begin with <. if the next character is a / (slash), then it is an end-tag; otherwise it is a start-tag). 2. Read the tag's identity. (e.g. both tags 3. If the tag was a start-tag, push it onto the Stack. 4. Otherwise, it is an end-tag. In this case, pop the Stack (which contains a previously pushed start-tag identity) and verify that the popped identity is the same as the end-tag just processed. Note: if the stack cannot be popped (because it is empty), the input is invalid; the algorithm should STOP. 5. If the identities do not match, the XML expression is invalid. STOP. 6. If they do match, then no error has been detected (yet!). 7. If there is still input that has not yet been processed, go back to the first step. 8. Otherwise (no more input) then the input is valid unless the Stack is not empty. Indicate whether the input is valid (Stack empty) or invalid and STOP. If it is valid, it prints to stdout the message "Valid" and exits with an exit code of 0 (zero); otherwise, it prints "NOT Valid" and exits with an exit code of 1 (one). In addition to validating the input, your program should also keep track of the number of times each start-tag is used. As before, the program should print "Valid" or "NOT Valid". In addition, if the input was valid, it should then print a table with a line for each start-tag encountered and a count of how often it occurred. For example, given the input: The output should be: Valid a 2 b 1 x 1 y 1
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