Lab10_Full4

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
4.4 kB
0
Indexable
Never
#include <iostream>
#include <cstring>

using namespace std;

const int StrLen = 250;

struct Military
{
    public:
    char FirstName[20], SecondName[20], LastName[20];
    char Country[20], Region[20], District[20], City[20], Street[20];    
    int Postcode, HouseNumber, Flat;
    char Nationality[10];
    int BirthDay, BirthMonth, BirthYear;
    char Position[10], Rank[10];
    
    Military* Next = 0;
};

istream& operator>>(istream& stream, Military& obj)
{
    char str[StrLen];
    
    cout << "\nEnter first name: ";
    cin.getline(obj.FirstName, StrLen);
    
    cout << "\nEnter second name: ";
    cin.getline(obj.SecondName, StrLen);
    
    cout << "\nEnter last name: ";
    cin.getline(obj.LastName, StrLen);
    
    cout << "\nEnter country: ";
    cin.getline(obj.Country, StrLen);
    
    cout << "\nEnter region: ";
    cin.getline(obj.Region, StrLen);
    
    cout << "\nEnter district: ";
    cin.getline(obj.District, StrLen);
    
    cout << "\nEnter city: ";
    cin.getline(obj.City, StrLen);
    
    cout << "\nEnter street: ";
    cin.getline(obj.Street, StrLen);
    
    cout << "\nEnter house number: ";
    cin >> obj.HouseNumber;
    
        
    cout << "\nEnter flat number: ";
    cin >> obj.Flat;
    
    cout << "\nEnter postcode: ";
    cin >> obj.Postcode;
    cin.ignore();
    
    cout << "\nEnter nationality: ";
    cin.getline(obj.Nationality, StrLen);
    
    cout << "\nEnter birthday day: ";
    cin >> obj.BirthDay;
    
    cout << "\nEnter birthday month: ";
    cin >> obj.BirthMonth;
    
    cout << "\nEnter birthday year: ";
    cin >> obj.BirthYear;
    
    cin.ignore();
    cout << "\nEnter position: ";
    cin.getline(obj.Position, StrLen);
    
    cout << "\nEnter rank: ";
    cin.getline(obj.Rank, StrLen);
    
    return stream;
}
ostream& operator<<(ostream& stream, Military& obj)
{
    stream
        << " " << obj.FirstName
        << " " << obj.SecondName
        << " " << obj.LastName
        << " " << obj.Country
        << " " << obj.Region
        << " " << obj.District
        << " " << obj.City
        << " " << obj.Street
        << " " << obj.HouseNumber
        << " " << obj.Flat
        << " " << obj.Postcode
        << " " << obj.Nationality
        << " " << obj.BirthDay
        << " " << obj.BirthMonth
        << " " << obj.BirthYear
        << " " << obj.Position
        << " " << obj.Rank;

    return stream;
}

Military *CreateList(int);
void DisplayList(Military*);
void DisplayChoise(Military*, char*, char*, char*);
void DeleteList(Military*);
int main()
{
    int dimension;
    char position[10], rank[10], nationality[20];
    
    cout << "Enter the number of militaries: ";
    cin >> dimension;
    Military *beginList = CreateList(dimension);
    
    cout << "\nThe list of militaries: \n";
    DisplayList(beginList);
    
    cout << "\nEnter position: ";
    cin.getline(position, StrLen);
    
    cout << "\nEnter rank: ";
    cin.getline(rank, StrLen);
    
    cout << "\nEnter nationality: ";
    cin.getline(nationality, StrLen);
        
    DisplayChoise(beginList, position, rank, nationality);
    
    DeleteList(beginList);    
    return 0;
}

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

    Military* current = beginList;
    for (int i = 1; i < n; i++) 
    {
        cout << "Enter information about " << i << "-th " << "military" << endl;
        current->Next = new Military;
        cin >> (*(current-> Next));
        current = current -> Next;
    }
    return beginList;
}
void DisplayList(Military* beginList)
{
    Military* current = beginList;
    while(current)
    {
        cout << (*current) << endl;
        current = current->Next;
    }
}
void DisplayChoise(Military* beginList, char* position, char* rank, char* nationality)
{
    Military* current = beginList;
    while (current)
    {
        if (strcmp(current->Position, position) == 0 
        && strcmp(current->Rank, rank) == 0 
        && strcmp(current->Nationality, nationality) == 0)
            cout << (*current);
        current = current->Next;
    }
}
void DeleteList(Military *beginList) 
{
    Military* current = beginList;
    while (current) 
    {
        beginList = current->Next;
        delete current;
        current = beginList;
    }
}