LAB10_VLAD

 avatar
unknown
c_cpp
3 years ago
2.9 kB
11
Indexable
#include <iostream>
#include <cstring>

using namespace std;

const int StrLen = 250;

class Product
{
    public:
    char Name[20];
    char Grade[20];
    int Cost;
    int ShelfLife;
    int ReleaseDate;
    Product* Next = 0;
};

istream& operator>>(istream& stream, Product& obj)
{
    char str[StrLen];

    cout << "\nEnter product name: ";
    cin.getline(obj.Name, StrLen);

    cout << "\nEnter product grade: ";
    cin.getline(obj.Grade, StrLen);
    
    cout << "\nEnter product cost: ";
    cin >> obj.Cost;
    
    cout << "\nEnter product shelf life: ";
    cin >> obj.ShelfLife;
    
    cout << "\nEnter product release date: ";
    cin >> obj.ReleaseDate;
    
    cin.ignore();
    return stream;
}
ostream& operator<<(ostream& stream, Product& obj)
{
    stream
        << " " << obj.Name
        << " " << obj.Grade
        << " " << obj.Cost
        << " " << obj.ShelfLife
        << " " << obj.ReleaseDate;

    return stream;
}

Product *CreateList(int);
void DisplayList(Product*);
void DisplayChoise(Product*, char*, int, int);
void DeleteList(Product*);
int main()
{
    int dimension;
    char grade[15];
    int lowerCost, higherCost;
    
    cout << "Enter the number of products: ";
    cin >> dimension;
    Product *beginList = CreateList(dimension);
    
    cout << "\nThe list of products: \n";
    DisplayList(beginList);
    
    cout << "\nEnter the type of product: ";
    cin.getline(grade, StrLen);
    
    cout << "\nEnter lower cost: ";
    cin >> lowerCost;
    
    cout << "\nEnter highter cost: ";
    cin >> higherCost;
    
    DisplayChoise(beginList, grade, lowerCost, higherCost);
    
    DeleteList(beginList);    
    return 0;
}

Product* CreateList(int n) 
{
    Product* beginList = new Product;
    cout << "Enter information about 1-th product: " << endl;
    cin.ignore();
    cin >> (*beginList);

    Product* current = beginList;
    for (int i = 1; i < n; i++) 
    {
        cout << "Enter information about " << i << "-th " << "product" << endl;
        current->Next = new Product;
        cin >> (*(current-> Next));
        current = current -> Next;
    }
    return beginList;
}
void DisplayList(Product* beginList)
{
    Product* current = beginList;
    while(current)
    {
        cout << (*current) << endl;
        current = current->Next;
    }
}
void DisplayChoise(Product* beginList, char* grade, int lowerCost, int higherCost)
{
    Product* current = beginList;
    while (current)
    {
        if (strcmp(current->Grade, grade) == 0 && current->Cost > lowerCost && current->Cost <= higherCost)
            cout << (*current);
        current = current->Next;
    }
}
void DeleteList(Product *beginList) 
{
    Product* current = beginList;
    while (current) 
    {
        beginList = current->Next;
        delete current;
        current = beginList;
    }
}
Editor is loading...