Victoria_Lab10_Fixed

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

using namespace std;

const int StrLen = 250;

struct Military
{
    public:
    char SecondName[20];
    char Nationality[20];
    char Position[20], Rank[20];
    int Postcode;
    Military* Next = 0;
};

istream& operator>>(istream& stream, Military& obj)
{
    char str[StrLen];
    
    cout << "\nEnter second name: ";
    cin.getline(obj.SecondName, StrLen);
    
    cout << "\nEnter nationality: ";
    cin.getline(obj.Nationality, StrLen);

    cout << "\nEnter position: ";
    cin.getline(obj.Position, StrLen);
    
    cout << "\nEnter rank: ";
    cin.getline(obj.Rank, StrLen);
    
    cout << "\nEnter postcode: ";
    cin >> obj.Postcode;
    
    cin.ignore();
    
    return stream;
}
ostream& operator<<(ostream& stream, Military& obj)
{
    stream
        << " " << obj.SecondName
        << " " << obj.Nationality
        << " " << obj.Position
        << " " << obj.Rank
        << " " << obj.Postcode;

    return stream;
}

Military *CreateList(int);
void DisplayList(Military*);
void DisplayChoise(Military*, char*, char*, char*);
void DeleteList(Military*);
int main()
{
    int dimension;
    char position[20], rank[20], 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;
    }
}