Question

Write a C++ program to store and update students' academic records in a binary search tree....

Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:

              1) Student name - the key field (string);

              2) Credits attempted (integer);

              3) Credits earned (integer);

              4) Grade point average - GPA (real).

             

              All student information is to be read from a text file. Each record in the file represents the grade information on a course attempted by a student and contains:

             

              1) Student name ;

              2) Course credits ( integer);

              3) Course grade (character).

              A file excerpt might look like:

                             Jones,Sue           3 B

                             Brown,Charlie   3 D

              This file contains no special end-of-data tag (flag) except for the usual end-of-file condition.

              In computing grade point averages, GPAs, you will need to compute the quality points associated with each grade according to the scale: A = 4, B = 3, C = 2, D = 1 and F = 0. No "+" of "-" grades are given, and no grades other that A, B, C, D, or F are given.

              Your program should read the student course grade records from the data file one at a time. For each record it should search the current binary search tree for a node with a name matching the student name it has just read. There are 2 cases to consider.

              1) The name is not found so this record corresponds to a new student.   A new node for this

                     student should be created and inserted into the binary search tree. The information fields of

                     this new node should be initialized as follows:

                            a) Student name = student name read from the file;

                             b) Credits attempted = course credits read from the file;

c) Credits earned = credits attempted if the course grade read from the file was not an F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           or 0 if the course grade read from the file was an F.

                            d) GPA = quality points associated with the grade read from the file.

             

               2) The name is found so this record corresponds to an update for a current student. The                                                                                                                                                                                                                                                 information fields in this student's node should be updated as follows:

a) New GPA = [(old GPA) * (old credits attempted) + (course credits) * (quality points

        of the grade)] / [(old credits attempted) + (course credits)]

                                                

                             b) Credits attempted = old credits attempted + course credits read from the file.

c) If the grade read from the file was not an F, credits earned = old credits earned +

       course credits read from the file; otherwise, credits earned remains the same.

              After your program has created and updated the binary search tree of student academic records, it                 must perform the following tasks:

              1) Print the students' academic records in alphabetical order according to student names;

              2) Find the student who has earned the most credits using a postorder traversal;

              3) Count both the number of students with GPAs under 2.5 using a single preorder traversal.

              Each of these tasks must be done using an appropriate recursive function for the tree traversal.

              In task #1, the student academic records must be printed in a table with the following headings:

                                                                STUDENT RECORDS

                                                                        CREDITS                 CREDITS

                             NAME                            ATTEMPTED            EARNED         GPA

GPAs must be printed with exactly 3 decimal places.

              In task #2, finding the student who has earned the most credits, your program must print the student names as it processes their nodes in the postorder traversal. Your program should then print the name, GPA, and number of credits earned of the student who has earned the most credits in the form:

               xxxxxxxxxxxxxxxxxxxx WITH A GPA OF x.xxx HAS EARNED THE MOST CREDITS, xx

              Your program may assume that the most number of credits earned is unique, i.e., no two students have earned this "maximum" number of credits. If it is not unique, your program can print either name. Finding the student who has earned the most number of credits requires you to use a pointer to the student node of the student who has the most credits so far in the traversal. This pointer is initialized to the root. As each node is processed, this pointer will change if the current student has earned a higher number of credits than the one pointed to by this pointer; otherwise it remains the same. When the processing ends, this pointer points to the node corresponding to the student who has earned the most credits.

              In task #3, your program must use a single preorder traversal to find the count. As your program does the preorder traversal, it must print each student name as the student's node is processed. The count should be printed in the following form:

                          xx STUDENTS HAD GPAS UNDER 2.50.

                            

             

Run your program using the attached file.

                                     Student.txt

Jones,Sue            3 B

Brown,Charlie     3 D

Smith,John           3 C

Smith,John          4 C

Bird,Sam             2 C

Bird,Sam             2 C

Smith,John          5 C

Jones,Sue             3 A

Moose,Manny      3 A

Bear,Ben              4 C

Bear,Ben              5 F

Bear,Ben              2 C

Brown,Charlie     3 C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdbool.h>

#include <conio.h>

#include <windows.h>

typedef struct{

char id;

char fname[30];

char lname[30];

char creditAtt[6];

char creditEarned[3];

char gpa[1];

}STUD;

typedef struct node *nd;

struct node

{

STUD stu;

nd left;

nd rigth;

}NODE;

void inputStud(nd *);

bool treeEmpty(nd *);

void deleteNode(nd *);

void editStud(nd *);

void viewAll(nd);

nd searchNode(nd *,nd *,char[]);

int main(void)

{

int ans,c;

bool e;

nd root = NULL;

do

{

system("cls");

printf("1] Add New Student Record\n");

printf("2] Edit Student Record\n");

printf("3] View Specific Record\n");

printf("4] Display All\n");

printf("5] Delete Student Record\n");

printf("6] Exit\n");

printf("Pick your choice: ");

scanf("%d",&ans);

switch(ans)

{

case 1: inputStud(&root);

break;

case 2: editStud(&root);

break;

case 3: viewAll(root);

getch();

break;

case 4: viewAll(root);

getch();

break;

case 5: deleteNode(&root);

break;

case 6: printf("\n\nclosing the program in 5 seconds....");

sleep(5000);

}

}while (ans != 6);

return 0;

}

void inputStud(nd *root)

{

nd t, t1, temp;

char creAtt[6];

char lnam[20];

char fnam[30];

char creEar[3];

char gpa[1];

STUD st;

ifstream file;

file.open("StudentRecord.txt");

if (!file) // Validation statement

{

cout << "Can't open input file " << endl;

}

for(int x = 0;x <= filecheck; x++)

{

id = x+1;

file >> fnam >> lnam >> creAtt >> creEar;

}

strcpy(st.creditAtt, creAtt);

strcpy(st.lname, lnam);

strcpy(st.fname, fnam);

strcpy(st.creditEarned, creEar);

st.gpa=gpa;

temp = malloc(sizeof(NODE));

temp -> stu = st;

temp -> left = NULL;

temp -> rigth = NULL;

if (*root == NULL)

{

*root = temp;

}

else

{

t = *root;

while (t != NULL)

{

t1 = t;

if (strcmp(temp -> stu.creditAtt,t -> stu.creditAtt)==0)

t = t -> left;

else

t = t -> rigth;

}

if (strcmp(temp -> stu.creditAtt,t1 -> stu.creditAtt) < 0)

t1 -> left = temp;

else

t1 -> rigth = temp;

}

t1 = NULL;

t = NULL;

temp = NULL;

return;

}

void editStud(nd *root)

{

int id;

char creAtt[6];

char lnam[20];

char fnam[30];

char creEar[3];

float gpa;

STUD st;

  

printf("Please enter the ID of the Student you would like to edit: ");

gets(id);

  

if(strcmp(id,st.id)==0)

{

printf("Enter Student Credit Earned: /n");

gets(creEar);

printf("Enter Student's Last Name: /n");

gets(lnam);

printf("Enter Student's First Name: /n");

gets(fnam);

printf("Enter Student's Credit earned: /n");

gets(creAtt);

printf("Enter Student's gpa: /n");

scanf("%f", &gpa);

strcpy(st.creditAtt, creAtt);

strcpy(st.lname, lnam);

strcpy(st.fname, fnam);

strcpy(st.creditEarned, creEar);

st.gpa=gpa;

}

else

{

printf("Student not found..");

}

return;

}   

  

void deleteNode(nd *root)

{

char iD[6];

bool e,lmoved = false;

nd t,t1,t2;

STUD st;

e = treeEmpty(root);

if (e)

{

printf("\n\nSorry, the tree is empty...");

sleep(3000);

}

else

{

printf("Please enter ID of student to be deleted: ");

gets(iD);

t = searchNode(root,&t1,iD[]);

if (t == NULL)

{

printf("\n\n%s not found...",iD);

sleep(2000);

}

else

{

if ((t -> left == NULL)&&(t -> rigth == NULL))

{

if (strcmp( iD, t1 -> stu.creditAtt) <0)

t1 -> left = NULL;

else

t1 -> rigth = NULL;

}

else if ((t -> left == NULL)&&(t -> rigth != NULL))

{

  

if (strcmp( iD,t1 -> stu.creditAtt) <0)

t1 -> left = t -> rigth;

else

t1 -> rigth = t -> rigth;

}

else if ((t -> left != NULL)&&(t -> rigth == NULL))

{

if (strcmp( iD,t1 -> stu.creditAtt) <0)

t1 -> left = t -> left;

else

t1 -> rigth = t -> left;

}

else

{

t1 = t;

t = t1 -> rigth;

while(t -> left != NULL)

{

t2 = t;

t = t -> left;

lmoved = true;

}

t1 -> stu.creditAtt = t -> stu.creditAtt;

if (t -> rigth == NULL)

{

if (lmoved)

t2 -> left = NULL;

else

t1 -> rigth = NULL;

}

else

{

if (lmoved)

t2 -> left = t -> rigth;

else

t1 -> rigth = t -> rigth;

}

}

free(t);

t1 = NULL;

t2 = NULL;

}

}

return;

}

nd searchNode(nd *r,nd *t1,char iD[], nd root)

{

nd t;

t = *r;

STUD st;

while((t != NULL) && (strcmp(t -> stu.creditAtt, iD)== 0)

{

*t1 = t;

if ((strcmp( iD,t1 -> stu.creditAtt) <0)

t = t -> left;

else

t = t -> rigth;

}

return t;

}

bool treeEmpty(nd *r)

{

bool e = false;

if (*r == NULL)

e = true;

return e;

}

void viewAll(nd ptr)

{

if (ptr != NULL)

{

printf("Student Attempted: %s", stu.creditAtt );

printf("First Name: %s", stu.fName);

printf("Credit Earned: %s", stu.creditEarned);

printf("Last Name: %s", stu.lName);

printf("gpa: %f", stu.gpa);

}

return;

}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to store and update students' academic records in a binary search tree....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT