Advertisement
skb50bd

Football ScoreCard

May 20th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. struct team
  8. {
  9.     string name;
  10.     int played, won, drawn, lost, points, goals, goalsa, diff;
  11.     team(): played(0), won(0), drawn(0), lost(0), points(0), goals(0), goalsa(0), diff(0) {};
  12. };
  13.  
  14. void sortin (struct team *p, struct team *q);
  15. void swapr (struct team *p, struct team *q);
  16.  
  17. int main ()
  18. {
  19.     string tournament, line, tm1, tm2;
  20.     int T, G, i, j, tm1g, tm2g;
  21.  
  22.     //Taking name of the Tournament
  23.     cout << "Name of the Tournament: ";
  24.     getline (cin, tournament);
  25.  
  26.     //Taking input for Team Number
  27.     cout << "How many Teams? ";
  28.     getline (cin, line);
  29.     stringstream (line) >> T;
  30.  
  31.     //Setting up Array Size for Teams
  32.     struct team tm[T], tts;
  33.     for (i = 0; i < T; i++)
  34.         getline (cin, tm[i].name);
  35.  
  36.     //Taking input for the Number of Games Already Played
  37.     cout << "Total Games Played in the Tournament: ";
  38.     getline (cin, line);
  39.     stringstream (line) >> G;
  40.  
  41.     //Taking Scores
  42.     while (G--)
  43.     {
  44.         getline (cin, tm1);
  45.  
  46.         getline (cin, line);
  47.         stringstream (line) >> tm1g;
  48.  
  49.         getline (cin, tm2);
  50.  
  51.         getline (cin, line);
  52.         stringstream (line) >> tm2g;
  53.  
  54.         //Checking Team Match and Updating Scores
  55.         for (i = 0; i < T; i++)
  56.             if (tm[i].name == tm1)
  57.             {
  58.                 tm[i].played++;
  59.                 if (tm1g > tm2g)
  60.                 {
  61.                     tm[i].won++;
  62.                     tm[i].points += 3;
  63.                     tm[i].goals += tm1g;
  64.                     tm[i].goalsa += tm2g;
  65.                     tm[i].diff += (tm1g - tm2g);
  66.                 }
  67.                 else if (tm1g < tm2g)
  68.                 {
  69.                     tm[i].lost++;
  70.                     tm[i].goals += tm1g;
  71.                     tm[i].goalsa += tm2g;
  72.                     tm[i].diff += (tm1g - tm2g);
  73.                 }
  74.                 else
  75.                 {
  76.                     tm[i].drawn++;
  77.                     tm[i].points++;
  78.                     tm[i].goals += tm1g;
  79.                     tm[i].goalsa += tm2g;
  80.                 }
  81.                 break;
  82.             }
  83.  
  84.          for (i = 0; i < T; i++)
  85.             if (tm[i].name == tm2)
  86.             {
  87.                 tm[i].played++;
  88.                 if (tm2g > tm1g)
  89.                 {
  90.                     tm[i].won++;
  91.                     tm[i].points += 3;
  92.                     tm[i].goals += tm2g;
  93.                     tm[i].goalsa += tm1g;
  94.                     tm[i].diff += (tm2g - tm1g);
  95.                 }
  96.                 else if (tm2g < tm1g)
  97.                 {
  98.                     tm[i].lost++;
  99.                     tm[i].goals += tm2g;
  100.                     tm[i].goalsa += tm1g;
  101.                     tm[i].diff += (tm2g - tm1g);
  102.                 }
  103.                 else
  104.                 {
  105.                     tm[i].drawn++;
  106.                     tm[i].points += 1;
  107.                     tm[i].goals += tm2g;
  108.                     tm[i].goalsa += tm1g;
  109.                 }
  110.                 break;
  111.             }
  112.     }
  113.  
  114.     //Sorting
  115.     for (i = 0; i < T - 1; i++)
  116.         for (j = i + 1; j < T; j++)
  117.             sortin (&tm[i], &tm[j]);
  118.  
  119.     //printing Scores
  120.     cout << endl << tournament << endl << left << setw(6) << "Pos." << left << setw(15) << "Team Name" << right << setw(7) << "Played" << right << setw(4) << "Won" << right << setw(6) << "Drawn" << right << setw(5) << "Lost" << right << setw(7) << "Points" << right << setw(13) << "Goals (F/A)" << right << setw(5) << "Diff" << endl;
  121.     for (i = 0; i < T; i++)
  122.         cout << left << setw(6) << i+1 << left << setw(15) << tm[i].name << right << setw(7) << tm[i].played << right << setw(4) << tm[i].won << right << setw(6) << tm[i].drawn << right << setw(5) << tm[i].lost << right << setw(7) << tm[i].points << right << setw(11) << tm[i].goals << "/" << tm[i].goalsa << right << setw(5) << tm[i].diff << endl;
  123.     cout << endl;
  124.  
  125.     return 0;
  126. }
  127.  
  128. //Swapping Function
  129. void swapr (struct team *p, struct team *q)
  130. {
  131.     struct team t;
  132.         t = *p;
  133.         *p = *q;
  134.         *q = t;
  135.         return;
  136. }
  137.  
  138. //Sorting Function
  139. void sortin (struct team *p, struct team *q)
  140. {
  141.     if ((*p).points < (*q).points)
  142.         swapr(p,q);
  143.  
  144.     else if (((*p).won < (*q).won) && ((*p).points == (*q).points))
  145.         swapr(p,q);
  146.  
  147.     else if (((*p).diff < (*q).diff) && ((*p).won == (*q).won) && ((*p).points == (*q).points))
  148.         swapr(p,q);
  149.  
  150.     else if (((*p).won < (*q).goals) && ((*p).diff == (*q).diff) && ((*p).won == (*q).won) && ((*p).points == (*q).points))
  151.         swapr(p,q);
  152.  
  153.     else if (((*p).played > (*q).played) && ((*p).won < (*q).goals) && ((*p).diff == (*q).diff) && ((*p).won == (*q).won) && ((*p).points == (*q).points))
  154.         swapr(p,q);
  155.  
  156.     return;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement