Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in C++ Web page coding in HTML includes pairs of what are called \"tags\":

ID: 3863930 • Letter: C

Question

Code in C++

Web page coding in HTML includes pairs of what are called "tags": and , and , and . Tags are surrounded by pointed brackets () and tags that occur in pairs have a forward slash just inside the left bracket of the closing tag. The last tag started must be the first one closed. Using a stack, write a program which takes HTML input and determines whether the tags are properly matched. Note that there are some tags that do NOT have a match - e.g. , , do not ha matching ending tag. Also, count the number of times each non-ending tag appears in the code - e.g. count but do not count . 1.) Read in the tags that do not have matches from a file (assume only the three listed above) and store in a linked list 2.) Read in the html from a file and print each line to the screen AND an output file 3.) Find tags and process (can have more than one in a line) 3.1) create alphabetical list of opening and no-match tags found in file 3.1.1) tag not in list - add to list with count of 1 3.1.2) tag in list - add to count 3.2) if opening tag - add to top of verify tag stack 3.3) if closing lag - check for a "matching" opening tag on top of verify tag stack 3.3.1) if match to tag on top of the slack - pop the verify tag stack 3.3.2) if not a match to tag on top of the stack - error - print an error message(s) to the screen AND an output file and continues processing 4.) After html file processed print out to the screen AND an output file an the alphabetical list of tags indicating "match" or "no match" tag type and the count of the number of times each occurred.

Explanation / Answer

Below is the code to read from file the tags that do not have matches and save in the linked list and to display the list at the end of file.

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

bool Tokenize_tag(char* , char* );
struct node{
   char data[10];
   node *next;
}*head;
void saveToList(node* ,node* );
void displayList(node *);

int main() {
   ifstream inFile;
   inFile.open("file.html");
   std::string buffer;
   bool ret = false;
   while (getline(inFile, buffer))
   {
       inFile >> buffer;
       if(Tokenize_tag("<P>",(char*)buffer.c_str()))
       {
           node *node1;
           node1 = new node;
           strcpy(node1->data,"<P>");
           node1->next=NULL;
           saveToList(head,node1);
       }
       if(Tokenize_tag("<BR>",(char*)buffer.c_str()))
       {
           node *node2;
           node2 = new node;
           strcpy(node2->data,"<BR>");
           node2->next=NULL;
           saveToList(head,node2);
       }
       if(Tokenize_tag("<IMG",(char*)buffer.c_str()))
       {
           node *node3;
           node3 = new node;
           strcpy(node3->data,"<IMG>");
           node3->next=NULL;
           saveToList(head,node3);
       }
   }
   displayList(head);  
   return 0;
}

bool Tokenize_tag(char* tag, char* buff)
{
   char* stemp = strstr(buff, tag);
   if(stemp != NULL)
   {
       return true;
   }
   else
   {
       return false;
   }
}

void saveToList(node* head, node *new1)
{
   struct node *temp;
temp=head;
   if (head == NULL)
       head = new1;
   else
   {
       while(temp!=NULL)
           temp = temp->next;
       temp->next=new1;
   }
}

void displayList(node *root)
{
   if (head == NULL)
       cout<<"no tags that do not have matches";
   else
   {
       struct node *temp;
       temp=head;
       while (temp!=NULL)
       {
           cout<<temp->data<<" ";
           temp=temp->next;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote