Untitled
unknown
plain_text
2 years ago
5.6 kB
3
Indexable
#include <iostream> using namespace std; struct subject; struct SJ_Node; struct ListSubjects; struct subject{ string ID_SJ; string name_SJ; int number_credit_SJ; float GK, CK; float Process_weight; // Trong so qua trinh float FinalExam_weight; // Trong so cuoi ki subject(string ID, string name, int credit, float process, float final, int gk, int ck): ID_SJ(ID), name_SJ(name), number_credit_SJ(credit), Process_weight(process), FinalExam_weight(final), GK(gk), CK(ck){}; }; struct SJ_Node{ subject SJ_infor; SJ_Node* prev; SJ_Node* next; SJ_Node (subject sj): SJ_infor(sj){}; }; struct ListSubjects{ SJ_Node* Head; ListSubjects () : Head(NULL) {}; // Bo sung mon hoc cho sinh vien void InsertAfter(string ID, subject sj){ SJ_Node* run = Head; while (run){ if (run->SJ_infor.ID_SJ == ID){ SJ_Node* newNode = new SJ_Node(sj); newNode->next = run->next; run->next = newNode; return; } run = run->next; } // If the list is empty or the ID is not found, create a new node at the beginning SJ_Node* newNode = new SJ_Node(sj); newNode->next = Head; Head = newNode; } }; struct student; struct Student_Node; struct ListStudents; struct student{ long int ID_Student; string name_Student; string class_Student; ListSubjects List_Subject_ofStudent; student(long int ID, string name, string classs, ListSubjects listSJ): ID_Student(ID), name_Student(name), class_Student(classs), List_Subject_ofStudent(listSJ){}; }; struct Student_Node{ student Student_infor; Student_Node* prev; Student_Node* next; Student_Node (student sv): Student_infor(sv) {}; }; struct ListStudents{ Student_Node* Head; ListStudents (): Head(NULL) {}; bool IsEmpty(){ cout << "empty" << endl; return Head == NULL; } // Bo sung sinh vien vao dau danh sach void InsertBegin(student sv){ Student_Node* newNode = new Student_Node(sv); if (!Head){ Head = newNode; }else{ newNode->next = Head; Head->prev = newNode; Head = newNode; } } // Bo sung sinh vien vao sau sinh vien co mssv cho truoc void InsertAfter(int ID, student sv) { if (IsEmpty()) return; Student_Node* run = Head; while (run != NULL) { if (run->Student_infor.ID_Student == ID){ Student_Node* newNode = new Student_Node(sv); newNode->next = run->next; run->next = newNode; return; } run = run->next; } } // Tim kiem sinh vien theo mssv cho truoc Student_Node* SearchByID(int ID){ Student_Node* run = Head; if (IsEmpty()) return NULL; while(run != NULL){ if (run->Student_infor.ID_Student == ID){ return run; } run = run->next; } cout << "No exists ID" << ID << endl; return NULL; } // Tinh diem tich luy tat ca cac sinh vien trong danh sach float CalculateAll(){ if (IsEmpty()) return -1; float mark = 0; Student_Node* run = Head; while (run != NULL) { mark += CalculateByID(run->Student_infor.ID_Student); run = run->next; } return mark; } // Tinh diem tich luy cua mot sinh vien cho truoc float CalculateByID(int ID){ if (IsEmpty()) return -1; Student_Node* run = Head; while(run != NULL){ if (run->Student_infor.ID_Student == ID){ ListSubjects list = run->Student_infor.List_Subject_ofStudent; SJ_Node* run = list.Head; float mark = 0; int cntSJ = 0; while (run){ float GK = run->SJ_infor.GK * run->SJ_infor.Process_weight; float CK = run->SJ_infor.CK * run->SJ_infor.FinalExam_weight; mark += GK + CK; cntSJ++; run = run->next; } float res = mark/cntSJ; return res; } run = run->next; } cout << "No exists ID" << ID << endl; return -1; } }; int main(){ // Create a list of subjects ListSubjects subjectsList; subjectsList.InsertAfter("1", subject("S1", "Math", 3, 0.3, 0.7, 8, 9)); subjectsList.InsertAfter("2", subject("S2", "Physics", 4, 0.4, 0.6, 7, 8)); subjectsList.InsertAfter("3", subject("S3", "Chemistry", 3, 0.3, 0.7, 9, 7)); // Create a list of students ListStudents studentsList; studentsList.InsertBegin(student(1, "John", "ClassA", subjectsList)); studentsList.InsertBegin(student(2, "Jane", "ClassB", subjectsList)); studentsList.InsertBegin(student(3, "Bob", "ClassC", subjectsList)); // Calculate and display the total marks for all students float totalMarks = studentsList.CalculateAll(); cout << "Total Marks for all students: " << totalMarks << endl; // Calculate and display the average marks for a specific student (e.g., ID 1) int studentID = 1; float averageMarks = studentsList.CalculateByID(studentID); if (averageMarks != -1) { cout << "Average Marks for Student ID " << studentID << ": " << averageMarks << endl; } return 0; }
Editor is loading...