Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
7.9 kB
3
Indexable
Never
#include <bits/stdc++.h>
//#include <iostream>
//#include <string>
//#include "preheader.h"
bool stop = false;`
using namespace std;

class StudentNode {
public:
    string MSSV;
    string TEN;
    float DTB;
    StudentNode* next;
    StudentNode* prev;
};

void swapNodes(StudentNode*& head, StudentNode* x, StudentNode* y) {
    if (x == y) return;
    if (x->prev) x->prev->next = y;
    else head = y;
    if (y->next) y->next->prev = x;
    x->next->prev = y;
    y->prev->next = x;
    swap(x->prev, y->prev);
    swap(x->next, y->next);
}


void displayStudents(StudentNode* head) {
    if (head == nullptr) {
        cout << "Danh sach khong co sinh vien.\n";
        return;
    }
    cout << "Danh sach sinh vien:\n";
    for (StudentNode* p = head; p != nullptr; p = p->next) {
        cout << "Ten sinh vien: " << p->TEN << " - MSSV: " << p->MSSV << " - DTB: " << p->DTB << endl;
    }
}


void addStudent(StudentNode* &head) {
    while (true) {
        StudentNode* newNode = new StudentNode;
        cout << "Nhap ten sinh vien (nhap khoang trang de ket thuc): ";
        getline(cin, newNode->TEN);
        if (newNode->TEN.empty()) {
            delete newNode;
            break;
        }
        cout << "Nhap MSSV: ";
        getline(cin, newNode->MSSV);

        //StudentNode* currNode = head;


        cout << "Nhap diem trung binh: ";
        cin >> newNode->DTB;
        cin.ignore(); 

        newNode->next = head;
        newNode->prev = NULL;
        if (head != NULL) {
            head->prev = newNode;
        }
        head = newNode;
        cout << "Da them sinh vien " << newNode->TEN << " vao danh sach.\n";
    }
}




void searchStudent(StudentNode* head) {
    string name;
    cout << "Nhap ten sinh vien can tim: ";
    getline(cin, name);
    StudentNode* currNode = head;
    while (currNode != NULL) {
        if (currNode->TEN == name) {
            cout << "Sinh vien " << currNode->TEN << " co MSSV la " << currNode->MSSV << " va diem TB la " << currNode->DTB << endl;
            return;
        }
        currNode = currNode->next;
    }
    cout << "Khong tim thay sinh vien co ten la " << name << endl;
}



void removeStudent(StudentNode* &head) {
    string mssv;
    cout << "Nhap MSSV cua sinh vien can xoa: ";
    getline(cin, mssv);
    StudentNode* currNode = head;
    while (currNode != NULL) {
        if (currNode->MSSV == mssv) {
            cout << "Da xoa sinh vien " << currNode->TEN << " khoi danh sach.\n";
            if (currNode->prev != NULL) {
                currNode->prev->next = currNode->next;
            }
            else {
                head = currNode->next;
            }
            if (currNode->next != NULL) {
                currNode->next->prev = currNode->prev;
            }
            delete currNode;
            return;
        }
        currNode = currNode->next;
    }
    cout << "Khong tim thay sinh vien co MSSV " << mssv << " trong danh sach.\n";
}

void listStudents(StudentNode* head) {
    cout << "Danh sach sinh vien co diem TB >= 5.0:\n";
    StudentNode* currNode = head;
    while (currNode != NULL) {
        if (currNode->DTB >= 5.0) {
            cout << "  " << currNode->TEN << " (MSSV: " << currNode->MSSV << ", diem TB: " << currNode->DTB << ")\n";
        }
        currNode = currNode->next;
    }
}

void ratingStudents(StudentNode* head) {
    cout << "Xep loai sinh vien:\n";
    StudentNode* current = head;
    while (current != nullptr) {
        string rating;
        if (current->DTB >= 9.0) {
            rating = "Xuat sac";
        }
        else if (current->DTB >= 8.0) {
            rating = "Gioi";
        }
        else if (current->DTB >= 7.0) {
            rating = "Kha";
        }
        else if (current->DTB >= 6.5) {
            rating = "Trung binh kha";
        }
        else if (current->DTB >= 5.0) {
            rating = "Trung binh";
        }
        else {
            rating = "Yeu";
        }
        cout << "  " << current->TEN << " (MSSV: " << current->MSSV << ", xep loai: " << rating << ")\n";
        current = current->next;
    }
}

void sortStudents(StudentNode*& head) {
    if (!head || !head->next) {
        cout << "Danh sach sinh vien da duoc sap xep theo diem TB tang dan.\n";
        return;
    }

    StudentNode* i = head->next;

    while (i) {
        float key = i->DTB;
        string keyMSSV = i->MSSV;
        string keyTEN = i->TEN;
        StudentNode* j = i->prev;

        while (j && j->DTB > key) {
            j->next->prev = j->prev;
            if (j->prev) {
                j->prev->next = j->next;
            } else {
                head = j->next;
            }
            j->prev = j->next;
            j->next = j->next->next;
            j->prev->next = j;
            if (j->next) {
                j->next->prev = j;
            }
            j = j->prev;
        }
        i->DTB = key;
        i->MSSV = keyMSSV;
        i->TEN = keyTEN;
        i = i->next;
    }

    cout << "Danh sach sinh vien da duoc sap xep theo diem TB tang dan.\n";
}


void insertStudent(StudentNode*& head) {
    StudentNode* newNode = new StudentNode();
    cout << "Nhap ten sinh vien: ";
    getline(cin, newNode->TEN);
    cout << "Nhap MSSV: ";
    getline(cin, newNode->MSSV);
    cout << "Nhap diem trung binh: ";
    cin >> newNode->DTB;

    cin.ignore(); 

    if (head == nullptr || head->DTB >= newNode->DTB) {
        newNode->next = head;
        newNode->prev = nullptr;
        if (head != nullptr) {
            head->prev = newNode;
        }
        head = newNode;
    }
    else {
        StudentNode* current = head;
        while (current->next != nullptr && current->next->DTB < newNode->DTB) {
            current = current->next;
        }
        newNode->next = current->next;
        newNode->prev = current;
        if (current->next != nullptr) {
            current->next->prev = newNode;
        }
        current->next = newNode;
    }
    sortStudents(head);

    cout << "Da chen sinh vien " << newNode->TEN << " vao danh sach.\n";
}


int main() {
    StudentNode* head = nullptr;
    int choice;
    do {
        cout << "-------MENU-------\n";
        cout << "1. Hien thi danh sach sinh vien\n";
        cout << "2. Nhap danh sach sinh vien\n";
        cout << "3. Tim sinh vien trong danh sach\n";
        cout << "4. Xoa sinh vien khoi danh sach\n";
        cout << "5. Liet ke sinh vien co diem TB >= 5.0\n";
        cout << "6. Xep loai va in ra thong tin cua tung sinh vien\n";
        cout << "7. Sap xep danh sach sinh vien theo diem TB tang dan\n";
        cout << "8. Chen sinh vien vao danh sach theo diem TB tang dan\n";
        cout << "9. Thoat chuong trinh\n";
        cout << "Chon mot chuc nang (1-9): ";
        cin >> choice;
        cin.ignore(); 

        switch (choice) {
        case 1:
            displayStudents(head);
            break;
        case 2:
            addStudent(head);
            break;
        case 3:
            searchStudent(head);
            break;
        case 4:
            removeStudent(head);
            break;
        case 5:
            listStudents(head);
            break;
        case 6:
            ratingStudents(head);
            break;
        case 7:
            sortStudents(head);
            break;
        case 8:
            insertStudent(head);
            break;
        case 9:
            cout << "Chuong trinh da thoat.\n";
            break;
        default:
            cout << "Khong co chuc nang tuong ung.\n";
            break;
        }
        cout << endl;
    } while (choice != 9);

    return 0;
}