Respuesta :
Answer:
#include<iostream>
#include<string.h>
using namespace std;
class gradebook;
class student
{
friend class gradebook;
char name[20]; // std name
int homework_grades[6], quiz_grades[6], exam_grades[4]; //grades array
float homework_avg, quiz_avg, exam_avg, final_avg;
};
class gradebook
{
student list[16];
public:
char gb_name[20]; // gradebook name
int max, total;
// Constructor
gradebook()
{
strcpy(gb_name,"noname");
max = 5;
total = 0;
for(int i=1;i<=15;i++)
{
strcpy(list[i].name,"noname");
for(int j=1;j<=5;j++)
{
list[i].homework_grades[j] = -1;
list[i].quiz_grades[j] = -1;
}
list[i].exam_grades[1] = list[i].exam_grades[2] = list[i].exam_grades[3] = -1;
}
}
// Member functions
void display_std_name();
void display_grades(int, int);
int add_std(char *new_name);
int remove(int);
//int ch_grade(int, int);
};
// Define all functions
// Remove a student from list and adjust remaining
int gradebook::remove(int std_id)
{
if(total==0 || std_id>total || std_id<=0)
return 0;
if(std_id==total)
{
cout<<"\n“"<<list[std_id].name<<"” has been successfully removed! (New class size: 2)";
strcpy(list[total--].name,"noname");
return 1;
}
cout<<"\n“"<<list[std_id].name<<"” has been successfully removed! (New class size: 2)";
for(int i=std_id; i<total; i++)
{
list[i]=list[i+1];
}
total--;
return 1;
}
// Add student to gradebook
int gradebook::add_std(char *new_name)
{
if(total<15)
{
strcpy(list[++total].name, new_name);
return 1;
}
return 0;
}
// Display student name list
void gradebook::display_std_name()
{
for(int i=1;i<=total;i++)
{
cout<<"\n"<<i<<" "<<list[i].name;
}
}
/* Display grades of students
// (grade_id, student_number_in_list)
homework(1), quiz(2), exam(3)
*/
void gradebook::display_grades(int g_id, int std_id)
{
int i;
if(std_id<=total)
{
if(g_id==1)
{
for(i=1;i<6;i++)
{
cout<<"\n"<<i<<". ";
if(list[std_id].homework_grades[i]==-1) cout<<"<ungraded>";
}
}
else if(g_id==2)
{
for(i=1;i<6;i++)
{
cout<<"\n"<<i<<". ";
if(list[std_id].quiz_grades[i]==-1) cout<<"<ungraded>";
}
}
else if(g_id==3)
{
for(i=1;i<4;i++)
{
cout<<"\n"<<i<<". ";
if(list[std_id].exam_grades[i]==-1) cout<<"<ungraded>";
}
}
}
else
cout<<"\nNo such std exists";
}
/****************************** MAIN ****************************/
int main()
{
int main_choice, grade_choice, std_id, new_grade;
char g_name[20], s_name[20];
float a,b,c;
gradebook g;
do
{
cout<<"\n\n-=| MAIN MENU |=-";
cout<<"\n1. Add a student\n2. Remove a student\n3. Change a student’s grade\n4. Change group weights\n5. Change the gradebook name\n6. Display class averages\n7. Display full report\n0. QUIT\n";
cout<<"\nEnter an action: ";
cin>>main_choice;
switch(main_choice)
{
case 0:
break;
case 1:
cout<<"\n-=| ADDING STUDENT |=-";
cout<<"\nPlease enter the student’s name: ";
cin>>s_name;
if(g.add_std(s_name))
cout<<"\n"<<s_name<<" was successfully added to the gradebook!";
else
cout<<"\nStudents cannot be added because the gradebook is full!";
break;
case 2:
cout<<"\n-=| REMOVING STUDENT |=-";
if(g.total!=0)
{
g.display_std_name();
cout<<"\n Enter student to remove: ";
cin>>std_id;
if(!g.remove(std_id))
cout<<"\nCannot remove students";
}
else
cout<<"\nStudents cannot be added because the gradebook is full!";
break;
case 3:
cout<<"\n-=| CHANGING GRADE |=-\n1. Change a homework grade\n2. Change a quiz grade\n3. Change an exam grade";
cout<<"\nWhat type of grade would you like to change: ";
cin>>grade_choice;
g.display_std_name();
cout<<"\nWhich student’s grade would you like to change? ";
cin>>std_id;
//ch_grade(grade_choice, std_id);
g.display_grades(grade_choice, std_id);
break;
/*
case 4:
cout<<"\n-=| CHANGING WEIGHTS |=-";
while(1)
{
cout<<"\nEnter the weights, separated by spaces, in the order of homework, quizzes, and exams (total must add up to 1.0):";
cin>>a>>b>>c;
if(a+b+c==1.00)
break;
else
cout<<"\n Weights do not add up to 1.0, try again...";
}
break;
case 5:
cout<<"\n-=| CHANGING NAME |=-\nPlease enter the new name for the gradebook: ";
cin>>g_name;
cout<<"\n Gradebook name changed from “ ” to "<<g_name;
break;
case 6:
cout<<"\n-=| CLASS AVERAGES |=-";
cout<<"\nHomework average of class: ";
cout<<"\nQuiz average of class: ";
cout<<"\nExam average of class: ";
cout<<"\nFinal average of class: ";
break;
case 7:
break;
*/
default:
cout<<"\n\nInvalid input\n\n";
}
}while(main_choice!=0);
return 0;
}
Explanation: