1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
struct TeamInfo
{
char name[50];
int points;
int goalsFor;
int goalsAgainst;
int played;
int won;
int lost;
int drawn;
};
typedef struct TeamInfo T;
void addTeam(T *, char[50], int);
//void addTeam(char[50], int);
void calculateResult(T *, int, int, int, int);
void sortTable(T[]);
int main(void)
{
char newTeam[50];
int menuChoice = 0;
int numOfTeams = 0;
int team1, team2, team1Score, team2Score;
TeamInfo T[6];
int i;
while (menuChoice != 4)
{
cout << "*** SOCCER LEAGUE TABLE ***\n\n" << endl;
cout << "1. Add new team\n" << endl;
cout << "2. Display soccer league table\n" << endl;
cout << "3. Add result or score\n" << endl;
cout << "4. Quit the program\n" << endl;
cin >> menuChoice;
if (menuChoice == 1)
{
if (numOfTeams == 6)
cout << "Error: Maximum amount of teams has been entered" << endl;
else
{
cout << "Add new team\n" << endl;
//cin >> newTeam, sizeof(newTeam), stdin;
//newTeam[strlen(newTeam)] = '\0';
cin >> newTeam, sizeof(newTeam), stdin;
newTeam[strlen(newTeam)] = '\0';
addTeam(T, newTeam, numOfTeams);
//numOfTeams++;
cout << "\nNew team added\n" << endl;
system("pause");
system("CLS");
numOfTeams++;
}
}
else if (menuChoice == 2)
{
cout << "\t\tP\tW\tD\tL\tF\tA\tT\n\n\n" << endl;
cin.get();
for ( i = 0; i < numOfTeams; i++)
{
//i < numOfTeams;
cout << T[i].name<<"\t\t"<<T[i].played<<"\t"<<T[i].won<<"\t"<<T[i].drawn<<"\t"<<T[i].lost<<"\t"<<T[i].goalsFor<<"\t"<<T[i].goalsAgainst<<"\t"<<T[i].points<<"\n\n\n"<< endl;
cin >> T[i].name, T[i].played, T[i].won, T[i].drawn, T[i].lost, T[i].goalsFor, T[i].goalsAgainst, T[i].points;
//system("pause");
//cin.ignore();
}
}
else if (menuChoice == 3)
{
cout << "Enter first team playing the match:\n" << endl;
cin >> team1;
cout << "Enter second team playing the match:\n" << endl;
cin >> team2;
cout << "Enter first teams score:\n" << endl;
cin >> team1Score;
cout << "Enter second teams score:\n" << endl;
cin >> team2Score;
calculateResult(T , team1, team2, team1Score, team2Score);
}
}
return 0;
}
void addTeam(T* T, char *teamName, int i)
{
strcpy(T[i].name, teamName);
T[i].points = 0;
T[i].goalsFor = 0;
T[i].goalsAgainst = 0;
T[i].played = 0;
T[i].won = 0;
T[i].lost = 0;
T[i].drawn = 0;
}
void calculateResult(T* T, int t1, int t2, int t1R, int t2R)
{
int numOfTeams;
if (t1R > t2R)
{
T[(t1-1)].points+=3;
T[(t1-1)].won++;
T[(t2-1)].lost++;
}
else if (t2R > t1R)
{
T[(t2-1)].points+=3;
T[(t2-1)].won++;
T[(t1-1)].lost++;
}
else
{
T[(t2-1)].points++;
T[(t1-1)].points++;
T[(t1-1)].drawn++;
T[(t2-1)].drawn++;
}
T[(t1-1)].goalsFor+=t1R;
T[(t2-1)].goalsFor+=t2R;
T[(t1-1)].goalsAgainst+=t2R;
T[(t2-1)].goalsAgainst+=t1R;
T[(t1-1)].played++;
T[(t2-1)].played++;
//i < numOfTeams;
sortTable(T);
}
void sortTable(T teams1[])
{
T temp;//[1];
int i, j;
for ( i = 0; i < 6; i++)
{
for ( j = 6; j >=0; j--)
{
if (teams1[j].points > teams1[(j-1)].points)
{
temp/*[1]*/ = teams1[(j-1)];
teams1[(j-1)] = teams1[j];
teams1[j] = temp;//[1];
}
}
}
}
| |