C LANGUAGE IMPLEMENTATION - Writing source files
implement several functions, declared in student.h, transcript.h and internal.h (with support from common.h).
There are 3 rules:
1. There are three types of students: undergraduate, MEng and PhD.
2. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2.
3. Undergraduate students require a mark of 50 to pass; graduate students need a 65.
----------------Common.h--------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef COMMON_H
#define COMMON_H
/*Portable macros for declaring C functions visible to C++.*/
#ifdef __cplusplus
#define BEGIN_DECLS extern "C" {
#define END_DECLS }
#else
#define BEGIN_DECLS
#define END_DECLS
#endif
#endif /* COMMON_H */
------------------------------------Internal.h--------------------------------------------------------------------------------------------------------------------------------
#ifndef STUDENT_INTERNAL_H
#define STUDENT_INTERNAL_H
#include "student.h"
/** Allocate memory for a new @ref student object. */
struct student* student_alloc(void);
/**Initialize an already-allocated @ref student object.
* @returns NULL on failure but does not free the memory*/
struct student* student_init(struct student *, const char *name, size_t namelen,
struct student_id, student_complete);
#endif /* STUDENT_INTERNAL_H */
---------------------------------------------student.h------------------------------------------------------------------------------------------------------------------------
#ifndef STUDENT_H
#define STUDENT_H
#include <sys/types.h>
#include "common.h"
#include "transcript.h"
BEGIN_DECLS
/** A student ID is composed from a registration year and a serial number. */
struct student_id {
unsigned short sid_year;
unsigned int sid_serial;
};
/* Forward declaration. */
struct student;
/**
* Function pointer: a function that examines a @ref student to determine
* whether or not they've finished their program.
*/
typedef int (*student_complete)(struct student *);
/**
* Representation of a registered student.
*/
struct student {
/** The student's full name. Memory is owned by this structure. */
char *s_name;
struct student_id s_id;
/** Number of references to this student. */
unsigned int s_ref;
/** Transcript (singly-linked list, NULL terminator). */
struct transcript_entry *s_transcript;
/** Whether or not this student has completed his/her program. */
student_complete s_complete;
enum Student_Type stundent_type;
};
enum student_Type{
UnderGradStudent,
MEngStudent,
PhdStudent,
}
/**Allocate and initialize a new grad student (MEng or PhD).
*
* The returned structure will have a refcount of 1.*/
struct student* student_grad_create(const char *name, size_t namelen,
struct student_id, int phd);
/** Allocate and initialize a new undergraduate student.
*
* The returned structure will have a refcount of 1.*/
struct student* student_undergrad_create(const char *name, size_t namelen,
struct student_id);
/** A student has completed a course: add to their transcript.
*
* @returns 1 on success, 0 on failure
*/
int student_add_entry(struct student *, struct transcript_entry *);
/**Deallocate a @ref student with a refcount of 1.
*/
void student_free(struct student *);
/** Increment a @ref student's refcount. */
void shold(struct student *);
/** Decrement a @ref student's refcout and free if 0. */
void srelease(struct student *);
END_DECLS
#endif /* STUDENT_H */
----------------------------------------------------------transcript.h--------------------------------------------------------------------------------------------------------
#ifndef TRANSCRIPT_H
#define TRANSCRIPT_H
#include <sys/types.h>
#include "common.h"
BEGIN_DECLS
enum faculty {
ARTS,
BUSINESS,
ENGINEERING,
MEDICINE,
MUSIC,
NURSING,
PHARMACY,
};
/**
* A result in a course (
*/
struct transcript_entry {
/** Identifier: Engineering 1020, etc. */
enum faculty te_faculty;
enum Student_Type student_type;
unsigned int te_number;
/** Grade attained in the course. */
unsigned int te_grade;
/** Number of references to this entry. */
unsigned int te_ref;
/** Next entry in the singly-linked list (or NULL terminator). */
struct transcript_entry *te_next;
};
/**
* Create a new transcript entry.
* @returns a @ref transcript_entry with refcount 1
*/
struct transcript_entry* transcript_entry(
enum faculty, unsigned int course_number, unsigned int grade);
/** Deallocate a @ref transcript_entry. */
void transcript_entry_free(struct transcript_entry *);
/** Increment a @ref transcript_entry's refcount.*/
void tehold(struct transcript_entry *);
/** Decrement a @ref transcript_entry's refcout and free if 0. */
void terelease(struct transcript_entry *);
END_DECLS
#endif /* TRANSCRIPT_H */
#include<stdio.h>
#include<Common.h>
#include<Internal.h>
#include<Transcript.h>
#include<Student.h>
/** Allocate memory for a new @ref student object. */
struct student* student_alloc(void) {
return (struct student*) malloc(sizeof(struct
student));
}
/**Initialize an already-allocated @ref student object.
* @returns NULL on failure but does not free the memory*/
struct student* student_init(struct student *student, const char
*name,
size_t namelen, struct
student_id*student_id, student_complete) {
if (namelen != 0) {
student->s_name = name;
student->s_id =
student_id;
student->s_complete =
student_complete;
return student;
} else {
return NULL;
}
}
/**Allocate and initialize a new grad student (MEng or
PhD).
*
* The returned structure will have a refcount of 1.*/
struct student* student_grad_create(const char *name, size_t
namelen,
struct student_id*student_id, int
phd) {
struct student* student = student_alloc();
student = student_init(student, name, namelen,
student_id,
student_complete);
student->s_ref=1;
return student;
}
/** Allocate and initialize a new undergraduate student.
*
* The returned structure will have a refcount of 1.*/
struct student* student_undergrad_create(const char *name, size_t
namelen,
struct student_id*student_id)
{
struct student* student = student_alloc();
student = student_init(student, name, namelen,
student_id,
student_complete);
student->s_ref=1;
return student;
}
/** A student has completed a course: add to their
transcript.
*
* @returns 1 on success, 0 on failure
*/
int student_add_entry(struct student *student, struct
transcript_entry *transcript_entry){
if(student==NULL){
return 0;
}else{
struct transcript_entry
*t_entry=student->s_transcript;
while(t_entry->te_next!=NULL){
t_entry=t_entry->te_next;
}
t_entry->te_next=transcript_entry;
return 1;
}
}
/**Deallocate a @ref student with a refcount of 1.
*/
void student_free(struct student *student){
if(student!=NULL){
free(student);
}
}
/** Increment a @ref student's refcount. */
void shold(struct student *student){
if(student!=NULL){
student->s_ref++;
}
}
/** Decrement a @ref student's refcout and free if 0. */
void srelease(struct student *student){
if(student!=NULL){
student->s_ref--;
if(student->s_ref==0){
student_free(student);
}
}
}
/**
* Create a new transcript entry.
* @returns a @ref transcript_entry with refcount 1
*/
struct transcript_entry* transcript_entry(enum faculty
faculty,unsigned int course_number, unsigned int grade){
struct transcript_entry* t_entry=(struct
transcript_entry*)malloc(sizeof(transcript_entry));
t_entry->te_faculty=faculty;
t_entry->te_number=course_number;
t_entry->te_grade=grade;
return t_entry;
}
/** Deallocate a @ref transcript_entry. */
void transcript_entry_free(struct transcript_entry *t_entry){
if(t_entry!=NULL){
free(t_entry);
}
}
/** Increment a @ref transcript_entry's refcount.*/
void tehold(struct transcript_entry *t_entry){
if(t_entry!=NULL){
t_entry->te_ref++;
}
}
/** Decrement a @ref transcript_entry's refcout and free if 0.
*/
void terelease(struct transcript_entry *t_entry){
if(t_entry!=NULL){
t_entry->te_ref--;
if(t_entry->te_ref==0){
free(t_entry);
}
}
}
Following is the implementation of the methods. Please revert in case of doubt.
Get Answers For Free
Most questions answered within 1 hours.