Sanlover

Untitled

Sep 27th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace  std;
  5.  
  6. const size_t nTeams = 5;
  7.  
  8. struct FootballChampionship
  9. {
  10.     size_t table[nTeams][nTeams];
  11.     string teamNames[nTeams];
  12.  
  13.     void print()
  14.     {
  15.         cout << "Table:" << endl;
  16.         for (size_t i = 0; i < nTeams; i++)
  17.         {
  18.             for (size_t j = 0; j < nTeams; j++)
  19.             {
  20.                 cout << setw(3) << table[i][j];
  21.             }
  22.             cout << endl;
  23.         }
  24.     };
  25.  
  26.     void fill()
  27.     {
  28.         cout << "Enter the names of teams:" << endl;
  29.         for (size_t i = 0; i < nTeams; i++)
  30.         {
  31.             cout << i + 1 << ") ";
  32.             getline(cin, teamNames[i]);
  33.         }
  34.  
  35.         cout << endl << "Fill table:" << endl;
  36.         for (size_t i = 0; i < nTeams; i++)
  37.             for (size_t j = 0; j < nTeams; j++)
  38.                 if (i != j)
  39.                     cin >> table[i][j];
  40.                 else
  41.                     table[i][j] = 0;
  42.     }
  43.  
  44.     //a
  45.     string getWinMoreLose()
  46.     {
  47.         string str;
  48.  
  49.         size_t lose, win;
  50.  
  51.         for (size_t i = 0; i < nTeams; i++)
  52.         {
  53.             lose = win = 0;
  54.             for (size_t j = 0; j < nTeams; j++)
  55.                 if (i != j)
  56.                 {
  57.                     if (table[i][j] == 3)
  58.                         win++;
  59.                     else if (table[i][j] == 0)
  60.                         lose++;
  61.                 }
  62.             if (win > lose)
  63.                 str += teamNames[i] + "; ";
  64.         }
  65.         return str;
  66.     }
  67.  
  68.     //b
  69.     string getWithoutLose()
  70.     {
  71.         string str;
  72.  
  73.         for (size_t i = 0; i < nTeams; i++)
  74.         {
  75.             bool isFailed = false;
  76.             for (size_t j = 0; j < nTeams; j++)
  77.                 if (i != j && table[i][j] == 0)
  78.                 {
  79.                     isFailed = true;
  80.                     break;
  81.                 }
  82.             if (!isFailed)
  83.                 str += teamNames[i] + "; ";
  84.         }
  85.         return str;
  86.     };
  87.  
  88.     //B
  89.     string getWinner()
  90.     {
  91.         size_t iMax = 0, sMax = 0;
  92.  
  93.         for (size_t i = 0; i < nTeams; i++)
  94.         {
  95.             size_t sCurr = _getTeamScore(i);
  96.             if (sCurr > sMax)
  97.             {
  98.                 iMax = i;
  99.                 sMax = sCurr;
  100.             }
  101.         }
  102.         return teamNames[iMax];
  103.     }
  104.  
  105.     //C
  106.     bool isSorted()
  107.     {
  108.         for (size_t i = 0; i < nTeams - 1; i++)
  109.             if (_getTeamScore(i) < _getTeamScore(i + 1))
  110.                 return false;
  111.         return true;
  112.     };
  113.  
  114.     //helping void
  115.     size_t _getTeamScore(size_t id)
  116.     {
  117.         size_t score = 0;
  118.         for (size_t j = 0; j < nTeams; j++)
  119.             if (id != j)
  120.                 score += table[id][j];
  121.         return score;
  122.     }
  123. };
  124.  
  125. int main()
  126. {
  127.     FootballChampionship s;
  128.  
  129.     s.fill();
  130.  
  131.     cout << endl;
  132.  
  133.     s.print();
  134.  
  135.     cout << endl << "Teams, that have more wins neither loses: " << s.getWinMoreLose();
  136.     cout << endl << "Teams, that didn't lose: " << s.getWithoutLose();
  137.     cout << endl << "Team winner: " << s.getWinner();
  138.     cout << endl << "Is sorteb by places: " << s.isSorted();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment