Need Help

Implementing code in classes

Page 1 of 1

0 Replies - 407 Views - Last Post: 30 October 2009 - 07:06 PM Rate Topic: -----

#1 Anum22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-September 09

Need Help

Post icon  Posted 30 October 2009 - 07:06 PM

My professor wants us to take this code :
/********************************************
* Anum Nasirullah						   *
* Homework 3								*
* Date: October 22, 2009					*
* Discription: Recursive Tree Traversal	 *
********************************************/
#include<iostream>					   // use iostream
using namespace std;					 // general implimentation method

struct traversalnode					 // declare the  structure node			   
{

	traversalnode *left;				 // declare the left node
	char info;						   // declare a character info					 
	traversalnode *right;				// declare the right node

};

traversalnode *treeData = NULL;			  // declare treeData as NULL
traversalnode *root = new traversalnode;	 // declare root as a new node
traversalnode *ptr = new traversalnode;	  // declare pointer and a new node

traversalnode PreOrder_Store(struct traversalnode *ptr) // Declare the PreOrder Store
{
	char answer = ' ';						   // declare a char answer
	cout << "Enter the node you are currently at " << endl;  // Ask user to enter a root
	cin >> ptr->info;							// showing what the user enters
	cout << endl;								// end line

if(treeData == NULL)							 // if statement
{
	treeData = ptr;							  // declares what happens if treeData equals NULL
}

cout << "Does the node " << ptr->info<< " have a left node? (Y or N)" << endl;  // asking the user a question
cin >> answer;														 // the user's answer
if(answer == 'Y')													  // an if statement
{
	traversalnode *left = new traversalnode;							   // what occurs if the answer is Y
	ptr->left = left;
	PreOrder_Store(left);
}

else																// else statement

{
	ptr->left = NULL;											  // condition of else statement
}

cout<< "Does " << ptr->info << " have a right node? (Y or N)" << endl; // asking th user if there is a right node
cin >> answer;													  // the users answer													   
if(answer == 'Y')												   //  the if statement
{
	traversalnode *right = new traversalnode;					   // delcares what is suppose to happen if the answer is Y
	ptr->right = right;
	PreOrder_Store(right);
}

else															  // else statement
{
	ptr->right = NULL;											// condition of else statement
}

return *treeData;												 // says to return treeData

}

void PreOrder(struct traversalnode *ptr)						 // declare the preorder traversal
{
	if(ptr != NULL)											  // if statement
{

		cout << ptr->info<< " ";						 // conditions to follow
		PreOrder(ptr->left);
		PreOrder(ptr->right);
	}
}

void InOrder(struct traversalnode *ptr)							 // Declare inorder traversal

{
	if(ptr != NULL)										   // if statement
{

		InOrder(ptr->left);								  // Conditions to follow
		cout << ptr->info<< " ";
		InOrder(ptr->right);
	}
}

void PostOrder(struct traversalnode *ptr)					// Declare the Postorder traversal
{
	if(ptr != NULL)									 // If statement
{

		PostOrder(ptr->left);						  // conditions to follow
		PostOrder(ptr->right);
		cout << ptr->info<< " ";
	}
}

int main()											// the main function

{

	PreOrder_Store(root);							// apply the preorder store
	cout << "PreOrder Traversal" << endl;			// print out the title for the preorder traversal
	PreOrder(treeData);							  // print out the actual traversal
	cout << endl;									// endline
	cout<< "InOrder Traversal" << endl;			  // print out the title for the inorder traversal
	InOrder(treeData);							   // print out the actual traversal
	cout << endl;									// endline
	cout<< "PostOrder Traversal" << endl;			// Print out the title for the postorder traversal
	PostOrder(treeData);							 // print out the actual traversal
	cout << endl;									// endline

	system("pause");								 // system pause
	return 0;										// return zero

}





and implement it in classes using Itemtype, and he said that there should be 5 files, but did not tell us anything about doing it, Here are 5 files I created in linear data structures class last year which are in the format , I think the professor wants but I dont know how t0 manipulate the code above to fit these files. Can someone help me in figuring out which parts of the code need to go into each of these files and how I need to implement it?

File 1

#include <string>
#include "person.h"
using namespace std;

const int MAXLEN = 100;
typedef Person ITEMTYPE;

class List{

	public:
		List();
		~List();

		ITEMTYPE retrieve (int pos) const;
		void insert (const ITEMTYPE input, int pos);
		void append (ITEMTYPE input);
		bool remove (int pos);
		int length () const;
		bool isEmpty ();
		void makeEmpty();
		void printlist();
		bool search(ITEMTYPE input);
		int itempos(ITEMTYPE input);

	private:
		ITEMTYPE item[MAXLEN];
		int len;
};




File 2

#include <string>
using namespace std;

class Person {
	private:
		string lastName;
		string firstName;
		float	gpa;
	public:
		Person() {};						// default constructor
		Person(string l, string f, float g);// constructor
		Person(const Person& p);			// copy constructor
		~Person() {};						// destructor

		//set and get functions
		void setLastName(string l);
		void setFirstName(string f);
		void setGPA(float g);
		string getLastName();
		string getFirstName();
		float getGPA();

		// overloading operators
		const Person & operator=(const Person & p); // assignment operator
		bool operator==(const Person &p);			// equal operator
		friend ostream & operator << (ostream &out, const Person &p);
													// << operator

};



File 3

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

Person::Person(string l, string f, float g)
{
	lastName = l;
	firstName = f;
	gpa = g;
}

Person::Person(const Person& p)
{
	lastName = p.lastName;
	firstName = p.firstName;
	gpa = p.gpa;
}

		
void Person::setLastName(string l)
{
	lastName = l;
}

void Person::setFirstName(string f)
{
	firstName = f;
}

void Person::setGPA(float g)
{
	gpa = g;
}

string Person::getLastName()
{
	return lastName;
}

string Person::getFirstName()
{
	return firstName;
}

float Person::getGPA()
{
	return gpa;
}


// assignment operator
const Person & Person::operator=(const Person & p) 
{
	if (this != &p) {
		lastName = p.lastName;
		firstName = p.firstName;
		gpa = p.gpa;		
	}

	return *this;
}


bool Person::operator==(const Person &p)			 // equal operator
{
	if (lastName == p.lastName &&
		firstName == p.firstName &&
		gpa < p.gpa + 0.001 && 
		gpa > p.gpa - 0.001) {

		return true;

	} else {

		return false;

	}
}


ostream & operator << (ostream &out, const Person &p)
{													// << operator
	out << p.lastName << "\t";
	out << p.firstName << "\t";
	out << p.gpa << "\n";

	return out;
}



File 4
#include <iostream>
#include "listArray.h"
#include <string>
using namespace std;


List::List(){

	cout <<"List constructor called - Creating empty list" << endl;
	len = 0;

}

List::~List(){

	cout<<"List destructor called"<<endl;
	len = 0;

}


ITEMTYPE List::retrieve (int pos) const {

	if (pos < 0 || pos >= len) { // check the boundary

		cout << "Position should be between 1 and the length of the list\n";
		exit(1);

	} 

	return item[pos];

}	


void List::insert (const ITEMTYPE input, int pos){

	int curIndex = len;
	
	if (len == MAXLEN) {				// case 0: check the capacity
		cout << "Cannot add one more item the list\n";
		exit(1);
	} else if (pos < 0 || pos > len) {	// case 1: out of the boundary

		cout << "Position should be between 0 and the length of the list\n";
		exit(1);

	} else if (pos == len){				// case 2: insert in the last

		item[curIndex] = input;
		len++;

	} else {

		do{							// case 3: genereal scenario
			item[curIndex]=item[curIndex-1];
			curIndex--;	  
		} while(curIndex > pos);

		item[curIndex] = input;
		len++;
	}
}


void List::append (ITEMTYPE input){

	insert(input, len);
}


bool List::remove (int pos){
	
	bool removed = false;
	int index = pos;
	int indexOfLast = len-1;
	
	if (len == 0) {				// case 0: check the size = 0
		cout << "Cannot remove any item in the list\n";
		exit(1);
	} else if (pos < 0 || pos > indexOfLast) { // case 1: out of the boundary

		cout << "Position should be between 0 and the length-1\n";
		exit(1);

	} else if (index == indexOfLast){	// case 2: the last position to be removed

		removed=true;
		len--;

	} else {

		do{								// case 3: general case
			item[index]=item[index+1];
			index++;

		} while(index < indexOfLast);

		removed=true;
		len--;

	}
	
	return removed;
}


int List::length () const{
	
	return len;

}


bool List::isEmpty (){
	
	if(len==0) {

		return true;

	}

	return false;
}


void List::makeEmpty(){

	len = 0;

}


void List::printlist(){

	if (isEmpty()) {

		cout<<"\nThe List is empty\n\n\n";

	} else {

		cout<<"\nThe Available Items in the List (" << len << "):\n";
		for (int j = 0; j < len; j++){
			cout << item[j];
		}
	}
}


bool List::search(ITEMTYPE input){

	for(int j = 0; j < len; j++){

		if ( item[j] == input) {
			return true;
		}
	}

	return false;
}


int List::itempos(ITEMTYPE input){

	for(int j = 0; j <= len; j++){

		if ( item[j] == input) {
			return j + 1;
		}
	}

	return -1;
}





File 5


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

using namespace std;

int main(int argc, char *argv[]) {

	string	lastName;		// last name with string type
	string	firstName;		// first name with string type
	float	gpa;			// gpa

	int		number;			// number
	Person	item;
	List	ListArray;


	ifstream myfile (argv[1]);

	if (!myfile.is_open()) {

		cout << "Unable to open file\n"; 

	} else {
		
		// read the first line
		myfile >> number;
	
		// read records
		while (! myfile.eof() ) {

			myfile >> lastName;
			myfile >> firstName;
			myfile >> gpa;

			item.setLastName(lastName);
			item.setFirstName(firstName);
			item.setGPA(gpa);

			ListArray.insert(item, ListArray.length());
			//ListArray.insert(item, 0);

		}
		myfile.close();

		// print the whole list
		ListArray.printlist();
		
		// keep removing the first item until the list is empty
		int times = ListArray.length();
		for (int i = 0; i < times; i++) {
			ListArray.remove(ListArray.length()-1);
			//ListArray.remove(0);
			ListArray.printlist();
		}
	}

	system("pause");
	return 0;
}



I really Hope you can help me, my whole class is doing terrible because the professor doesn't know anything about code he just explains the logic and we all understand that really well but he gives us projects on code and expects us to know it

Is This A Good Question/Topic? 0
  • +

Page 1 of 1