#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// player node for linkedlist
class Player {
public:
int id;
string name;
string position;
int age;
double salary;
Player* next;
};
// player linkedlist
class team {
public:
int rank;
string name;
string president;
Player* head;
// function to insert newnode(add new player)
void addPlayer(int id, string name, string position, int age, double salary) {
Player* newPlayer = new Player();
newPlayer->id = id;
newPlayer->name = name;
newPlayer->position = position;
newPlayer->age = age;
newPlayer->salary = salary;
if (head == NULL) {
head = newPlayer;
} else {
Player* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newPlayer;
}
}
void readTeamFromFile( string fileName) {
ifstream inputFile(fileName);
if (!inputFile.is_open()) {
cout << "Unable to open file "<< endl;
return;
}
string line;
while (getline(inputFile, line)) {
// Find positions of commas
/*19,nada mohamed
pos1 -> 1->9->, =2;
pos2->1->9->,->n->a->d->a->space->m->o->h->a->m->e->d->,=15
*/
size_t pos1 = line.find(",");
size_t pos2 = line.find(",", pos1 + 1);
size_t pos3 = line.find(",", pos2 + 1);
size_t pos4 = line.find(",", pos3 + 1);
// Extract data from the line
/*pos1+1 skip first comma 19|,n/ada
pos1=| & pos1+1=/ , pos2-pos1 find space between tow comma & -1 without second comma*/
int id = stoi(line.substr(0, pos1));//stoi convert string into int num
string name = line.substr(pos1 + 1, pos2 - pos1 - 1);
string position = line.substr(pos2 + 1, pos3 - pos2 - 1);
int age = stoi(line.substr(pos3 + 1, pos4 - pos3 - 1));
double salary = stod(line.substr(pos4 + 1));
// Add the player to the linked list
addPlayer(id, name, position, age, salary);
}
inputFile.close();
}
void displayPlayers() {
Player* temp = head;
if (temp==NULL) {
cout << "No players in the team." << endl;
return;
}
while (temp) {
cout << temp->id << "," << temp->name<< "," << temp->position << "," << temp->age << "," << temp->salary << endl;
temp = temp->next;
}
}
};
int main() {
team team;
team.name = "el bank el-ahly";
team.readTeamFromFile("El Bank Al-ahly.txt");
cout << "Players in team " << team.name << ":\n";
team.displayPlayers();
return 0;
}
ICNpbmNsdWRlIDxpb3N0cm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PSB0ZW1wLT5uZXh0OwogICAgICAgICAgICB9CiAgICAgICAgICAgIHRlbXAtPm5leHQgPSBuZXdQbGF5ZXI7CiAgICAgICAgfQogICAgfQoKICAgIHZvaWQgcmVhZFRlYW1Gcm9tRmlsZSggc3RyaW5nIGZpbGVOYW1lKSB7CiAgICAgICAgaWZzdHJlYW0gaW5wdXRGaWxlKGZpbGVOYW1lKTsKCiAgICAgICAgaWYgKCFpbnB1dEZpbGUuaXNfb3BlbigpKSB7CiAgICAgICAgICAgIGNvdXQgPDwgIlVuYWJsZSB0byBvcGVuIGZpbGUgIjw8IGVuZGw7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIHN0cmluZyBsaW5lOwogICAgICAgIHdoaWxlIChnZXRsaW5lKGlucHV0RmlsZSwgbGluZSkpIHsKICAgICAgICAgICAgLy8gRmluZCBwb3NpdGlvbnMgb2YgY29tbWFzCiAgICAgICAgICAgIC8qMTksbmFkYSBtb2hhbWVkCiAgICAgICAgICAgIHBvczEgLT4gMS0+OS0+LCA9MjsKICAgICAgICAgICAgcG9zMi0+MS0+OS0+LC0+bi0+YS0+ZC0+YS0+c3BhY2UtPm0tPm8tPmgtPmEtPm0tPmUtPmQtPiw9MTUKICAgICAgICAgICAgKi8KICAgICAgICAgICAgc2l6ZV90IHBvczEgPSBsaW5lLmZpbmQoIiwiKTsKICAgICAgICAgICAgc2l6ZV90IHBvczIgPSBsaW5lLmZpbmQoIiwiLCBwb3MxICsgMSk7CiAgICAgICAgICAgIHNpemVfdCBwb3MzID0gbGluZS5maW5kKCIsIiwgcG9zMiArIDEpOwogICAgICAgICAgIHNpemVfdCBwb3M0ID0gbGluZS5maW5kKCIsIiwgcG9zMyArIDEpOwogICAgICAgICAgICAvLyBFeHRyYWN0IGRhdGEgZnJvbSB0aGUgbGluZQogICAgICAgICAgICAgLypwb3MxKzEgc2tpcCBmaXJzdCBjb21tYSAxOXwsbi9hZGEKICAgICAgICAgICAgcG9zMT18ICYgc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