|
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() {
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);
ListIterator<Student> studentIterator(studentList);
studentIterator.insertData(s1);
studentIterator.insertData(s2);
studentIterator.insertData(s3);
studentIterator.insertData(s4);
studentIterator.insertData(s5);
studentIterator.printData();
cout << endl;
studentIterator.removeData(s1);
cout << "List After Removing A Student\n" << endl;
studentIterator.printData();
cout << endl;
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);
s5.removeCourse(c2);
cout << "After removing a course\n" << endl;
s5.printStudent();
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;
s5.calCGPA();
s5.printStudent();
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;
studentList.printData();
return 0;
}
|
|
|
|
|
IMO, You should insert into a list, not in to an iterator.
The iterator is (only) used for traversing the list; have a look at what STL does, the container is std::vector and its iterator is std::vector::iterator.
Ok, to answer the questions:
1. Yes, there should be a method (private) inside the Student class to iterator over the course.
2. the printCourse method should iterator over the courseList list.
3. the studentList should not have a "printData"; just iterate over over the Student and call printCourse for each student in the list.
...
YMMV
Good luck.
Max.
I'd rather be phishing!
|
|
|
|
|
Please see #4 here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I have a console application which calculates using formulas based on the inputs it is receiving every cycle in a particular frequency and gives dynamic output values based on its input variables and constants and so on. In short its a real time application.
This application was initially developed in Visual Studio 6.0 as c++ console application. Recently we converted it to Visual Studio 2008.
It was working fine in VS 6.0. But after conversion, some of the values shows -1.#J which seems like infinite values.
When I observed, it seems like certain calculations may result in divide by 0 or complex values like square root of a negative number and these values should show the value as -1.#J.
But I wonder how there was no such values in VS6.0 whereas VS2008 shows these junk values.
Please suggest me some ways of preventing it.
modified 10-Apr-14 7:56am.
|
|
|
|
|
Please show your code.
Everything is the code !
|
|
|
|
|
Please show your code.
Everything is in the code !
|
|
|
|
|
I am posting one of the functions used in the code. There are many functions similar to this in the entire application
int CONDENSER(float Fl,float Fv,float Rc,float Gain,float DesFp,float Htcfl,
float Htcmaxfl,float Tflsh,float Fc, float Cpcw,
float Cppl, float Cppv, float Cpov,float Cpol, float DesFc,
float Tcwi,float Tpfi,float Tc,float Lhpv,float Lhv,
float Condeff,float CondHtarea,float Ambhc,
float Ambhf, float Ambt,float Mhc, float Mhf,
float Tfmax, float Tcmax,float *Liquid, float *Vapor,int IL, int IV,int OV, int OL,int FN,int CN)
{
int J,N,K,TYPE;
float Fp,SYCOMP,SXCOMP,Heatin,Heatvap,Heatliq;
float Heatloss,Hg,Hc,DELT1,DELT2,A,B,DENOM,DELT,Heatout;
float DER,MAX,MIN,FLAG,P,I,PI;
// Amount of Vapor condensed [ L = ( 1 - V/F) * F ]
Fp = Fl + Fv;
*Liquid = Rc * Fp;
// By material balance
*Vapor = Fp - (*Liquid);
// Composition of Vapor in equilibrium with Liquid Y = K * X
SYCOMP = 1.0E-10;
for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++)
{
Esim->SCOMP[J][OV] = Esim->NKVAL[J][FN]*Esim->NXS[J][FN];
SYCOMP = SYCOMP + Esim->SCOMP[J][OV];
}
// Condensation rate (PI Logic)
Esim->NS1DER[FN]= 1 - (SYCOMP);
P = Gain*Esim->NS1DER[FN];
I = Esim->NS1S[FN];
I = LIMIT01(I+Esim->NODEK7[FN]*Esim->NS1DER[FN]);
PI = LIMIT01(P+I);
Esim->NS1S[FN] = LAG(PI,Esim->NS1S[FN],0.05);
Esim->NS1S[FN] = LIMIT01(Esim->NS1S[FN]);
// Condensed Liquid Composition
// FEED * Yi - VAP * Yi - LIQ * Xi = ACC * (d(Xi)/dt)*dt
// d(Xi) = (FEED * Yi - VAP * Yi - LIQ * Xi)/(ACC * dt)
SXCOMP = 1.0E-10;
for(J=Esim->COMPBEG;J<=Esim->COMPEND;J++)
{
Esim->SCOMP[J][OV] = Esim->SCOMP[J][OV]/SYCOMP;
Esim->NXDER[J][FN] = (Fl*Esim->SCOMP[J][IL]+Fv*Esim->SCOMP[J][IV] -(Fv)*Esim->NXS[J][FN]
-(Fl)*Esim->NXS[J][FN])/(10 + MAXI(Fp-(*Vapor),100.0)
*Esim->STPSIZEH/10000);
Esim->NXS[J][FN] = EULER( N, K, TYPE, DER, MAX, MIN, FLAG);
SXCOMP = SXCOMP + Esim->NXS[J][FN];
}
for(J=Esim->COMPBEG; J<=Esim->COMPEND; J++)
{
Esim->NXS[J][FN] = Esim->NXS[J][FN]/SXCOMP;
Esim->SCOMP[J][OL] = Esim->NXS[J][FN];
}
Hg = power((Fp/DesFp),0.5);
// Efficiency of the condenser
Esim->NS2[FN] = Condeff*MAXI(Htcfl*Hg,Htcmaxfl);
Esim->NHTR[FN] = Esim->NS2[FN]*(Tflsh - Tc);
if(Fc>0) // Cooling water side calculations
{
// From McCabe & Smith h=K*(Fc)**(-1/3)
Hc = power((Fc/DesFc),-0.33);
// Accounting for the overall Heat transfer coefficient
// 1/U = 1/Hg + 1/Hc
Esim->NS2[FN] = Condeff*CondHtarea*(Hg * Hc)/(Hg + Hc);
// Energy balance across the cooling node to find out temperature
// F*CP*(I/T-AMB)+HTRAN-F*CP*(O/T-AMB)-HLOSS = ACC*CP*(dT/dt)*dt
Esim->NTDER[CN]=(Heatin-Heatout)/(Mhc*Cpcw*Esim->STPSIZEH/10000);
} // Cooling water side calculations
Esim->STEMP[OV] = Esim->NTEMP[FN];
Esim->STEMP[OL] = Esim->NTEMP[FN];
Esim->SMFLOW[OV] = *Vapor;
Esim->SMFLOW[OL] = *Liquid;
return 0;
}
modified 10-Apr-14 8:26am.
|
|
|
|
|
Firstly, please indent your code properly and use <pre> tags around it so that it is readable, like:
for(J = Esim->COMPBEG; J <= Esim->COMPEND; J++)
{
Esim->SCOMP[J][OV] = Esim->SCOMP[J][OV]/SYCOMP;
Esim->NXDER[J][FN] = (Fl*Esim->SCOMP[J][IL]+Fv*Esim->SCOMP[J][IV] -(Fv)*Esim->NXS[J][FN]
-(Fl)*Esim->NXS[J][FN])/(10 + MAXI(Fp-(*Vapor),100.0)
*Esim->STPSIZEH/10000);
Esim->NXS[J][FN] = EULER( N, K, TYPE, DER, MAX, MIN, FLAG);
SXCOMP = SXCOMP + Esim->NXS[J][FN];
}
Secondly, please indicate exactly where the error occurs, and show the values of the inputs at the time.
|
|
|
|
|
You don't want to do that, you want to avoid device by zero period. If you don't know why, go back to school.
"The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold
|
|
|
|
|
One possible reason of many is that in the original (vc++ 6) structured exception handling was enabled and now it isn't. This would mean that divide by 0 exceptions were being thrown but not being caught (possibly). Now they are just crashing the program.
Unhandled divide by zero is bad so don't worry about why it did/did not. Just fix the problem now. Structured exception handling is a very good way of doing that.
Peter Wasser
"The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts." - Bertrand Russell
|
|
|
|
|
Hello there,
I am using CDHtmlDialog in my MFC application to create a UI with HTML, CSS & JavaScript.
In my project there's one dialog created from CDHtmlDialog and a corresponding HTML file for that dialog.
Now during run time i want to change the contents of the HTML file.
Not just the HTML part, i want to change the CSS & JavaScript too.
So basically what am trying to do is, change the entire content of the default HTML file.
Is this possible? If yes, how can i do that?
Thanks in advance!
|
|
|
|
|
I have a static control area, it is about 4 lines height.
if I only have one string to display, How can I make it horizontal and vertical center?
Thanks
|
|
|
|
|
|
For the horizontal center, use SS_CENTER, there is not vertical center for CStatic ( or Cedit), you will have to do it manually.
I'd rather be phishing!
|
|
|
|
|
I use \r\n, almost got vertical center effect
|
|
|
|
|
If you want it to be exact then you need to use a different control (possibly a RichEdit) or subclass the label and do your own painting of the text.
|
|
|
|
|
Exactly what Richard said above just subclass a static text box and you can use it over and over again.
I have a standard code I use to do what you are doing and it also allows you to change the font, text color and background color as well which is all fairly trivial.
Let me know if you want to see how to code it as I wont paste that amount of code if you don't need it.
|
|
|
|
|
We have a client-server application developed using MFC. The client-server communication is via TCP/IP sockets within a LAN network.
Now we want to convert this application to a web application. Could you please provide me with any guidelines how to start and proceed.
|
|
|
|
|
|
Yo need to write a web based VM that supports the win32 spi.
"The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold
|
|
|
|
|
You would be much better learning ASP.NET and starting from scratch. Converting MFC to ASP.NET would be much more difficult.
|
|
|
|
|
I like the idea of building a web based VM, shouldn't take him more than a few years dint you think?
"The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold
|
|
|
|
|
im using visual studio 2010.im new to this.i have a button and a text box. i want to enter a string in the text box.eg:"hello". and if i click the button it should count the number of characters in the string and return the value..eg: in this case "hello" ,when i click the button it should count each letter and display the value "5"(string "hello" contains 5 characters).can any1 help me by sending the exact code?
|
|
|
|
|
Have you looked at the WM_GETTEXTLENGTH message? If you are using MFC, check out GetWindowTextLength().
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
He has to handle WM_COMMAND first.... then the button ID..... then get the window handle to the edit control....
This guy needs to go back and start form scratch and read some books/do some tutorials on Win32/MFC.
"The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold
|
|
|
|
|