Faria R.
asked 07/12/21modify the code so i can submit it without plagiarism
Main.cpp
1 Expert Answer

Patrick B. answered 07/13/21
Math and computer tutor/teacher
Create a data file that contains the student records so that you do not have to input them every time the programs runs.
Are you trying to sort them?
using namespace std;
#include
#include
#include
#include
#include
typedef
class Student
{
protected:
long studentNum;
string studentName;
float studentGPA;
public:
Student(long longIntNum, string strName, float gpa)
{
studentNum = longIntNum;
studentName = string(strName);
studentGPA = gpa;
}
Student() { studentNum=0; studentGPA=0.0f; studentName=""; }
Student(const Student&student)
{
studentNum = student.studentNum;
studentName = string(student.studentName);
studentGPA = student.studentGPA;
}
string GetStudentName() { return(studentName); }
float GetStudentGPA() { return(studentGPA); }
long GetStudentNum() { return(studentNum); }
void SetStudentGPA(float gpa) { studentGPA = gpa; }
} *TStudent;
#define STUDENT_SIZE (sizeof(Student))
//callback functions passed to quicksort
//sorts by id#
int CompareStudentByIdNum(const void * rec1, const void * rec2)
{
int iReturn=0;
TStudent studentRec1 = (TStudent)rec1;
TStudent studentRec2 = (TStudent)rec2;
long longIntStudentNum1 = studentRec1->GetStudentNum();
long longIntStudentNum2 = studentRec2->GetStudentNum();
if (longIntStudentNum1 != longIntStudentNum2)
{
iReturn = (longIntStudentNum1 > longIntStudentNum2) ? 1 : -1;
}
return(iReturn);
}
int CompareStudentByName(const void * rec1,const void * rec2)
{
int iReturn=0;
TStudent studentRec1 = (TStudent)rec1;
TStudent studentRec2 = (TStudent)rec2;
string studentNameStr1 = studentRec1->GetStudentName();
string studentNameStr2 = studentRec2->GetStudentName();
iReturn = studentNameStr1.compare(studentNameStr2);
if (iReturn==0) //names are the same, sorts by id #
{
iReturn = CompareStudentByIdNum(rec1,rec2);
}
return ( iReturn );
}
int CompareStudentByGPA(const void * rec1, const void * rec2)
{
int iReturn=0;
TStudent studentRec1 = (TStudent)rec1;
TStudent studentRec2 = (TStudent)rec2;
float gpa1 = studentRec1->GetStudentGPA();
float gpa2 = studentRec2->GetStudentGPA();
if (gpa1 != gpa2)
{
iReturn = gpa1 > gpa2 ? 1 : -1;
}
else //GPAs are the same, sorts by id #
{
iReturn = CompareStudentByIdNum(rec1,rec2);
}
return ( iReturn );
}
typedef struct _TStudentDB
{
TStudent students;
int nCount;
} * TStudentDB;
#define STUDENT_DB_SIZE (sizeof(struct _TStudentDB))
void Read(TStudentDB studentDB, char * filename)
{
char inbuff[1025];
FILE * fptr = fopen(filename,"r");
if (fptr!=NULL)
{
fscanf(fptr,"%d",&studentDB->nCount);
int n = studentDB->nCount;
//eats the newline
//cout << n << " records in the file " << endl;
fgets(inbuff,1025,fptr);
studentDB->students = new Student[n];
for (int iLoop=0; iLoop
{
long longIntStudentNum;
float gpa;
char nameStr[1025];
fscanf(fptr,"%ld %s %f",&longIntStudentNum,nameStr,&gpa);
string strName(nameStr);
//cout << longIntStudentNum << " : " << strName << " : " << gpa << endl;
studentDB->students[iLoop] = Student(longIntStudentNum,strName,gpa);
}
}
else
{
cout << "error reading input " << endl;
}
}
void Dump(TStudentDB studentDB, char * strMsg=NULL)
{
if (strMsg!=NULL)
{
cout << "*******************************************************" << endl;
cout << strMsg << endl;
}
cout << "*******************************************************" << endl;
cout << "id # name gpa " << endl;
for (int iLoop=0; iLoopnCount; iLoop++)
{
TStudent curStudentRec = &studentDB->students[iLoop];
cout << curStudentRec->GetStudentNum() << " " << curStudentRec->GetStudentName() << " " << curStudentRec->GetStudentGPA() << endl;
}
}
main()
{
struct _TStudentDB studentDB;
Read(&studentDB,(char*) "E:\\students.dat");
qsort(studentDB.students,studentDB.nCount,STUDENT_SIZE,CompareStudentByIdNum); //change the function to sort in another order
//CompareStudentByName
//COmpareStudentByGPA
Dump(&studentDB /* , (char*) "SORTED BY ..... " */ );
}
//--------------------------------------------
~users\pab\home> cat E:students.dat
5
51381 Baldwin:Patrick 4.0
33333 Sullivan:David 3.3
69711 Abels:Jennifer 4.0
66666 Roy:Mark 1.5
43210 Ocampeo:Mary 3.4
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Faria R.
the last part of these code int main() { UnsortedType l; if(l.isEmpty()) { cout << "List is empty" << endl; } else { cout << "List is not empty" << endl; } l.InsertItem(5); l.InsertItem(7); l.InsertItem(6); l.InsertItem(9); if(l.isEmpty()) { cout << "List is empty" << endl; } else { cout << "List is not empty" << endl; } if(l.isFull()) { cout << "List is full" << endl; } else { cout << "List is not full" << endl; } l.printList(); cout << l.LengthIs() << endl; l.InsertItem(1); l.printList(); bool f; l.RetrieveItem(4, f); if(f) { cout << "It is found" << endl; } else { cout << "It is not found" << endl; } l.RetrieveItem(5, f); if(f) { cout << "It is found" << endl; } else { cout << "It is not found" << endl; } l.RetrieveItem(9, f); if(f) { cout << "It is found" << endl; } else { cout << "It is not found" << endl; } l.RetrieveItem(10, f); if(f) { cout << "It is found" << endl; } else { cout << "It is not found" << endl; } if(l.isFull()) { cout << "List is full" << endl; } else { cout << "List is not full" << endl; } l.DeleteItem(1); l.printList(); l.DeleteItem(7); l.printList(); UnsortedType l2; StudentInfo a(15234, "Jon", 2.6), b(13732, "Tyrion", 3.9), c(13569, "Sandor", 1.2), d(15467, "Ramsey", 3.1), e(16285, "Arya", 3.1); l2.InsertItem(a); l2.InsertItem(b); l2.InsertItem(c); l2.InsertItem(d); l2.InsertItem(e); l2.printList(); l2.DeleteItem(d); l2.RetrieveItem(c, f); if(f) { cout << "It is found" << endl; cout << c << endl; } else { cout << "It is not found" << endl; } l2.printList(); }07/12/21