שאלה במדעי המחשב בנושא מבנה נתונים שרשרת חוליות מט...
שאלה במדעי המחשב בנושא מבנה נתונים שרשרת חוליות מטיפוס מורכב
שימו לב השאלה - השאלה מנוסחת בשפת c#!
השאלה:
מחלקת סטודנט Student בנויה בעזרת התכונות הבאות:
שם - name מטיפוס string
רשימת קורסים - courses מטיפוס חולייה מטיפוס Course.
המחלקה קורס Course בנויה בעזרת התכונות הבאות:
קוד הקורס - code מטיפוס string
ציון - grade n מטיפוס int.
מאגר ציונים Grades מוגדר כרשימה של תלמידים, כלומר כל חוליה היא מטיפוס Student.
א. בנו את כל המחלקות:
מחלקת מאגר ציונים - Grades: רשימה של תלמידים.
מחלקת תלמיד Student כמתואר לעיל.
מחלקת קורס - Course כמתואר לעיל.
יש לכתוב מחלקה מלאה (מחלקה הכוללת בנאים, מאחזרים, קובעים, פעולת תיאור ToString אשר יודעת להדפיס לכל תלמיד את הקורסים והציונים שלו.
ב. כיתבו פונקציה פנימית במחלקה Student אשר מחשבת ומחזירה את ממוצע הציונים עבור אותו תלמיד. יש לזכור כי ממוצע הוא מספר עשרוני.
ג. כתבו פעולה חיצונית המקבלת מאגר ציונים - כלומר עצם מטיפוס Grades ומדפיסה עבור כל תלמיד את ממוצע ציוניו.
ד. כיתבו פעולה חיצונית המקבלת מאגר ציונים, ומחזירה את קוד המקצוע שבו ממוצע הציונים הוא המקסימלי.
ה. על מנת לבדוק את עצמיכם, בנו את הפונקציות הבאות:
בתוך המחלקה Grades צרו פונקציה אשר מוסיפה תלמיד לרשימת התלמידים.
בתוך המחלקה Student צרו פונקציה אשר מוסיפה קורס לרשימת הקורסים.
צרו 4 תלמידים ו-8 קורסים: 2 קורסים לכל תלמיד.
צרו מופע מטיפוס Grade, הוסיפו את התלמידים.
הריצו את שתי הפונקציות ובידיוק את פתרונכם.
פתרון מלא:
המחלקה Course אשר כתובה בקובץ Course.cs
public class Course
{
private string code;
private int grade;
public Course(string code, int grade)
{
this.code = code;
this.grade = grade;
}
public string GetCode()
{
return code;
}
public void SetCode(string code)
{
this.code = code;
}
public int GetGrade()
{
return grade;
}
public void SetGrade(int grade)
{
this.grade = grade;
}
public override string ToString()
{
return "Code: " + code + "-> Grade: " + grade;
}
}
המחלקה Student אשר כתובה בקובץ Student.cs
public class Student
{
private string name;
private string id;
private int year;
private Node<Course> courses;
public Student(string name, string id, int year)
{
this.name = name;
this.id = id;
this.year = year;
this.courses = null; // Initialize courses to null
}
public void AddCourse(Course c)
{
courses = new Node<Course>(c,courses);
}
public string GetName()
{
return name;
}
public void SetName(string name)
{
this.name = name;
}
public string GetId()
{
return id;
}
public void SetId(string id)
{
this.id = id;
}
public int GetYear()
{
return year;
}
public void SetYear(int year)
{
this.year = year;
}
public Node<Course> GetCourses()
{
return courses;
}
public void SetCourses(Node<Course> courses)
{
this.courses = courses;
}
public double Avg()
{
Node<Course> p = courses;
int sum=0,counter=0;
while(p!=null)
{
int grade = p.GetValue().GetGrade();
sum+=grade;
counter++;
p=p.GetNext();
}
return (sum/(double)counter);
}
public string GetAllCoursesAsString()
{
string coursesString ="";
Node<Course> p = courses;
while (p != null)
{
coursesString+=p.GetValue().ToString();
if (p.GetNext() != null)
{
coursesString+=", ";
}
p = p.GetNext();
}
return coursesString;
}
public override string ToString()
{
return "Name: " + name + ", ID: " + id + ", Year: " + year+", Courses: "+GetAllCoursesAsString()+"\n";
}
}
המחלקה Grades אשר כתובה בקובץ Grades.cs
public class Grades
{
private Node<Student> students;
public Grades()
{
students = null;
}
public void AddStudent(Student s) //הוספת תלמיד
{
students = new Node<Student>(s, students);
}
public Node<Student> GetStudents()
{
return students;
}
public void SetStudents(Node<Student> students)
{
this.students = students;
}
public override string ToString()
{
string studentsString = "";
Node<Student> currentStudent = students;
while (currentStudent != null)
{
studentsString += currentStudent.GetValue().ToString();
if (currentStudent.GetNext() != null)
{
studentsString += ", ";
}
currentStudent = currentStudent.GetNext();
}
return studentsString;
}
}
המחלקה Main אשר כתובה בקובץ Main.cs
using System;
class HelloWorld {
// סעיף א
public static void AvgEachStudent(Grades grades)
{
Node<Student> p = grades.GetStudents();
while(p!=null)
{
Student s = p.GetValue();
Console.WriteLine("The avg of "+s.GetName()+" is "+ s.Avg());
p=p.GetNext();
}
}
//סעיף ב
public static string MaxCourseAvg(Grades grades)
{
Node<Student> pS = grades.GetStudents(); //pointer student
Node<string> codes =null;
Node<int> sums =null;
Node<int> counters = null;
Node<string> pCode =null; //pointer code courses
Node<int> pSum =null; //pointer sum courses
Node<int> pCounter = null; //pointer counter courses
while(pS!=null)
{
Student s = pS.GetValue();
Node<Course> pC = s.GetCourses();
while(pC!=null)
{
Course c = pC.GetValue();
string code = c.GetCode();
//make the pointer at the beggning
pCode=codes;
pSum=sums;
pCounter=counters;
// find the code in list of codes, or add at the begning if not found
while(pCode!=null && !pCode.GetValue().Equals(code) )
{
pCode=pCode.GetNext();
pSum=pSum.GetNext();
pCounter=pCounter.GetNext();
}
//not found
if(pCode==null)
{
//add at the begning
codes = new Node<string>(code,codes);
sums = new Node<int>(c.GetGrade(),sums);
counters = new Node<int>(1,counters);
}
else //found
{
//add values for avg
pSum.SetValue(pSum.GetValue()+c.GetGrade());// add grade to sum
pCounter.SetValue(pCounter.GetValue()+1); //add one to counter
}
pC=pC.GetNext();
}
pS=pS.GetNext();
}
//now we have all the sums and counters of all the courses.
//let's calculate the avg
pCode=codes;
pSum=sums;
pCounter=counters;
string maxCode = pCode.GetValue();
double maxAvg = pSum.GetValue()/(double)pCounter.GetValue();
while(pCode!=null)
{
double avg = pSum.GetValue()/(double)pCounter.GetValue();
if(avg>maxAvg)
{
maxCode = pCode.GetValue();
maxAvg = avg;
}
pCode=pCode.GetNext();
pSum=pSum.GetNext();
pCounter=pCounter.GetNext();
}
return maxCode;
}
static void Main() {
Grades grades = new Grades();
// Create 4 students
Student student1 = new Student("John", "S001", 2022);
Student student2 = new Student("Alice", "S002", 2023);
Student student3 = new Student("Bob", "S003", 2022);
Student student4 = new Student("Emily", "S004", 2023);
// Create 8 courses
Course course1 = new Course("C101", 85);
Course course2 = new Course("C102", 78);
Course course3 = new Course("C101", 90);
Course course4 = new Course("C102", 92);
Course course5 = new Course("C101", 80);
Course course6 = new Course("C102", 87);
Course course7 = new Course("C101", 88);
Course course8 = new Course("C102", 95);
student1.AddCourse(course1);
student1.AddCourse(course2);
grades.AddStudent(student1);
student2.AddCourse(course3);
student2.AddCourse(course4);
grades.AddStudent(student2);
student3.AddCourse(course5);
student4.AddCourse(course6);
grades.AddStudent(student3);
student4.AddCourse(course7);
student4.AddCourse(course8);
grades.AddStudent(student4);
Console.WriteLine(grades);
AvgEachStudent(grades); // סעיף א
Console.WriteLine(MaxCourseAvg(grades));
}
}
הערות: האסטרטגיה לסעיף ד:
נבנה שלוש שרשראות: אחד בשיבל קוד הקורס, השנייה בשביל סכום הציונים והשלשית סך הכל קורסים.
כל פעם שנסרוק קורס חדש, נחפש אותו בשרשרת קודי הקורס: אם מצאנו נעדכן את הערכים במקביל ברשימות האחרות, אם לא מצאנו סימן שנתקלנו בקורס זה בפעם הראשונה, לכן נוסיף אותו בתחילת הרשימות הנותרים.
לאחר מכן נוכל לסרוק את כל הרשימות ולחשב מי הממוצע הגבוה ביותר מבין כל הקורסים.
פלט של הרצה:
Name: Emily, ID: S004, Year: 2023, Courses: Code: C102-> Grade: 95, Code: C101-> Grade: 88, Code: C102-> Grade: 87
, Name: Bob, ID: S003, Year: 2022, Courses: Code: C101-> Grade: 80
, Name: Alice, ID: S002, Year: 2023, Courses: Code: C102-> Grade: 92, Code: C101-> Grade: 90
, Name: John, ID: S001, Year: 2022, Courses: Code: C102-> Grade: 78, Code: C101-> Grade: 85
The avg of Emily is 90
The avg of Bob is 80
The avg of Alice is 91
The avg of John is 81.5
C102