Question

C LANGUAGE IMPLEMENTATION - Writing source files implement several functions, declared in student.h, transcript.h and internal.h...

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 */

Homework Answers

Answer #1

#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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
//evil_server.cpp #include <string> #include <cstdlib> #include <iostream> #include "evil_server.h" using namespace std; EvilServer :: EvilServer() {...
//evil_server.cpp #include <string> #include <cstdlib> #include <iostream> #include "evil_server.h" using namespace std; EvilServer :: EvilServer() {    hacked[0] = hacked[1] = hacked[2] = false;    passwords[agent_index(MrMean)] = random_pw(MrMean);    passwords[agent_index(MsChief)] = random_pw(MsChief);    passwords[agent_index(DrEvil)] = random_pw(DrEvil); } void EvilServer :: change_pw(EvilAgent agent, string new_pw) {    int index = agent_index(agent);    if (new_pw == passwords[index])        return;    hacked[index] = false;    passwords[index] = new_pw; } string EvilServer :: random_pw(EvilAgent agent) {    string password;    int length;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT