Advertisement
vencinachev

Ex3.Packets-v3

Jan 16th, 2021
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int SIZE = 100;
  6.  
  7. struct Packet
  8. {
  9.     string msg;
  10.     int num;
  11.     double losses;
  12. };
  13.  
  14. void readPacket(Packet& p);
  15. void readPacketsArr(Packet parr[], int n);
  16. void filterPackets(Packet parr[], int& n);
  17. void swapPackets(Packet& p1, Packet& p2);
  18. void packetsSort(Packet parr[], int n);
  19. void printMsg(const Packet parr[], int n);
  20.  
  21. int main()
  22. {
  23.     int n;
  24.     Packet pck[SIZE];
  25.     do
  26.     {
  27.         cout << "Enter packets count [5-100]: ";
  28.         cin >> n;
  29.     }
  30.     while (n < 5 || n > 100);
  31.  
  32.     readPacketsArr(pck, n);
  33.  
  34.     filterPackets(pck, n);
  35.  
  36.     packetsSort(pck, n);
  37.  
  38.     cout << "Message: " << endl;
  39.     printMsg(pck, n);
  40.  
  41.     return 0;
  42. }
  43.  
  44. void readPacket(Packet& p)
  45. {
  46.     cout << "Read message: ";
  47.     cin.ignore();
  48.     getline(cin, p.msg);
  49.     cout << "Enter packet number: ";
  50.     cin >> p.num;
  51.     do
  52.     {
  53.         cout << "Enter packet losses (0 - 1): ";
  54.         cin >> p.losses;
  55.     }
  56.     while (p.losses < 0.0 || p.losses > 1.0);
  57. }
  58.  
  59. void readPacketsArr(Packet parr[], int n)
  60. {
  61.     for (int i = 0; i < n; i++)
  62.     {
  63.         readPacket(parr[i]);
  64.     }
  65. }
  66.  
  67. void filterPackets(Packet parr[], int& n)
  68. {
  69.     int j = 0;
  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         if (parr[i].losses < 0.51)
  73.         {
  74.             parr[j] = parr[i];
  75.             j++;
  76.         }
  77.     }
  78.     n = j;
  79. }
  80.  
  81. void swapPackets(Packet& p1, Packet& p2)
  82. {
  83.     Packet temp = p1;
  84.     p1 = p2;
  85.     p2 = temp;
  86. }
  87.  
  88. void packetsSort(Packet parr[], int n)
  89. {
  90.     int min_idx;
  91.     for (int i = 0; i < n-1; i++)
  92.     {
  93.         min_idx = i;
  94.         for (int j = i + 1; j < n; j++)
  95.         {
  96.             if (parr[j].num < parr[min_idx].num)
  97.             {
  98.                 min_idx = j;
  99.             }
  100.         }
  101.         swapPackets(parr[min_idx], parr[i]);
  102.     }
  103. }
  104.  
  105. void printMsg(const Packet parr[], int n)
  106. {
  107.     cout << parr[0].msg;
  108.     for (int i = 1; i < n; i++)
  109.     {
  110.         int diff = parr[i].num - parr[i - 1].num;
  111.         if (diff > 1)
  112.         {
  113.             for (int i = 0; i < diff; i++)
  114.             {
  115.                 cout << "-";
  116.             }
  117.         }
  118.         cout << parr[i].msg;
  119.     }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement