EECE 352: PS3

Due: Tuesday, 14 September 1999

Problem 1(50%): For the first part of this PS you will implement a simple function called isBalanced(string & s). This function is given a string s as input and return true if the string has balanced parentheses, and returns false otherwise. You must use a stack to keep track of the parenthesis. Note that the expression can contain other characters that are not parenthesis.

Problem 2(50%): For the second part you will implement a URLContainer class with several member functions. The header file for your class is enclosed below. Please read the comments next to each function that tell you what the function does.
Notice that you will need to use, and maybe expand upon, the URL class you built for PS2. You can either use the URL class you wrote or you can use mine (once I post the solutions to PS2). Also note that all the functions you will write are really just one or two lines long. All you need to do is call the appropiate STL functions.

The header file is:

// URLContainer.h: interface for the URLContainer class.
#include "URL.h"
using namespace std;

//#include "URL.h"

class URLContainer  
	//You will be using a vector.
	vector<URL> urls;

	//Takes as input is which refers to an RDF file and reads all the
	// URLs in that file, adding them to the container. (see the main.cpp
	// from PS2 for help).
	URLContainer(istream & is);
	virtual ~URLContainer();

	//Returns the number of elements in this container.
	int size();

	//Prints the Nth element
	void printNth(ostream & os, const int n);
	//Prints the first n URLs (given the current ordering) to os, one in each line.
	void printFirstN(ostream & os, const int n);

	//Sort the URLs based on the actual URLs (alphabetically).
	//NOTE: you might need to make the data members of URL public in order
	// to implement both of the sort functions. You might also want to 
	// implement operator< for the URL class...its up to you.
	void sortByURL();

	//Sort the objects alphabetically by title.
	void sortByTitle();

The main.cpp file you will be using is:

#include "URLContainer.h"

using namespace std;

bool isBalanced(string p){
  //define this
int main(int argc, char* argv[])

	//This is the first part of the PS.
	//You must implement isBalanced(string s) so that it checks
	// for balanced parenthesis.
	string line;
	getline(cin, line); //you will neet to press Enter twice.
	if (isBalanced(line)) //you must implement this function
		cout << "Its a Balanced expression" << endl;
		cout << "It is NOT Balanced" << endl;

	//This is the second part 
	ifstream opendir("//Engr_asu/ECE352/computer_science.txt");
	if (opendir == 0) {
		cout << "Could not open input file" << endl;
	URLContainer uc(opendir); //read the whole file into uc

	//print out the number of URLs we read.
	cout << "There were " << uc.size() << " URLs"<< endl; 
	uc.printFirstN(cout, 3); //print the first three.

	uc.printNth(cout, 11); //print the 11th item

	uc.sortByURL(); //sort alphabetically by URL
	cout << "===First three, sorted by URL" << endl;
	uc.printFirstN(cout, 3); //checking to make sure they are OK

	cout << "===First three, sorted by title" << endl;
	uc.printFirstN(cout, 3);

	return 0;

Jose M. Vidal
Last modified: Thu Sep 9 08:48:22 EDT 1999