Stack Array

 avatar
TalonEzio
c_cpp
2 years ago
2.9 kB
9
Indexable
Never
#include <iostream>
#include <string>
using namespace std;
#define MAX_OF_STACK 100
int top = -1;
struct Date 
{
    int Ngay, Thang, Nam;
    void Nhap()
    {
        cout << "Ngay : "; cin >> Ngay;
        cout << "Thang :"; cin >> Thang;
        cout << "Nam : "; cin >> Nam;
    }
    void Xuat()
    {
        cout << Ngay << "/" << Thang << "/" << Nam << endl;
    }

};
struct HangHoa
{
    string ID, Ten;
    Date NgayXuat;
    float GiaXuat;
    void Nhap()
    {
        cout << "ID : ";
        getline(cin, ID);
        cout << "Ten : ";
        getline(cin, Ten);
        NgayXuat.Nhap();
        cout << "Gia xuat : ";
        cin >> GiaXuat;
        cin.ignore();
    }
    void Xuat()
    {
        cout << "ID :" << ID << endl;
        cout << "Ten : " << Ten << endl;
        cout << "Ngay Xuat : "; NgayXuat.Xuat();
        cout << "Gia Xuat : " << GiaXuat << endl;
    };

};
void push(HangHoa** stack,HangHoa* Data)
{
    if (top < MAX_OF_STACK)
    {
        top = top + 1;
        stack[top] = Data;
    }
    else
    {
        cout << "Stack da day!";
        exit(0);
    }
}
HangHoa* pop(HangHoa** stack)
{
    HangHoa* Data;
    if (top >= 0)
    {
        Data = stack[top];
        top = top - 1;
    }
    else
    {
        cout << "Stack rong!";
        return NULL;
    }
    return Data;
}
void SearchStack(HangHoa** stack,string ID = "001")
{
    cout << "------------------------------------\n";
    int count = 0;
    for (int i = 0; i <= top; ++i)
    {
        HangHoa* hh = stack[i];
        if (hh->ID == ID)
        {
            hh->Xuat();
            count++;
        }
    }
    if (count == 0)
    {
        cout << "Khong co hang hoa nao ID = " << ID << endl;
    }
    cout << "------------------------------------\n";

}
void EmptyStack(HangHoa** stack)
{
    while (top > -1)
    {
        cout << "Thong tin hang hoa chuan bi boc ra :\n";
        HangHoa* hh = pop(stack);
        hh->Xuat();
        cout << "Da boc goi hang ra khoi bang truyen!\n";
        cout << "-----------------------\n";
    }
    cout << "Da boc het hang ra khoi bang truyen!\n";
    cout << "------------------------------------\n";
}
int main()
{
    //Cau 1
    HangHoa* hangHoa = new HangHoa();
    hangHoa->Nhap();
    hangHoa->Xuat();
    delete hangHoa;
    cout << "------------------------------------\n";
    //Cau 2
    int n;
    HangHoa** stack = new HangHoa*[MAX_OF_STACK];
    cout << "So luong hang hoa : "; cin >> n;
    cin.ignore();
    cout << "Nhap danh sach hang hoa :" << endl;
    for (int i = 0; i < n; ++i)
    {
        cout << "Thong tin hang hoa thu " << i + 1 << ":\n";
        HangHoa* hh = new HangHoa();
        hh->Nhap();
        push(stack, hh);
    }
    //Cau 3
    SearchStack(stack);
    //Cau 4
    EmptyStack(stack);
    system("pause");
}