Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
We were given a task to use lists and iterators. We were supposed to make them from scratch. I'm done with that. The problems that I'm having are as following:




C++
#include <iostream>
#include <string>
using namespace std;

const int ILLEGAL_SIZE = 1;
const int OUT_OF_MEMORY = 2;
const int NO_SPACE = 3;
const int ILLEGAL_INDEX = 4;
const int ILLEGAL_REFERNCE = 5;
const int COURSE_LIMIT_REACHED = 6;

template <class T>
class ListIterator;

template <class T>
class OrderedList {
	
	
	friend class ListIterator<T>;

private:
	
	int maxSize;
	int currentSize;
	T *listArray;
	bool removeAt(int index) {  
		
		if(index < 0 || index >= currentSize) throw ILLEGAL_INDEX;
	
		else {
		
			for(int i=index;i<currentSize;i++) 
			
				listArray[i] = listArray[i+1];
			
			currentSize--;
			return true;
		}					
	}

public:

	OrderedList(int size);
	OrderedList(const OrderedList& copy);
	~OrderedList();
	void setData(T s);
	T getData(int i);
	int getCurrent() { return currentSize; }
	bool isFull();
	bool isEmpty();
	int isPresent(T value);
	void insert(T value);
	bool remove(T value);
	int search(T value);
	void printData();
	const OrderedList& operator = (const OrderedList& assignment);

};

template <class T>
OrderedList<T>::OrderedList(int size) {

	if(size < 0) throw ILLEGAL_SIZE;

	maxSize = size;

	listArray = new T[maxSize];
	
	if(listArray == NULL) throw OUT_OF_MEMORY;
	
	currentSize = 0;

}

template <class T>
OrderedList<T>::OrderedList(const OrderedList& copy) {

	maxSize = copy.maxSize;
	currentSize = copy.currentSize;

	listArray = new T[maxSize];
	for(int i=0;i<currentSize;i++) {
	
		listArray[i] = copy.listArray[i];
	
	}
}

template <class T>
const OrderedList<T>& OrderedList<T>::operator= (const OrderedList& assignment) {

	if(this != &assignment) {
	
		currentSize = assignment.currentSize;
		delete [] listArray;
		listArray = new T[maxSize];
		
		for(int i=0;i<currentSize;i++) {
		
			listArray[i] = assignment.listArray[i];
		
		}
	}
	
	return *this;
}

template <class T>
OrderedList<T>::~OrderedList() {

	delete [] listArray;

}

template <class T>
void OrderedList<T>::setData(T s) {

		
		listArray[currentSize] = s ;
		
		currentSize++; 
}

template <class T>
T OrderedList<T>::getData(int i) {

	return listArray[i];		
}

template <class T>
bool OrderedList<T>::isEmpty() {

	return (currentSize == 0);

}

template <class T>
bool OrderedList<T>::isFull() {

	return (currentSize == maxSize);

}

template <class T>
int OrderedList<T>::isPresent(T value) {

	return(search(value));

}

template <class T>
int OrderedList<T>::search(T value) {

int low = 0;
int mid;
int high = currentSize-1;

	while(low<=high) {

		mid = (low + high)/2;

		if(value == listArray[mid])
			
			return mid;
		
		else if(value > listArray[mid])

			low = mid + 1;

		else
			high = mid - 1;
	}	
	return -1;
}

template <class T>
void OrderedList<T>::insert(T value) {


	if(isFull()) throw NO_SPACE;

	else {
	int i = currentSize;
	while(  i > 0 && value < listArray[i-1]) {
	
		listArray[i] = listArray[i-1];
		i--;
	}
	
	listArray[i] = value;
	currentSize++;
	}

}


template <class T>
bool OrderedList<T>::remove(T value) {

	int index = search(value);
	
	if(index == -1) return false;

	else {
	
		return removeAt(index);
	}
}

template <class T>
void OrderedList<T>::printData() {


	for(int i=0;i<currentSize;i++) {
	
		cout << listArray[i] << endl;
	
	}
}


class Course {

private:
	
	string ID;
	double GPA;

public:

	Course(): ID(""),GPA(0) {}
	Course(string id,double gpa): ID(id),GPA(gpa) {}
	double getGPA() { return GPA; }
	void printCourse() { cout << "Course ID: " << ID << " " << "GPA: " << GPA << endl;  }
	friend ostream& operator<< (ostream & out, const Course& course) { out << course.ID << " " << course.GPA << endl; return out; }
	bool operator< (Course c) { if (ID < c.ID) return true; else return false; }
	bool operator> (Course c) { if (ID > c.ID) return true; else return false; }
	bool operator== (Course c) { if (ID == c.ID) return true; else return false; }
};


class Student {
	
private:	
	
	string name;
	int rNo;
	int noOfCourses;
	double CGPA;
	OrderedList<Course> courseList;
	
	
public:

	Student(): name(),rNo(0),CGPA(0),noOfCourses(0),courseList(50) {}
	Student(string n,int r): name(n),rNo(r),CGPA(0),noOfCourses(0),courseList(50) {}
	void printStudent() { cout << "Name : " << name << " " << "Roll No: " << rNo << " " << "CGPA: " << CGPA << endl; if(noOfCourses !=0)  printCourse(); }
	
	int getRoll() { return rNo; }

	void addCourse(Course c) {

		if(noOfCourses < 50) {
		courseList.insert(c);
		noOfCourses++;
		}
		else throw COURSE_LIMIT_REACHED; 
	}

	void removeCourse(Course c) {
	
		if(noOfCourses != 0){
		courseList.remove(c);
		noOfCourses--;
		}

		else cout << "There are no Courses to be removed\n" << endl;
	}

	void printCourse() { cout << "Courses Taken\n\n"; courseList.printData(); }
	
	double calCGPA() {
	
		Course c;
		double sum = 0;
		for(int i=0;i<noOfCourses;i++){
		
		c = courseList.getData(i);
		sum = sum + c.getGPA();
		
		}
		CGPA = sum/noOfCourses;
		return CGPA;
	}


	friend ostream& operator<< (ostream& out, const Student& student) { out << student.name << " " << student.rNo << endl; return out; }
	bool operator <(Student s) { if (rNo < s.rNo) return true; else return false; }
	bool operator >(Student s) { if ( rNo > s.rNo) return true; else return false; }
	bool operator ==(Student s) { return (rNo == s.rNo); }

};

template <class T>
class ListIterator {

private:

	OrderedList<T> &list;
	int current;
	ListIterator & operator =(const ListIterator & rhs);

public:

	ListIterator(OrderedList<T> &l): list(l), current(0) { }
	ListIterator(const ListIterator<T> &li): list(li.list), current(li.current) { }
	void begin(){ current = 0; }
	int getCurrent() { return current; }
	void end() { current = list.currentSize; }
	bool isStart() { return current == 0; }
	bool isDone() { return current == list.currentSize; }
	void next() {
		
		if (!isDone()) current++;
		else throw ILLEGAL_REFERNCE;
}
	bool find(const T key) {
	
	current = list.search(key);

	if (current > -1) return true; 
	
	else return false;
}

	T getData() { return (list.listArray[current]);	} 
	void setData(const T value) { list.listArray[current];  } 
	void insertData(T value) { list.insert(value); }
	void removeData(T value) { list.remove(value); }
	void getCourse(Student s) { getCourse(s); }
	void printData() { list.printData(); }

};



int main() {

/************************************
1. Creating An Empty List Of Students
************************************* */
	OrderedList<Student> studentList(10);

	Student s1("Hamza",12105092);
	Student s2("Ahmad",12105081);
	Student s3("Sherlock",12105032);
	Student s4("Watson",12134989);
	Student s5("Gary",12105042);
	Student s6("Ali",3111111);

/*****************************
2. Adding Students To The List
****************************** */
	
	ListIterator<Student> studentIterator(studentList);
	
	studentIterator.insertData(s1);
	studentIterator.insertData(s2);
	studentIterator.insertData(s3);
	studentIterator.insertData(s4);
	studentIterator.insertData(s5);

	studentIterator.printData();
	cout << endl;
	

/**********************************
3. Deleting A Student From The List
*********************************** */
	studentIterator.removeData(s1);

	cout << "List After Removing A Student\n" << endl;
	
	studentIterator.printData();
	
	cout << endl;
	
/*************************************
4. Adding A Course For A Given Student
************************************** */

	Course c1("CS102",3.44);
	Course c2("CS101",3.11);
	Course c3("MATH101",4);

	
	s5.addCourse(c1);
	s5.addCourse(c2);
	s5.addCourse(c3);

	studentIterator.setData(s5);
	
	
/*************************************
5. Remove A Course For A Given Student
************************************** */

	s5.removeCourse(c2);

	cout << "After removing a course\n" << endl;
	s5.printStudent();

/****************************************************************
6. Finding If A Student is Present Or Not Through His Roll Number
***************************************************************** */

	bool store; 
	string name;
	int rollNo;
	cout << "Enter The Student's Name and Roll Number To Find If He's Present In The List\n";
	cin >> name;
	cin >> rollNo;
	Student s7(name,rollNo);
	store = studentIterator.find(s7);
	
	if(!store) cout << "Student is not present in the list\n" << endl;
	else cout << "Student is present\n" << endl;
	
	
/*************************************************
7. Calculating CGPA For A Given Student's Roll No.
************************************************** */

	s5.calCGPA();
	s5.printStudent();

/***********************************************************************************************************************
8. Given The Roll Number, List All The Course A Student Has Taken Along With The Awarded Grades And GPA For Each Course.
************************************************************************************************************************ */
	  
	  int index = 0;
	  cout << endl;
	  cout <<"Enter the Student's name and Roll No. Whose Course Details You Want To Know" << endl;
	  cin >> name;
	  cin >> rollNo;
	  Student s9(name,rollNo);
	 store = studentIterator.find(s9);
	 if(store) 
	 cout << studentIterator.getData();

	 else
		 cout <<"Entered Roll Number Isn't Present In The List" << endl;


/*********************************************************************
9.	List all the students. (complete details – student and his grades)
********************************************************************** */

	studentList.printData();
	

return 0;

}


1. I'm not able to access the list made of Course datatype which is present in each Student instance. Does this mean I need to make an iterator for that course list inside the student class?

2. Similarly since I don't have direct access to The course list so I added the course into the Student list through the student objects not through the iterator. How can I do it through the iterator?

3. Printing of a particular student and his courses is not happening as my iterator made for student only prints out the students, not the courses present in their courselist. How to do that?
Posted
Comments
Legor 11-Apr-14 6:35am    
Code dump .. post only relevant parts of your code. nobody will look through that much of code here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900