Main:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "StudentRecordDataNode.h"
#include "Course.h"
#include "StudentSingleListedLink.h"
#define SIZE_BUF 15
using namespace std;
void sortNodes(StudentRecordDataNode * ptr);
void main()
{
double marks[18];
unsigned int Age;
unsigned int ID;
unsigned int CourseCount;
char FirstName[10];
char LastName[10];
Course Courses[18];
StudentRecordDataNode *node = 0;
StudentSingleListedLink iList;
StudentRecordDataNode *nd = 0;
ifstream is;
is.open ("OOP344_Assignment_3_2011_01.dat", ios::binary );
do {
//beginning of saving records from the file
for(int i=0; i<18; i++)
marks[i] = 0;
is.read(reinterpret_cast<char *>(&marks), sizeof(double)*18);
is.read(reinterpret_cast<char *>(&Age), sizeof(Age));
is.read(reinterpret_cast<char *>(&ID), sizeof(ID));
is.read(reinterpret_cast<char *>(&CourseCount), sizeof(CourseCount));
is.read(reinterpret_cast<char *>(&FirstName), sizeof(FirstName));
is.read(reinterpret_cast<char *>(&LastName), sizeof(LastName));
for (int i=0; i < 18; i++)
{
is.read(reinterpret_cast<char *>(&Courses[i].Semester), sizeof(Courses[i].Semester));
is.read(reinterpret_cast<char *>(&Courses[i].Name), sizeof(Courses[i].Name));
}
//end of saving
StudentRecordData *r;
r = new StudentRecordData(marks,Age,ID,CourseCount,FirstName,LastName, Courses);
node = new StudentRecordDataNode(r);
iList.AddNode(node);
} while (!is.eof());
//nd=iList.getFirstNode();
sortNodes(node);
for(nd=iList.getFirstNode();nd;nd=iList.getNextNode(nd))
nd->getValue()->display();
is.close();
cin.get();
return;
}
void sortNodes(StudentRecordDataNode * ptr) {
StudentRecordData * temp=0;
StudentRecordDataNode * curr;
for(bool didSwap = true; didSwap; ) {
didSwap = false;
for(curr = ptr; curr->next != NULL; curr = curr->next) {
if(curr->getValue()->getAge() > curr->next->getValue()->getAge()) {
temp = curr->getValue();
*curr->getValue() = *curr->next->getValue();
*curr->next->getValue() = *temp;
didSwap = true;
}
}
}
}
StudentRecordDataNode.h
Code:
#ifndef STUDENTRECORDDATANODE_H
#define STUDENTRECORDDATANODE_H
#include "StudentRecordData.h"
class StudentRecordDataNode
{
StudentRecordData *Value;
friend class StudentSingleLinkedList;
public:
StudentRecordDataNode *next;
StudentRecordDataNode();
StudentRecordDataNode(StudentRecordData *val)
{
Value = val;
next = 0;
}
StudentRecordData *getValue()
{
return Value;
}
};
#endif
StudentSingleListedLink.h
Code:
#ifndef StudentSingleListedLink_H
#define StudentSingleListedLink_H
#include "StudentRecordDataNode.h"
class StudentSingleListedLink
{
StudentRecordDataNode *root;
public:
StudentSingleListedLink();
~StudentSingleListedLink();
void AddNode(StudentRecordDataNode *);
StudentRecordDataNode *getFirstNode();
StudentRecordDataNode *getNextNode(StudentRecordDataNode *nd);
};
#endif
StudentSingleListedLink.cpp
Code:
#include "StudentSingleListedLink.h"
StudentSingleListedLink::StudentSingleListedLink()
{
root = 0;
}
StudentSingleListedLink::~StudentSingleListedLink()
{
StudentRecordDataNode *curr=root;
StudentRecordDataNode *next;
while(curr)
{
next = curr->next;
delete curr;
curr = next;
}
}
void StudentSingleListedLink::AddNode(StudentRecordDataNode *nd)
{
if(!root)
{
root = nd;
return;
}
nd->next = root;
root = nd;
}
StudentRecordDataNode *StudentSingleListedLink::getFirstNode()
{
return root;
}
StudentRecordDataNode *StudentSingleListedLink::getNextNode(StudentRecordDataNode *nd)
{
return nd->next;
}
StudentRecordData.h
Code:
#ifndef STUDENTRECORDDATE_H
#define STUDENTRECORDDATE_H
#include "Course.h"
#define MAX_COURSES 18
#define LEN_FN 10
#define LEN_LN 10
class StudentRecordData
{
double Marks[MAX_COURSES];
unsigned int Age;
unsigned int ID;
unsigned int CourseCount;
char FirstName[LEN_FN];
char LastName[LEN_FN];
Course Courses[MAX_COURSES];
public:
StudentRecordData();
StudentRecordData(double m[18], unsigned int age, unsigned int id, unsigned int cc, char fn[10], char ln[10], Course c[18]);
unsigned int getID() {return ID;}
unsigned int getAge() {return Age;}
void display();
};
#endif
StudentRecordData.cpp
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "StudentRecordData.h"
#include "Course.h"
using namespace std;
StudentRecordData::StudentRecordData(double m[18], unsigned int age, unsigned int id, unsigned int cc, char fn[10], char ln[10], Course c[18])
{
for(int i=0;i<18;i++)
Marks[i] = m[i];
Age = age;
ID = id;
CourseCount = cc;
strcpy(FirstName, fn);
strcpy(LastName, ln);
for(int i=0;i<18;i++)
{
strcpy(Courses[i].Name,c[i].Name);
Courses[i].Semester = c[i].Semester;
}
}
void StudentRecordData::display()
{
for(int i=0; i<18; i++)
{
if (Marks[i] > -1)
cout << Marks[i] << " ";
}
cout << endl << "Age: " << Age << " ID: " << ID << " Course Count: " << CourseCount << " First Name: " << FirstName
<< " Last Name: " << LastName << endl;
for(int i=0;i<CourseCount;i++)
{
cout << Courses[i].Name << " ";
}
cout << endl<<endl;
}
the file needed to be read:
Code:
@Q@ J@ O@ L@ ΐX@ ΐS@ Q@ O@ ΐQ@ N@ X@ J@ ΐV@ @X@ M@ R@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ Ψ Mitcha ΜΜΜAdru ΜΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ J@ W@ R@ L@ @U@ ΐX@ V@ Q@ O@ K@ @S@ T@ @T@ ΐU@ W@ @U@ ΐR@ R@ Ί6 James ΜΜΜΜHarkley ΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444 ΐR@ P@ W@ Q@ L@ ΐP@ O@ V@ @X@ ΐX@ V@ T@ ΐV@ @W@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ" βκ Jake ΜΜΜΜΜChoy ΜΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ T@ Q@ @X@ @P@ @P@ T@ @U@ @U@ L@ U@ R@ R@ M@ ΐQ@ S@ I@ R@ J@ }T David ΜΜΜΜHansen ΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444 T@ Q@ S@ V@ L@ @T@ @V@ W@ ΐR@ S@ P@ @S@ L@ O@ M@ W@ T@ O@ | Matt ΜΜΜΜΜFaria ΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444 L@ V@ @T@ M@ ΐS@ V@ X@ P@ N@ S@ ΐX@ X@ N@ ΐX@ ΐT@ I@ K@ΜΜΜΜΜΜΜΜ ξί Maria ΜΜΜΜDallan ΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ΜΜΜΜΜΜΜΜ N@ @X@ T@ P@ @T@ ΐV@ W@ T@ Q@ R@ M@ X@ @X@ T@ T@ @U@ W@ @U@ ι Brandon ΜΜLay ΜΜΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444 V@ L@ J@ ΐX@ @P@ U@ ΐV@ @Q@ J@ J@ P@ @P@ ΐR@ ΐS@ I@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ Ξω Bruno ΜΜΜΜCondello ΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ J@ L@ M@ U@ ΐX@ @T@ ΐS@ V@ S@ ΐT@ S@ V@ ΐR@ @P@ M@ ΐP@ S@ ΐQ@ ΗΡ
Lui ΜΜΜΜΜΜDonghui ΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444 Q@ I@ @Q@ @U@ @U@ ΐW@ @U@ ΐQ@ @R@ @R@ ΐS@ @S@ X@ ΐQ@ T@ N@ O@ΜΜΜΜΜΜΜΜ# 8 Alex ΜΜΜΜΜChow ΜΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ΜΜΜΜΜΜΜΜ ΐV@ ΐR@ Q@ K@ R@ V@ R@ @R@ @S@ V@ T@ ΐR@ @X@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ i
Phuong ΜΜΜLy ΜΜΜΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ U@ @W@ ΐS@ M@ Q@ @V@ ΐU@ ΐU@ ΐP@ X@ Q@ Q@ W@ ΐV@ M@ M@ @W@ΜΜΜΜΜΜΜΜ `β Arthur ΜΜΜBradlow ΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ΜΜΜΜΜΜΜΜ @Q@ W@ S@ ΐU@ ΐX@ O@ P@ W@ M@ ΐP@ N@ @V@ J@ T@ O@ R@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ gl Roya ΜΜΜΜΜSakedad ΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ @S@ N@ @V@ J@ N@ M@ ΐX@ M@ K@ @S@ S@ N@ ΐW@ @W@ @U@ @X@ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ Κ½ Charles ΜΜBrady ΜΜΜΜAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜΜ
The problem lies in the sort. It is duplicating a few of the nodes multiple times. While it is in sorted order. A good bunch of the nodes are gone because a few where duplicated. Any suggestions?