Activity 1: A Student Structure

In this activity you have to implement two functions to assist in the construction and printing of a student structure. The factory function createStudent() takes the appropriate parameters, allocates memory to hold a new student and sets each field of the student appropriately. In the case of strings, malloc is used to create enough space and the string is then copied. The birthdate is handled specially: it parses the date string and creates a struct tm, which is a “time” structure defined by the time library (time.h). Ultimately, a pointer to the new student structure is returned. You also need to implement the printStudent() function that takes a student (by reference) and prints it out to the standard output. We have also provided two fully implemented functions (createEmptyStudent and studentToString) for you to examine and understand how they work.

Instructions

1.Download the studentDemo.c source file

2.Implement the two functions: createStudent() and printStudent()

3.Change the values in the main function to your name, NUID, and birth date.

4.Compile and run the program.

5.Examine the following two implemented functions: createEmptyStudent() and studentToString().

6.Use them in your main function.

7.Show your program to a lab instructor.

Refer back to this program in Activity 2 as needed.

studentDemo.c

#include
#include
#include
#include

typedef struct {
char *firstName;
char *lastName;
int nuid;
struct tm birthDate;
} Student;

Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str);
void printStudent(Student * student);

int main(void) {

Student *me = createStudent(“Richard”, “Feynman”, 140602, “07-30-1980”);
printStudent(me);

}

/**
* A “factory” function to create a new student structure initialized with the given values
* Strings are deep copied.
* You must use the “strptime” function defined in the file to copy the “birthDate_str” from the argument to the “birthDate” struct component of the Student struct
*/
Student * createStudent(const char * firstName, const char *lastName, int nuid, const char * birthDate_str) {

  
return student;
}

/*
* A factory function to display a student first name, last name, nuid and birth date
*/

void printStudent(Student * student) {
  
}

/**
* A “factory” function to create a new student structure with everything initialized to
* default values (null or zero)
*/
Student * createEmptyStudent() {
Student * student = (Student *) malloc(sizeof(Student) * 1);
student->firstName = NULL;
student->lastName = NULL;
student->nuid = 0;
return student;
}

char *studentToString(Student *student) {
//create a temporary string that is “large enough”
char *tmp = (char *) malloc(sizeof(char) * 2000);
  
//format the student into the temporary string
sprintf(tmp, “%s, %s (%08d, %d-%d-%d)n”, student->lastName, student->firstName, student->nuid, (student->birthDate.tm_year+1900), (student->birthDate.tm_mon+1), student->birthDate.tm_mday);
  
//create the final string that is the exact size needed
char *result = (char *) malloc(sizeof(char) * (strlen(tmp)+1));
  
//free up the temporary string
free(tmp);
  
//return the result
return result;
}



Source link

Leave a Reply

Your email address will not be published.