Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
4.3 kB
2
Indexable
Never
#include <iostream>
#include <cstring>
#include <algorithm>
#include <time.h>
using namespace std;

struct congViec {
    int maSo;
    char* tenCongViec = new char[20];
    int thoiGian;
    char* nguoiThucHien = new char[30];
    int doUuTien;
};
void Xuat(congViec a) {
    cout << "===================================\n"; 
    cout << a.maSo << endl;
    cout << a.tenCongViec << endl;
    cout << a.thoiGian << endl;
    cout << a.nguoiThucHien << endl;
    cout << a.doUuTien << endl;
}
struct node {
    congViec data;
    node* next;
};
struct Stack {
    node* head;
};
Stack createStack(Stack& a) {
    a.head = NULL;
    return a;
}
Stack push(Stack& a, congViec& x) {
    node* tmp = new node;
    tmp->data = x;
    tmp->next = a.head;
    a.head = tmp;
    return a;
}
Stack pop(Stack& a) {
    if (a.head) return a;
    node* tmp = a.head;
    a.head = a.head->next;
    delete tmp;
    return a;
}
congViec top(Stack& a) {
    return a.head->data;
}
Stack KhoiTaoNgauNhien(Stack& a) {
    char Ten[20][20]{ "Giang","Ky","Phi","Tan","Van","Vuong","Cong","Hien","Linh","Ngoc",
                    "Huy","Khang","Bao","Minh","Phuc","Anh","Khoa","Phat","Vinh","Khoi" };
    char Ho[20][20] = { " NGUYEN"," TRAN"," LE"," PHAM"," HOANG"," VO"," PHAN"," TRUONG"," BUI"," DANG",
                    " DO"," NGO"," HO"," DUONG"," DINH"," DOAN"," LAM"," MAI"," TRINH"," DAO" };
    char Job[10][20] = { "Cau ca","Di boi","Lao cong","Cong nhan","Nhan vien",
                    "Ky su","Giao vien","Dau bep","Bao ve","Boi ban" };
    congViec tmp;
    tmp.maSo = rand();
    strcpy(tmp.tenCongViec,strcat(Job[rand() % 10],"\0"));
    tmp.thoiGian = rand() % 180 + 180;
    strcpy(tmp.nguoiThucHien,strcat(strcat(Ten[rand() % 20], Ho[rand() % 20]),"\0"));
    tmp.doUuTien = rand() % 10 + 1;
    a = push(a, tmp);
    return a;
}
// bool sosanh1(congViec a,congViec b){
//     return a.thoiGian>b.thoiGian;
// }
// congViec timCaoNhat(Stack& a){
//     node* p=a.head;
//     congViec tmp[100];
//     int n=0;
//     while(p->next!=NULL){
//         tmp[n]=p->data;
//         p=p->next;
//         n++;
//     }
//     sort(tmp,tmp+n,sosanh1);
//     if(tmp[0].thoiGian==tmp[1].thoiGian){
//         congViec ngoaiLe;
//         ngoaiLe.maSo=0;
//         char tmp1[20]="khong co";
//         ngoaiLe.tenCongViec=tmp1;
//         ngoaiLe.thoiGian=0;
//         char tmp2[20]="dung NGUYEN";
//         ngoaiLe.nguoiThucHien=tmp2;
//         return ngoaiLe;
//     }
//     return tmp[0];
// }
// Stack capNhap300(Stack& a){
//     node* p=a.head;
//     while(p->next!=NULL){
//         if(p->data.thoiGian>300){
//             p->data.thoiGian=300;
//         }
//     }
//     return a;
// }
Stack sort(Stack& a){
    for(node* i=a.head;i!=NULL;i=i->next){
         for(node* j=i->next;j!=NULL;j=j->next){
             if(i->data.doUuTien<j->data.doUuTien) swap(i->data,j->data);
        }
     }
    return a;
}

void swapData(congViec& a, congViec &b){
    congViec temp;
    temp.maSo = a.maSo;
    temp.tenCongViec = a.tenCongViec;
    temp.thoiGian = a.thoiGian;
    temp.nguoiThucHien = a.nguoiThucHien;
    temp.doUuTien = a.doUuTien;

    a.maSo = b.maSo;
    a.tenCongViec = b.tenCongViec;
    a.thoiGian = b.thoiGian;
    a.nguoiThucHien = b.nguoiThucHien;
    a.doUuTien = b.doUuTien;

    b.maSo = temp.maSo;
    b.tenCongViec = temp.tenCongViec;
    b.thoiGian = temp.thoiGian;
    b.nguoiThucHien = temp.nguoiThucHien;
    b.doUuTien = temp.doUuTien;
}


void isort(Stack& a){
    for(node* i = a.head; i!=NULL; i=i->next){
        for(node* j=i->next; j!=NULL; j=j->next){
            if((i->data).doUuTien<(j->data).doUuTien){
                swapData(i->data,j->data);//hoán vị a[i] và a[j]
            }
        }
    }
}


void printStack(Stack a) {
    node* p = a.head;
    while (p != NULL) {
        Xuat(p->data);
        p = p->next;
        cout << "--------------" << endl;
    }
}
int main() {
    srand(time(0));
    Stack a;
    createStack(a);
    for(int i =0;i<5;i++){
        KhoiTaoNgauNhien(a);
    };
    isort(a);
    printStack(a);
    return 0;
}