Showing Page:
1/18
ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS
WEEK.
PLEASE READ COMMENTS AND CODE BEFORE ANSWERING
CODING SECTIONS
HW07 #Q1-Q5
HW08 #Q1-Q2
Solution
#include <stdio.h>
#include <ctype.h>
#include <string>
// used to create a linked list of absents containing
\"dates\"
typedef struct absent {
char date[30];
struct absent *next;
} absent;
// used to hold student information and linked list of
\"absents\"
struct student {
char name[30];
Showing Page:
2/18
char standard[30];
struct absent *absents;
};
// used to create a linked list of containers, each
contaning a \"student\"
struct container {
struct student *student;
struct container *next;
}
*list = NULL;
// forward declaration of functions that have a lready been
implemented
void flush();
void branching(char);
void registration(char);
void remove_all(struct container*);
void display(struct container*);
// the following forward declarations are for functions that
require implementation
// return type // name and parameters // points
void add_student(char*, char*); // 5
struct student* search_student(char*); // 5
void add_absent(char*, char*); // 10
Showing Page:
3/18
char* last_absent(char*); // 15
void remove_one(char*); // 15
// Total: 50 points for hw07
struct container* list_of_standard(char*); // 25
struct container* list_by_name(); // 25
// Total: 50 points for hw08
int main()
{
char ch = \'i\';
printf(\"Student Information\ \ \");
do
{
printf(\"Please enter your selection:\ \");
printf(\"\\ta: add a new student to the list\ \");
printf(\"\\ts: search for a student on the list\ \");
printf(\"\\tr: remove a student from the list\ \");
printf(\"\\tc: add an absence date for a student\ \");
printf(\"\\tl: display last absence date for a student \ \");
printf(\"\\tn: display list of students by name\ \");
printf(\"\\tb: display list of students of a given standard\
\");
printf(\"\\tq: quit\ \");
ch = tolower(getchar());
flush();
Showing Page:
4/18
branching(ch);
} while (ch != \'q\');
remove_all(list);
list = NULL;
_CrtDumpMemoryLeaks(); // check for memory leaks (VS
will let you know in output if they exist)
return 0;
}
// consume leftover \'\ \' characters
void flush()
{
int c;
do c = getchar(); while (c != \'\ \' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case \'a\':
case \'s\':
case \'r\':
case \'c\':
case \'l\':
Showing Page:
5/18
case \'b\':
case \'n\': registration(c); break;
case \'q\': break;
default: printf(\"Invalid input!\ \");
}
}
// This function will determine what info is needed and
which function to send that info to.
// It uses values that are returned from some functions to
produce the correct ouput.
// There is no implementation needed here, but you
should trace the code and know how it works.
// It is always helpful to understand how the code works
before implementing new features.
// Do not change anything in this function or you risk
failing the automated test cases.
void registration(char c)
{
if (c == \'a\')
{
char input[100];
printf(\"\ Please enter the student\'s info in the following
format:\ \");
printf(\"name:standard\ \");
Showing Page:
6/18
fgets(input, sizeof(input), stdin);
// discard \'\ \' chars attached to input
input[strlen(input) - 1] = \'\\0\';
char* name = strtok(input, \":\"); // strtok used to parse
string
char* standard = strtok(NULL, \":\");
struct student* result = search_student(name);
if (result == NULL)
{
add_student(name, standard);
printf(\"\ Student added to list successfully\ \ \");
}
else
printf(\"\ That student is already on the list\ \ \");
}
else if (c == \'s\' || c == \'r\' || c == \'c\' || c == \'l\')
{
char name[30];
printf(\"\ Please enter the student\'s name:\ \");
fgets(name, sizeof(name), stdin);
// discard \'\ \' chars attached to input
name[strlen(name) - 1] = \'\\0\';
struct student* result = search_student(name);
if (result == NULL)
Showing Page:
7/18
printf(\"\ That student is not on the list\ \ \");
else if (c == \'s\')
printf(\"\ Standard: %s\ \ \", result->standard);
else if (c == \'r\')
{
remove_one(name);
printf(\"\ Student removed from the list\ \ \");
}
else if (c == \'c\')
{
char date[30];
printf(\"\ Please enter the date of absence:\ \");
fgets(date, sizeof(date), stdin);
// discard \'\ \' chars attached to input
date[strlen(date) - 1] = \'\\0\';
add_absent(name, date);
printf(\"\ Absent date added\ \ \");
}
else if (c == \'l\')
{
char* result = last_absent(name);
if (result == NULL)
printf(\"\ No absence documented.\ \ \");
else
Showing Page:
8/18
printf(\"\ Last absent on: %s\ \ \", result);
}
}
else if (c == \'b\')
{
char standard[30];
printf(\"\ Please enter the standard:\ \");
fgets(standard, sizeof(standard), stdin);
// discard \'\ \' chars attached to input
standard[strlen(standard) - 1] = \'\\0\';
struct container* result = list_of_standard(standard);
printf(\"\ List of students with standard %s:\ \ \",
standard);
display(result);
remove_all(result);
result = NULL;
}
else // c = \'n\'
{
struct container* result = list_by_name();
printf(\"\ List of students sorted by name:\ \ \");
display(result);
remove_all(result);
result = NULL;
Showing Page:
9/18
}
}
// This function recursively removes all students from the
linked list of containers
// Notice that all of the absents for all of the students
must be removed as well
void remove_all(struct container* students)
{
struct absent* temp;
if (students != NULL)
{
remove_all(students->next);
while (students->student->absents != NULL)
{
temp = students->student->absents;
students->student->absents = students->student-
>absents->next;
free(temp);
}
free(students->student);
free(students);
}
}
// This function prints the list of students in an organized
Showing Page:
10/18
format
// It may be useful to trace this code before you get
started
void display(struct container* students)
{
struct container* container_traverser = stu dents;
if (container_traverser == NULL)
{
printf(\"\ There are no students on this list!\ \ \");
return;
}
while (container_traverser != NULL) // traverse list of
students
{
printf(\"Name: %s\ \", container_traverser->student-
>name);
printf(\"Standard: %s\ \", container_traverser->student-
>standard);
printf(\"Absence on file: \");
struct absent* ptr = container_traverser ->student-
>absents;
if (ptr == NULL)
{
printf(\"No absence documented.\");
Showing Page:
11/18
}
else
{
while (ptr != NULL) // traverse list of absents
{
printf(\"\ %s\", ptr->date);
ptr = ptr->next;
}
}
printf(\"\ \ \"); // formatting
container_traverser = container_traverser->next;
}
}
// hw07 Q1 : add (5 points)
// This function should add student to the head of the list
of containers.
// The function search_student() is called before calling
this function,
// therefore you can assume that the student is not
already on the list.
void add_student(char* name, char* standard)
{
container *temp = new container();
temp->student = new student();
Showing Page:
12/18
strcpy(temp->student->name,name);
strcpy(temp->student->standard,standard);
temp->next = list;
list = temp;
}
// hw07 Q2 : search (5 points)
// In this function, you are passed the name of a student
to find his/her standard.
// If the student exists on the list, return a pointer to the
requested student. If not, return NULL.
// (You must return a pointer to a node in your list. Do
not create a pointer that just includes the standard)
// (Remember that it is enough to sea rch for a student by
only their name since no 2 students will have the same
name)
struct student* search_student(char* name)
{
container *temp = list;
while( temp )
{
if ( temp->student && strcmp (temp->student-
>name , name) == 0 )
{
Showing Page:
13/18
return temp->student;
}
temp = temp->next;
}
return NULL;
}
// hw07 Q3: add_absent (10)
// In this function, you are passed the name of a student
and a date of absence.
// You should add the date to the tail of t he linked list of
the students \"absents\".
// You can assume that all absents will be added in
chronological order.
// The function search_student() is called before calling
this function,
// therefore you can assume that the student is already
on the student list and
// the new absence date is not on the absents list.
void add_absent(char* name, char* date)
{
student *stu = search_student(name);
if ( stu )
{
absent *new_node = new absent();
Showing Page:
14/18
strcpy(new_node->date,date);
new_node->next = NULL;
absent **start = &stu->absents;
if(*start==NULL)
{
*start=new_node;
//current=new_node;
}
else
{
absent *temp = *start;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
}
}
// hw07 Q4: last_absent (15)
// In this function, you are passed the name of a student
to find the date of its last absent.
// Remember that absents are stored in chronological
order,
Showing Page:
15/18
// therefore the last checkup will be at the tail of the
linked list of absents.
// If the student has not yet had an anbsent added to its
list of absents, return NULL.
// The function search_student() is called before calling
this function,
// therefore you can assume that the student is not
already on the list.
char* last_absent(char* name)
{
student *stu = search_student(name);
if ( stu )
{
absent *temp = stu->absents;
if ( temp != NULL) {
while(temp->next != NULL)
{
temp = temp->next;
}
return temp->date;
}
return NULL;
}
}
Showing Page:
16/18
// hw07 Q5: remove_one (15)
// In this function, you are passed the name of a dog to
remove the corresponding dog from the list.
// The search function is called before this function so
you can assume that the dog is on the list.
// You will need to find the dog and remove it using
proper memory management to ensure no memory leaks.
void remove_one(char* name)
{
}
/// hw08 Q1: list_of_standard (25)
// This function is used to construct a new linked list of
containers from the global list of containers.
// The returned list should only contain students which
are of the standard type parameter (container->student-
>standard).
// No sorting is required for this list.
// The list that you return will be cleaned up for you by
the remove_all() function (see registration() function),
// however you will need to make sure that you leave no
dangling references (those cause memory leaks too).
// Notice that the returned list will need to contain all
student and checkup information to be displayed.
Showing Page:
17/18
// This function should NOT modify the global linked list.
struct container* list_of_standard(char* standard)
{
return NULL;
}
// hw08 Q2: list_by_name (25)
// This function is used to construct a new linked list of
containers from the global list of containers.
// The returned list should be sorted alphabetically by
each container\'s student\'s name (container->student-
>name).
// The list that you return will be cleaned up for you by
the remove_all() function (see registration() function),
// however you will need to make sure that you leave no
dangling references (those cause memory leaks too).
// Notice that the returned list will need to contain all
student and absence information to be displayed.
// You can again assume that for this assignment, no 2
students on the list will have the same name.
// You may want to use the function that you have written
above as a blueprint for this function.
// This function should NOT modify the global linked list.
struct container* list_by_name()
Showing Page:
18/18
{
return NULL;
}

Unformatted Attachment Preview

ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 Solution #include #include #include // used to create a linked list of absents containing \"dates\" typedef struct absent { char date[30]; struct absent *next; } absent; // used to hold student information and linked list of \"absents\" struct student { char name[30]; char standard[30]; struct absent *absents; }; // used to create a linked list of containers, each contaning a \"student\" struct container { struct student *student; struct container *next; } *list = NULL; // forward declaration of functions that have a lready been implemented void flush(); void branching(char); void registration(char); void remove_all(struct container*); void display(struct container*); // the following forward declarations are for functions that require implementation // return type // name and parameters // points void add_student(char*, char*); // 5 struct student* search_student(char*); // 5 void add_absent(char*, char*); // 10 char* last_absent(char*); // 15 void remove_one(char*); // 15 // Total: 50 points for hw07 struct container* list_of_standard(char*); // 25 struct container* list_by_name(); // 25 // Total: 50 points for hw08 int main() { char ch = \'i\'; printf(\"Student Information\ \ \"); do { printf(\"Please enter your selection:\ \"); printf(\"\\ta: add a new student to the list\ \"); printf(\"\\ts: search for a student on the list \ \"); printf(\"\\tr: remove a student from the list \ \"); printf(\"\\tc: add an absence date for a student \ \"); printf(\"\\tl: display last absence date for a student \ \"); printf(\"\\tn: display list of students by name \ \"); printf(\"\\tb: display list of students of a given standard \ \"); printf(\"\\tq: quit\ \"); ch = tolower(getchar()); flush(); branching(ch); } while (ch != \'q\'); remove_all(list); list = NULL; _CrtDumpMemoryLeaks(); // check for memory leaks (VS will let you know in output if they exist) return 0; } // consume leftover \'\ \' characters void flush() { int c; do c = getchar(); while (c != \'\ \' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case \'a\': case \'s\': case \'r\': case \'c\': case \'l\': case \'b\': case \'n\': registration(c); break; case \'q\': break; default: printf(\"Invalid input!\ \"); } } // This function will determine what info is needed and which function to send that info to. // It uses values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should trace the code and know how it works. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void registration(char c) { if (c == \'a\') { char input[100]; printf(\"\ Please enter the student\'s info in the following format:\ \"); printf(\"name:standard\ \"); fgets(input, sizeof(input), stdin); // discard \'\ \' chars attached to input input[strlen(input) - 1] = \'\\0\'; char* name = strtok(input, \":\"); // strtok used to parse string char* standard = strtok(NULL, \":\"); struct student* result = search_student(name); if (result == NULL) { add_student(name, standard); printf(\"\ Student added to list successfully\ \ \"); } else printf(\"\ That student is already on the list\ \ \"); } else if (c == \'s\' || c == \'r\' || c == \'c\' || c == \'l\') { char name[30]; printf(\"\ Please enter the student\'s name:\ \"); fgets(name, sizeof(name), stdin); // discard \'\ \' chars attached to input name[strlen(name) - 1] = \'\\0\'; struct student* result = search_student(name); if (result == NULL) printf(\"\ That student is not on the list\ \ \"); else if (c == \'s\') printf(\"\ Standard: %s\ \ \", result->standard); else if (c == \'r\') { remove_one(name); printf(\"\ Student removed from the list\ \ \"); } else if (c == \'c\') { char date[30]; printf(\"\ Please enter the date of absence:\ \"); fgets(date, sizeof(date), stdin); // discard \'\ \' chars attached to input date[strlen(date) - 1] = \'\\0\'; add_absent(name, date); printf(\"\ Absent date added\ \ \"); } else if (c == \'l\') { char* result = last_absent(name); if (result == NULL) printf(\"\ No absence documented.\ \ \"); else printf(\"\ Last absent on: %s\ \ \", result); } } else if (c == \'b\') { char standard[30]; printf(\"\ Please enter the standard:\ \"); fgets(standard, sizeof(standard), stdin); // discard \'\ \' chars attached to input standard[strlen(standard) - 1] = \'\\0\'; struct container* result = list_of_standard(standard); printf(\"\ List of students with standard %s:\ \ \", standard); display(result); remove_all(result); result = NULL; } else // c = \'n\' { struct container* result = list_by_name(); printf(\"\ List of students sorted by name:\ \ \"); display(result); remove_all(result); result = NULL; } } // This function recursively removes all students from the linked list of containers // Notice that all of the absents for all of the students must be removed as well void remove_all(struct container* students) { struct absent* temp; if (students != NULL) { remove_all(students->next); while (students->student->absents != NULL) { temp = students->student->absents; students->student->absents = students->student>absents->next; free(temp); } free(students->student); free(students); } } // This function prints the list of students in an organized format // It may be useful to trace this code before you get started void display(struct container* students) { struct container* container_traverser = stu dents; if (container_traverser == NULL) { printf(\"\ There are no students on this list! \ \ \"); return; } while (container_traverser != NULL) // traverse list of students { printf(\"Name: %s\ \", container_traverser->student>name); printf(\"Standard: %s\ \", container_traverser->student>standard); printf(\"Absence on file: \"); struct absent* ptr = container_traverser ->student>absents; if (ptr == NULL) { printf(\"No absence documented.\"); } else { while (ptr != NULL) // traverse list of absents { printf(\"\ %s\", ptr->date); ptr = ptr->next; } } printf(\"\ \ \"); // formatting container_traverser = container_traverser ->next; } } // hw07 Q1 : add (5 points) // This function should add student to the head of the list of containers. // The function search_student() is called before calling this function, // therefore you can assume that the student is not already on the list. void add_student(char* name, char* standard) { container *temp = new container(); temp->student = new student(); strcpy(temp->student->name,name); strcpy(temp->student->standard,standard); temp->next = list; list = temp; } // hw07 Q2 : search (5 points) // In this function, you are passed the name of a student to find his/her standard. // If the student exists on the list, return a pointer to the requested student. If not, return NULL. // (You must return a pointer to a node in your list. Do not create a pointer that just includes the standard) // (Remember that it is enough to sea rch for a student by only their name since no 2 students will have the same name) struct student* search_student(char* name) { container *temp = list; while( temp ) { if ( temp->student && strcmp (temp->student>name , name) == 0 ) { return temp->student; } temp = temp->next; } return NULL; } // hw07 Q3: add_absent (10) // In this function, you are passed the name of a student and a date of absence. // You should add the date to the tail of t he linked list of the students \"absents\". // You can assume that all absents will be added in chronological order. // The function search_student() is called before calling this function, // therefore you can assume that the student is already on the student list and // the new absence date is not on the absents list. void add_absent(char* name, char* date) { student *stu = search_student(name); if ( stu ) { absent *new_node = new absent(); strcpy(new_node->date,date); new_node->next = NULL; absent **start = &stu->absents; if(*start==NULL) { *start=new_node; //current=new_node; } else { absent *temp = *start; while(temp->next!=NULL) { temp = temp->next; } temp->next = new_node; } } } // hw07 Q4: last_absent (15) // In this function, you are passed the name of a student to find the date of its last absent. // Remember that absents are stored in chronological order, // therefore the last checkup will be at the tail of the linked list of absents. // If the student has not yet had an anbsent added to its list of absents, return NULL. // The function search_student() is called before calling this function, // therefore you can assume that th e student is not already on the list. char* last_absent(char* name) { student *stu = search_student(name); if ( stu ) { absent *temp = stu->absents; if ( temp != NULL) { while(temp->next != NULL) { temp = temp->next; } return temp->date; } return NULL; } } // hw07 Q5: remove_one (15) // In this function, you are passed the name of a dog to remove the corresponding dog from the list. // The search function is called before this function so you can assume that the dog is on the list. // You will need to find the dog and remove it using proper memory management to ensure no memory leaks. void remove_one(char* name) { } /// hw08 Q1: list_of_standard (25) // This function is used to construct a new linked list of containers from the global list of containers. // The returned list should only contain students which are of the standard type parameter (container ->student>standard). // No sorting is required for this list. // The list that you return will be cleaned up for you by the remove_all() function (see registration() function), // however you will need to make sure that you leave no dangling references (those cause memory leaks too). // Notice that the returned list will need to contain all student and checkup information to be displayed. // This function should NOT modify the global linked list. struct container* list_of_standard(char* standard) { return NULL; } // hw08 Q2: list_by_name (25) // This function is used to construct a new linked list of containers from the global list of containers. // The returned list should be sorted alphabetically by each container\'s student\'s name (container->student>name). // The list that you return will be cleaned up for you by the remove_all() function (see registration() function), // however you will need to make sure that you leave no dangling references (those cause memory leaks too). // Notice that the returned list will need to co ntain all student and absence information to be displayed. // You can again assume that for this assignment, no 2 students on the list will have the same name. // You may want to use the function that you have written above as a blueprint for this functi on. // This function should NOT modify the global linked list. struct container* list_by_name() { return NULL; } Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4