Untitled

 avatar
unknown
plain_text
5 months ago
4.8 kB
3
Indexable
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>

using namespace std;

struct HangHoa
{
    string maHang;
    string tenHang;
    string donViTinh;
    int donGia;
    int soLuong;
};

struct Node
{
    HangHoa infor;
    Node *next;
};

typedef Node *tro;

void nhapHH(HangHoa &hh)
{
    cout << "Nhap ma\n";
    getline(cin, hh.maHang);
    cout << "Nhap ten hang\n";
    getline(cin, hh.tenHang);
    cout << "Nhap don vi tinh\n";
    getline(cin, hh.donViTinh);
    cout << "Nhap don gia\n";
    cin >> hh.donGia;
    cout << "Nhap so luong\n";
    cin >> hh.soLuong;
    cin.ignore();
}

void input_List(tro &L)
{
    tro P, Q;
    HangHoa x;
    int d = 0;
    L = NULL;
    for (int i = 0; i < 3; i++)
    {
        nhapHH(x);
        P = new Node;
        P->infor = x;
        P->next = NULL;
        if (L == NULL)
        {
            L = P;
        }
        else
        {
            Q->next = P;
        }
        Q = P;
        d++;
    }
}

void hienThiDS(tro L)
{
    if (L == NULL)
    {
        cout << "Danh sach rong\n";
        return;
    }
    cout << setw(5) << "STT";
    cout << setw(15) << "Ma Hang";
    cout << setw(30) << "Ten Hang";
    cout << setw(10) << "Don Vi Tinh";
    cout << setw(10) << "Don Gia";
    cout << setw(10) << "So Luong";
    cout << setw(10) << "Thanh Tien" << endl;
    tro Q = L;
    int i = 0;
    while (Q != NULL)
    {
        HangHoa x = Q->infor;
        cout << setw(5) << i + 1;
        cout << setw(15) << x.maHang;
        cout << setw(30) << x.tenHang;
        cout << setw(10) << x.donViTinh;
        cout << setw(10) << x.donGia;
        cout << setw(10) << x.soLuong;
        cout << setw(10) << x.donGia * x.soLuong << endl;
        Q = Q->next;
        ++i;
    }
}

int length(tro L)
{
    int n = 0;
    tro Q = L;
    while (Q != NULL)
    {
        Q = Q->next;
        ++n;
    }
    return n;
}

void firstAdd(tro &L, HangHoa x)
{
    tro P;
    P = new Node;
    P->infor = x;
    P->next = L;
    L = P;
}

void removeIndex(tro &L, int k)
{
    tro Q = L;
    int d = 1;
    while (d < k - 1)
    {
        Q = Q->next;
        ++d;
    }
    tro M = Q->next;
    Q->next = M->next;
    delete (M);
}
tro timMax(tro L)
{
    tro P = L;
    tro Q = L->next;
    while (Q != NULL)
    {
        if (Q->infor.soLuong > P->infor.soLuong)
        {
            P = Q;
        }
        Q = Q->next;
    }
    return P;
}

void removeMax(tro &L)
{
    tro max = timMax(L);
    tro Q = L;
    while (Q->next != max)
        Q = Q->next;
    Q->next = max->next;
    delete (max);
}

void timSP(tro L)
{
    tro Q = L;
    cout << setw(5) << "STT";
    cout << setw(15) << "Ma Hang";
    cout << setw(30) << "Ten Hang";
    cout << setw(10) << "Don Vi Tinh";
    cout << setw(10) << "Don Gia";
    cout << setw(10) << "So Luong";
    cout << setw(10) << "Thanh Tien" << endl;
    int i = 0;
    while (Q != NULL)
    {
        if (Q->infor.soLuong >= 20)
        {
            HangHoa x = Q->infor;
            cout << setw(5) << i + 1;
            cout << setw(15) << x.maHang;
            cout << setw(30) << x.tenHang;
            cout << setw(10) << x.donViTinh;
            cout << setw(10) << x.donGia;
            cout << setw(10) << x.soLuong;
            cout << setw(10) << x.donGia * x.soLuong << endl;
        }
        ++i;
        Q = Q->next;
    }
}

void sapXep(tro &L)
{
    tro Q, R;
    tro P = L;
    while (P->next != NULL)
    {
        R = P;
        Q = P->next;
        while (Q != NULL)
        {
            if (Q->infor.donGia > R->infor.donGia)
                R = Q;
            Q = Q->next;
        }
        HangHoa tg = P->infor;
        P->infor = R->infor;
        R->infor = tg;
        P = P->next;
    }
    cout << "Danh sach sap xep\n";
    hienThiDS(L);
}

int main()
{
    tro L;
    cout << "Nhap danh sach hang hoa\n";
    input_List(L);
    cout << "Danh sach vua nhap\n";
    hienThiDS(L);
    cout << "Chieu dai cua danh sach " << length(L) << endl;
    HangHoa them;
    cout << "Hang hoa can them\n";
    nhapHH(them);
    firstAdd(L, them);
    cout << "Hien thi danh sach\n";
    hienThiDS(L);
    removeIndex(L, 3);
    cout << "Hien thi danh sach\n";
    hienThiDS(L);
    cout << "Max: " << timMax(L)->infor.soLuong << " " << timMax(L)->infor.maHang << endl;
    cout << "So san pham co so luong lon hon 20: " << endl;
    timSP(L);
    sapXep(L);
    cout << "Xoa max\n";
    removeMax(L);
    hienThiDS(L);
}

/*
h2003
hop but
chiec
35000
15
h2002
but chi
cai
12000
50
h2001
vo
quyen
6500
20
h2004
tay
cai
10000
50
*/
Editor is loading...
Leave a Comment