Cau2

 avatar
TalonEzio
c_cpp
2 years ago
2.7 kB
35
Indexable
Never
#include <iostream>
#include <string>
using namespace std;
struct NhanVien
{
    int MaNhanVien;
    string TenNhanVien;
    int Tuoi;
    float HeSoLuong;
};
struct Node
{
    NhanVien* data;
    Node* pNext;
};
struct SingleList
{
    Node* pHead;
    Node* pTail;
};
void init(SingleList*& List)
{
    List = new SingleList();
    List->pHead = List->pTail = NULL;
}
void CreateNhanVien(NhanVien*& NhanVien)
{
    cout << "Ma nhan vien: ";
    cin >> NhanVien->MaNhanVien;
    cin.ignore();
    cout << "Ten nhan vien: ";
    getline(cin,NhanVien->TenNhanVien);
    cout<<"Tuoi : ";
    cin>>NhanVien->Tuoi;
    cout<<"He so luong :";
    cin>>NhanVien->HeSoLuong;
}
Node* CreateNode()
{
    Node* pNode = new Node();
    pNode->data = new NhanVien;
    CreateNhanVien(pNode->data);
    pNode->pNext = NULL;
    return pNode;
}

void InsertLast(SingleList*& List)
{
    Node* pNode = CreateNode();
    if (List->pHead == NULL)
    {
        List->pHead = List->pTail = pNode;
    }
    else
    {
        List->pTail->pNext = pNode;
        List->pTail = pNode;
    }
}
void PrintNhanVien(NhanVien* NhanVien)
{
    cout << "Ma nhan vien : " << NhanVien->MaNhanVien << endl;
    cout << "Ten nhan vien : " << NhanVien->TenNhanVien << endl;
    cout << "Tuoi : " << NhanVien->Tuoi << endl;
    cout << "He so luong :" << NhanVien->HeSoLuong << endl;
}
void PrintList(SingleList* List)
{
    Node* pNode = List->pHead;
    cout << "Danh sach nhan vien:\n";
    while (pNode)
    {
        PrintNhanVien(pNode->data);
        pNode = pNode->pNext;
    }
}
void Cau3(SingleList* List)
{
    Node* pNode = List->pHead;
    while (pNode)
    {
        if(pNode->data->Tuoi >= 60)
        {
            PrintNhanVien(pNode->data);
        }
        pNode = pNode->pNext;
    }
}
void Cau4(SingleList* List)
{
    for(Node* pTemp1 = List->pHead;pTemp1->pNext;pTemp1 = pTemp1->pNext)
    {
        for(Node* pTemp2= pTemp1->pNext;pTemp2;pTemp2 = pTemp2->pNext)
        {
            if(pTemp1->data->HeSoLuong > pTemp2->data->HeSoLuong)
            {
                NhanVien *Temp = pTemp1->data;
                pTemp1->data = pTemp2->data;
                pTemp2->data = Temp;
            }
        }
    }
    cout<<"Danh sach sau khi sap xep he so luong giam dan:\n";
    PrintList(List);
}
int main()
{
    SingleList* List;
    init(List);
    int n;
    cout << "So luong nhan vien : "; cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cout << "nhan vien thu " << i <<":" << endl;
        InsertLast(List);
    }
    //cau 2
    PrintList(List);
    //cau 3
    Cau3(List);
    //Cau 4
    Cau4(List);
    return 0;
}