无法通过我自己的迭代器操作数据 [英] Trouble Manipulating Data through My Own Iterator

查看:67
本文介绍了无法通过我自己的迭代器操作数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们被赋予了使用列表和迭代器的任务。我们应该从头开始制作它们。我已经完成了。我遇到的问题如下:









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:




#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.我无法访问由当前数据类型组成的列表在每个学生实例中。这是否意味着我需要为学生班级内的课程列表制作一个迭代器?



2.同样,因为我没有直接访问课程列表所以我通过学生对象将课程添加到学生列表中,而不是通过迭代器。我怎样才能通过迭代器完成?



3.由于我为学生制作的迭代器仅打印出学生,因此不打印特定学生及其课程不是他们的courselist中的课程。怎么做?



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?

推荐答案

这篇关于无法通过我自己的迭代器操作数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆