Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int SIZE = 100;
- struct Packet
- {
- string msg;
- int num;
- double losses;
- };
- void readPacket(Packet& p);
- void readPacketsArr(Packet parr[], int n);
- void filterPackets(Packet parr[], int& n);
- void swapPackets(Packet& p1, Packet& p2);
- void packetsSort(Packet parr[], int n);
- void printMsg(const Packet parr[], int n);
- int main()
- {
- int n;
- Packet pck[SIZE];
- do
- {
- cout << "Enter packet count [5-100]: ";
- cin >> n;
- }
- while (n < 5 || n > 100);
- readPacketsArr(pck, n);
- filterPackets(pck, n);
- packetsSort(pck, n);
- cout << "Message: " << endl;
- printMsg(pck, n);
- return 0;
- }
- void readPacket(Packet& p)
- {
- cout << "Read message: ";
- cin.ignore();
- getline(cin, p.msg);
- cout << "Enter packet number: ";
- cin >> p.num;
- do
- {
- cout << "Enter packet losses (0 - 1): ";
- cin >> p.losses;
- }
- while (p.losses < 0.0 || p.losses > 1.0);
- }
- void readPacketsArr(Packet parr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- readPacket(parr[i]);
- }
- }
- void filterPackets(Packet parr[], int& n)
- {
- int j = 0;
- for (int i = 0; i < n; i++)
- {
- if (parr[i].losses < 0.51)
- {
- parr[j] = parr[i];
- j++;
- }
- }
- n = j;
- }
- void swapPackets(Packet& p1, Packet& p2)
- {
- Packet temp = p1;
- p1 = p2;
- p2 = temp;
- }
- void packetsSort(Packet parr[], int n)
- {
- int min_idx;
- for (int i = 0; i < n-1; i++)
- {
- min_idx = i;
- for (int j = i + 1; j < n; j++)
- {
- if (parr[j].num < parr[min_idx].num)
- {
- min_idx = j;
- }
- }
- swapPackets(parr[min_idx], parr[i]);
- }
- }
- void printMsg(const Packet parr[], int n)
- {
- cout << parr[0].msg;
- for (int i = 1; i < n; i++)
- {
- int diff = parr[i].num - parr[i - 1].num;
- for (int i = 0; i < diff - 1; i++)
- {
- cout << "-";
- }
- cout << parr[i].msg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement