Click here to Skip to main content
15,890,670 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Draw two monitors syncron Pin
_Flaviu13-Apr-14 22:23
_Flaviu13-Apr-14 22:23 
Question[MFC Desktop Application for enumerating Virtual customized folder and files] Windows 8 Address Bar icon problem Pin
Nirmal K P10-Apr-14 19:39
Nirmal K P10-Apr-14 19:39 
QuestionUse PlaySound api caused memory loading? Pin
cedricvictor10-Apr-14 16:02
cedricvictor10-Apr-14 16:02 
AnswerRe: Use PlaySound api caused memory loading? Pin
Richard Andrew x6410-Apr-14 17:19
professionalRichard Andrew x6410-Apr-14 17:19 
QuestionMS Word Automation - I get so far - now what? Pin
Bryan Anslow10-Apr-14 9:43
Bryan Anslow10-Apr-14 9:43 
AnswerRe: MS Word Automation - I get so far - now what? Pin
Richard MacCutchan10-Apr-14 22:39
mveRichard MacCutchan10-Apr-14 22:39 
SuggestionRe: MS Word Automation - I get so far - now what? Pin
David Crow11-Apr-14 4:40
David Crow11-Apr-14 4:40 
QuestionList And iterator Pin
Hamza Bin Amin10-Apr-14 2:49
Hamza Bin Amin10-Apr-14 2:49 
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:

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?

Here's the code:

<pre lang="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;
 
}

AnswerRe: List And iterator Pin
Maximilien10-Apr-14 10:21
Maximilien10-Apr-14 10:21 
SuggestionRe: List And iterator Pin
David Crow10-Apr-14 15:49
David Crow10-Apr-14 15:49 
Questionavoiding or skipping Divide by zero showing junk values Pin
manoharbalu10-Apr-14 0:50
manoharbalu10-Apr-14 0:50 
AnswerRe: avoiding or skipping Divide by zero crashes Pin
Heng Xiangzhong10-Apr-14 1:00
Heng Xiangzhong10-Apr-14 1:00 
AnswerRe: avoiding or skipping Divide by zero crashes Pin
Heng Xiangzhong10-Apr-14 1:01
Heng Xiangzhong10-Apr-14 1:01 
GeneralRe: avoiding or skipping Divide by zero crashes Pin
manoharbalu10-Apr-14 1:51
manoharbalu10-Apr-14 1:51 
GeneralRe: e: avoiding or skipping Divide by zero crashes Pin
Richard MacCutchan10-Apr-14 2:22
mveRichard MacCutchan10-Apr-14 2:22 
AnswerRe: avoiding or skipping Divide by zero showing junk values Pin
Munchies_Matt10-Apr-14 5:53
Munchies_Matt10-Apr-14 5:53 
AnswerRe: avoiding or skipping Divide by zero showing junk values Pin
User 5924111-Apr-14 19:07
User 5924111-Apr-14 19:07 
QuestionCDHtmlDialog: Changing HTML File Dynamically Pin
Don Guy9-Apr-14 11:31
Don Guy9-Apr-14 11:31 
QuestionSetWindowText() style question Pin
econy9-Apr-14 4:24
econy9-Apr-14 4:24 
AnswerRe: SetWindowText() style question Pin
jeron19-Apr-14 5:06
jeron19-Apr-14 5:06 
AnswerRe: SetWindowText() style question Pin
Maximilien9-Apr-14 8:30
Maximilien9-Apr-14 8:30 
GeneralRe: SetWindowText() style question Pin
econy9-Apr-14 10:11
econy9-Apr-14 10:11 
GeneralRe: SetWindowText() style question Pin
Richard MacCutchan9-Apr-14 23:21
mveRichard MacCutchan9-Apr-14 23:21 
AnswerRe: SetWindowText() style question Pin
leon de boer22-Apr-14 22:16
leon de boer22-Apr-14 22:16 
Questionhow to Convert MFC based Windows application to web application Pin
manoharbalu8-Apr-14 23:53
manoharbalu8-Apr-14 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.