reading from file and adding to the stack

may not be the prettiest way of doing it...

Page 1 of 1

10 Replies - 1306 Views - Last Post: 29 March 2009 - 02:42 PM Rate Topic: -----

#1 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

reading from file and adding to the stack

Posted 29 March 2009 - 12:11 PM

Me again, Margaret....

My assignment is to read an html file and balance the html tags. My code is working, except when there is a tag embedded in another tag..like such:

<center> asdf <b> asdf </b> </center>

here's how I'm reading it (i warn you it may be ugly...but it works....well, kinda... :))
while (!in.eof()) {
	 in.get(ch);
	   if(ch == '<') {
		 in.get(ch);
		 if (ch == '!')
		   comment += 1;
		 else if (ch == '/') {
				while (ch != '>') {
					 in.get(ch);
					 s2 += ch;
				}
		compare(head, s2, line, paired);
		 }
		 else  {	
			 s1 += ch;
			 while (ch != '>') {
			  in.get(ch);
			  s1 += ch;
			 }
			push(head, s1);
		 }
	   }
	   if (ch == '\n')
		 line += 1;


Here's my push function to add to the stack...this may be the problem as well...?
void push(TagNode* & top, string& open) 
{
   TagNode *current;
   
   if (top == NULL) {
	 current = new TagNode;
	 current->setData(open);   // class method
	 current->setLink(NULL);	// class method
	 top = current;
   }
   else {
	 current = new TagNode;
	 current->setData(open);  // Part of my class
	 current->setLink(top);	 // Class method
	 top = current;
   }	 

   open.clear();
}


I'm guessing it's the problem in adding another to the stack....?

Here's my .h file for my class, just so you know what's going on with that....
#include<string>

using namespace std;

class TagNode
{
public:
   TagNode();  // Constructor
   TagNode* getptr() const { return next; };  // Get next pointer
   string getdata() const;
   void setData(string theTag);
   void setLink(TagNode* ptr);
	  
private:
   string tag;
   TagNode* next;
};


Thanks a million, you guys! You help me out so much...
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: reading from file and adding to the stack

#2 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 12:44 PM

As part of this assignment, do you have to implement your own stack?
Was This Post Helpful? 0
  • +
  • -

#3 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 12:53 PM

Yes. Evidently, I'm having trouble adding another to the stack....

View Postbsaunders, on 29 Mar, 2009 - 11:44 AM, said:

As part of this assignment, do you have to implement your own stack?

Was This Post Helpful? 0
  • +
  • -

#4 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 12:55 PM

Are you sure the stack isn't working?
Was This Post Helpful? 0
  • +
  • -

#5 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 01:10 PM

I've assumed that...but I may be wrong....
Could it be something with my closing tag embedded in there?


View Postbsaunders, on 29 Mar, 2009 - 11:55 AM, said:

Are you sure the stack isn't working?

Was This Post Helpful? 0
  • +
  • -

#6 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 01:16 PM

What happens when you have a tag nested inside another?
Was This Post Helpful? 0
  • +
  • -

#7 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 01:19 PM

You're right....it's making it to the stack...but it's not popping it from the stack....

Here's my pop function...if I've got two on the stack...am I not popping it correctly?

string pop(TagNode* ptr, int linenum)
{

   TagNode *current;
   string tagToCompare;

   if (ptr == NULL)
	 cerr << "Empty list on line number " << linenum << endl;

   else {
	 current = ptr->getptr();
	 tagToCompare = ptr->getdata();
	 ptr = current;
	 delete current;
   }
   return tagToCompare;
}


Was This Post Helpful? 0
  • +
  • -

#8 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 01:30 PM

Where is pop called in your program?
Was This Post Helpful? 0
  • +
  • -

#9 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 01:46 PM

Pop is called in the compare function. I'll include my entire driver program. Thanks for your help.

#include <iostream>
#include <string>
#include <fstream>
#include "stack.h"

using namespace std;

void push(TagNode* & top, string& open, int& unpair);
// Precondition: Adds opening tag to the stack
// Postcondition: Once the opening tag has been added to the stack, the string is cleared and
//				ready to load another to push.


void compare(TagNode* ptr, string& close, int linenum, int& pair);
// Precondition: Program has found a closing tag to compare.
// Postcondition: The function calls the pop function to return the opening tag for comparison.
//				If the tags match, paired tag is incremented by one; if the tags do not match,
//				the program outputs an error and quits. 

string pop(TagNode* ptr, int linenum);
// Precondition: The compare function calls the pop function to return an opening tag for comparison.
// Postcondition: The stack has lost one node and reroutes pointers accordingly.



int main()
{
ifstream in;   // input file stream acting as conduit to html file
string s1;	 // stores opening tags
string s2;	 // stores closing tags
char ch;	   /// Used to read character by character to find tags in input file
int comment = 0;  // Counts Comment tags which do not have pairs
int line = 1;   // Counts '\n' characters to cite errors if necessary
int paired = 0;  // Counts paired tags that have been located and matched.
int unpaired = 0;  // Counts unpaired tags that do not have a match.
TagNode *head = NULL;  // Head pointer that points to the top of the stack.


   in.open("some_HTML_file.html");

   if (in.fail()) {
	 cout << "Bad filename. Exiting now." << endl;
	 exit(0);
   }

   else
	 cout << "HTML Tag Checker. Checking some_HTML_file.html ..." << endl;

//		while (in.get(ch)) {
//	 if (ch >= 'a' && ch <= 'z')
//	   ch -= 32;
//		 out << ch;
//	  }

   while (!in.eof()) {
	 in.get(ch);
	   if(ch == '<') {  // Has located beginning of a tag
		 in.get(ch);
		 if (ch == '!') // if it is a comment tag, it stops and begins looking for another tag
		   comment += 1;
		 else if (ch == '/') {  // Locates closing tags
				while (ch != '>') {
					 in.get(ch);
					 s2 += ch;
				}
		compare(head, s2, line, paired);  // Function call to compare tags after a closing tag has been found
		 }
		 else  {	
			 s1 += ch;		   // Begins reading in opening tag
			 while (ch != '>') {
			  in.get(ch);
			  s1 += ch;
			 }
			 push(head, s1, unpaired);   // Pushes opening tag when one has been found
		 }
	   }
	   if (ch == '\n')	///Counts '\n' characters
		 line += 1;
   }
   // output at the end of the program
   cout << paired << " Paired HTML Tags found." << endl;
   cout << unpaired << " unpaired tags." << endl;
   cout << "All paired tags correct." << endl;

return 0;
}



void push(TagNode* & top, string& open, int& unpair) 
{
   TagNode *current;
   string unPair1 = "li>";  // Unpaired tag that is allowed
   string unPair2 = "br>";  // Unpaired tag that is allowed

   if (open == unPair1 || open == unPair2) // Checks to see if tag found is permitted unpaired tag 
	 unpair += 1;
   
   else if (top == NULL) {  // If head equals null, knows that it is first node on stack
	 current = new TagNode;
	 current->setData(open);
	 current->setLink(NULL);
	 top = current;
   }
   else {
	 current = new TagNode;  // If there is another node on the stack, it acts accordingly
	 current->setData(open); // for pointers to point to the right nodes or NULL.
	 current->setLink(top);
	 top = current;
   }	 

   open.clear();  // Clears string in preparation to load another
}



void compare(TagNode* ptr, string& close, int linenum, int& pair)
{
   string popped;	 // Holds open tag popped from the list for comparison
   int line = linenum;

   popped = pop(ptr, line);  // Function call to pop to load open tag

   cout << popped << endl;  //I've used cout here only to see what my problem is...In doing this, it looks as if it 
   cout << close << endl;	 // hasn't popped.
// For instance if I have <center> <b> </b> </center>
// This would cout: center, b, /b, b, /center...as if the opening b didn't pop from the stack. 

   if (popped.compare(close) != 0) {  // If tags do not match, cites error line number and exits.
	 cerr << "ERROR! Unclosed <" << popped << "tag on line number " << linenum << endl;
	 exit(0);
   }
   else if (popped.compare(close) == 0)  // If tags match, increments paired tag count.
	 pair += 1;

   close.clear(); // clears string in preparation to load another closing tag
}



string pop(TagNode* ptr, int linenum)
{
   TagNode *current;
   string tagToCompare;

   if (ptr == NULL) // If empty list, this means there's no opening tag to pop. 
	 cerr <<"Empty list. Unpaired tag on line number" << linenum << endl; // Cites error line and exits
   else {
	 current = ptr->getptr();		// Reroutes pointers 
	 tagToCompare = ptr->getdata();
	 ptr = current;
	 delete current;
   }
   return tagToCompare;  // Returns opening tag for comparison
}

Was This Post Helpful? 0
  • +
  • -

#10 bsaunders   User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 02:13 PM

Can you post the code for TagNode's getdata , setData , and setLink member functions?
Was This Post Helpful? 0
  • +
  • -

#11 luckyman76   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: reading from file and adding to the stack

Posted 29 March 2009 - 02:42 PM

Sure....here's the definitions from the implementation file:

 
string TagNode::getdata()
{
  return tag;  // Get tag from stack to compare with closing tag.  
}


void TagNode::setData(string theTag)
{
  tag = theTag;  // Add tag to a stack
}


void TagNode::setLink(TagNode* ptr)
{
  next = ptr;	// Set Pointer in the stack
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1