I am writing a program for keeping a course list for each student in a college. The information about each student should be kept in an object that contains the student's name and a list of courses completed by the student.
* must use a DYNAMIC ARRAY for storing students
* must use LINKED LISTS for storing courses (1 course list for each student)
I have written a course class which is a linked list to store the courses with name, credit and GPA; a student class to contain a student's name and a course list. I am writing the third class: school which should be a dynamic array of student.
The prgram should prompt for user inputs for student name, course name, course GPA, and course credit. All these will be stored in student_list. I kind of wrote a student_insert function, but it doen't seem to work. Could someone please help me out in writing a school member function to insert a student into the student_list with the user inputs? Thank you in advance.
========
student.h
========
Code:
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
namespace jk1998_A
{
class course
{
public:
course(const std::string& name=NULL, const double& GPA=0, const double& credit=0, course* link = NULL);
void set_name(const std::string& name) {course_name = name;}
void set_GPA(const double& GPA) {course_GPA = GPA;}
void set_credit(const double& credit) {course_credit = credit;}
void set_node(course* new_node) {node=new_node;}
course* link() {return node;}
const course* link() const {return node;}
std::string get_name() const {return course_name;}
double get_GPA() const {return course_GPA;}
double get_credit() const {return course_credit;}
private:
std::string course_name;
double course_GPA;
double course_credit;
course* node;
};
void list_head_insert(course*& head_ptr, const std::string& name, const double& GPA, const double& credit);
course* list_search(course* head_ptr, const std::string& target);
const course* list_search(const course* head_ptr, const std::string& target);
void list_remove(course* previous_ptr);
void list_head_remove(course*& head_ptr);
void list_insert(course* previous_ptr, const std::string& name, const double& GPA, const double& credit);
void list_copy(const course* source_ptr, course*& head_ptr, course*& tail_ptr);
void list_clear(course*& head_ptr);
class student
{
public:
student(const std::string& name=NULL);
student(const student& source);
~student();
void operator =(const student& source);
void set_student_name(const std::string& name) {student_name=name;}
std::string get_student_name() {return student_name;}
void course_insert(const std::string& name, const double& GPA, const double& credit);
bool course_erase(const std::string& target);
private:
std::string student_name;
course* head_ptr;
int many_nodes;
};
class school
{
public:
static const int DEFAULT_CAPACITY;
school(int initial_capacity = DEFAULT_CAPACITY);
school(const school& source);
~school();
void reserve(int new_capacity);
void student_insert(const student& entry);
private:
student* student_list;
int used;
int capacity;
};
}
#endif
========
implement.cpp
========
Code:
#include <string>
#include <algorithm>
#include "student.h"
using namespace std;
namespace jk1998_A
{
/******COURSE*******/
course::course(const string& name, const double& GPA, const double& credit, course* link)
{
course_name = name;
course_GPA = GPA;
course_credit = credit;
node = link;
}
void list_head_insert(course*& head_ptr, const string& name, const double& GPA, const double& credit)
{
head_ptr = new course(name, GPA, credit, head_ptr);
}
course* list_search(course* head_ptr, const string& target)
{
course* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
if (target.compare(cursor->get_name())==0)
return cursor;
return NULL;
}
const course* list_search(const course* head_ptr, const std::string& target)
{
const course* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
if (target.compare(cursor->get_name())==0)
return cursor;
return NULL;
}
void list_remove(course* previous_ptr)
{
course* remove_ptr;
remove_ptr = previous_ptr->link();
previous_ptr->set_node(remove_ptr->link());
delete remove_ptr;
}
void list_head_remove(course*& head_ptr)
{
course* remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link();
delete remove_ptr;
}
void list_insert(course* previous_ptr, const string& name, const double& GPA, const double& credit)
{
course* insert_ptr;
insert_ptr = new course;
insert_ptr->set_name(name);
insert_ptr->set_GPA(GPA);
insert_ptr->set_credit(credit);
insert_ptr->set_node(previous_ptr->link());
previous_ptr->set_node(insert_ptr);
}
void list_copy(const course* source_ptr, course*& head_ptr, course*& tail_ptr)
{
head_ptr = NULL;
tail_ptr = NULL;
if(source_ptr == NULL)
return;
list_head_insert(head_ptr, source_ptr->get_name(), source_ptr->get_GPA(), source_ptr->get_credit());
tail_ptr = head_ptr;
source_ptr = source_ptr->link();
while(source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->get_name(), source_ptr->get_GPA(), source_ptr->get_credit());
tail_ptr = tail_ptr->link();
source_ptr = source_ptr->link();
}
}
void list_clear(course*& head_ptr)
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
/*******STUDENT******/
student::student(const std::string& name)
{
student_name = name;
head_ptr = NULL;
many_nodes=0;
}
student::student(const student& source)
{
course* tail_ptr;
list_copy(source.head_ptr, head_ptr, tail_ptr);
student_name=source.student_name;
many_nodes=source.many_nodes;
}
student::~student()
{
list_clear(head_ptr);
many_nodes=0;
}
void student::operator =(const student& source)
{
course* tail_ptr;
if(this == &source)
return;
list_clear(head_ptr);
many_nodes = 0;
list_copy(source.head_ptr, head_ptr, tail_ptr);
many_nodes = source.many_nodes;
}
void student::course_insert(const string& name, const double& GPA, const double& credit)
{
list_head_insert(head_ptr, name, GPA, credit);
++many_nodes;
}
bool student::course_erase(const string& target)
{
course* target_ptr;
target_ptr = list_search(head_ptr, target);
if (target_ptr == NULL)
return false;
target_ptr->set_name(head_ptr->get_name());
target_ptr->set_GPA(head_ptr->get_GPA());
target_ptr->set_credit(head_ptr->get_credit());
list_head_remove(head_ptr);
--many_nodes;
return true;
}
/*********SCHOOL********/
const int school::DEFAULT_CAPACITY=20;
school::school(int initial_capacity)
{
student_list = new student[initial_capacity];
capacity = initial_capacity;
used=0;
}
school::school(const school& source)
{
student_list = new student[source.capacity];
capacity = source.capacity;
used = source.used;
copy(source.student_list, source.student_list + used, student_list);
}
school::~school()
{
delete [] student_list;
}
void school::reserve(int new_capacity)
{
student* larger_array;
if(new_capacity == capacity)
return;
if(new_capacity < used)
new_capacity = used;
larger_array = new student[new_capacity];
copy(student_list, student_list + used, larger_array);
delete [] student_list;
student_list = larger_array;
capacity = new_capacity;
}
void school::student_insert(const student& entry)
{
if(used == capacity)
reserve(used + 10);
student_list[used] = entry;
++used;
}
}
=========
main
========
Code:
#include <cstdlib>
#include <iostream>
#include "student.h"
using namespace std;
using namespace jk1998_A;
int main()
{
school students;
system("PAUSE");
return EXIT_SUCCESS;
}