Commit ab3e4023 authored by affan.khan's avatar affan.khan

Add new file

parents
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//create a structure for Pokemons
typedef struct PokemonNode
{
//create the variables for the pokemons
char pokemonName[50];
char pokemonType[50];
char pokemonAbility[50];
//create the next pointer for the Pokemon list
struct PokemonNode *next;
} PokemonNode ;//end of the pokemon structure
//create the head and current pointers for pokemon list
struct PokemonNode *pokemonHead = NULL;
struct PokemonNode *pokemonCurrent = NULL;
//create a struct for players
typedef struct PlayerNode
{
//create the variables for the player nodes
char playerName[50];
int pokemonsOwned;
int *nameOfPokemonsOwned[20];
struct PlayerNode *playerNext;
} PlayerNode ;//end of the struct for the player list
//create the head and current pointers for the player list
struct PlayerNode *playerHead = NULL;
struct PlayerNode *playerCurrent = NULL;
//create a function to insert items to the pokemon list
void addPokemonList(char pokemonName[50], char pokemonType[50], char pokemonAbility[50])
{
//create a link at the first location
struct PokemonNode *link = (struct PokemonNode*)malloc(sizeof(struct PokemonNode));
//add the values to the node
strcpy(link->pokemonName, pokemonName);
strcpy(link->pokemonType, pokemonType);
strcpy(link->pokemonAbility, pokemonAbility);
//point the pointer to the previous first node
link->next = pokemonHead;
//point the pointer to the new first node
pokemonHead = link;
}//end of method that adds to the list
//create a function that will add items to the player list
void addPlayerList(char playerName[50])
{
//create a link at the first location
struct PlayerNode *link = (struct PlayerNode*)malloc(sizeof(struct PlayerNode));
//add the name of the player to the node
strcpy(link->playerName, playerName);
//set the intial count of pokemon owned to 0
link->pokemonsOwned = 0;
//point the pointer to the previous first node
link->playerNext = playerHead;
//point the pointer to the new first node
playerHead = link;
}//end of the method that will add nodes to player list
//create a function to display the pokemon list
void listPokemons()
{
//create a pointer
struct PokemonNode *ptr = pokemonHead;
//inform the user as what is being printed
printf("The list of Pokemons are:\n");
//create a while loop which iterates through the list
while (ptr != NULL)
{
//print out the pokemon's name
printf("%s \n", ptr->pokemonName);
//move the pointer to the next node
ptr = ptr->next;
}//end of the while loop for pokemon list
}//end of the function to display the pokemon list
//create a function that will display a list of player names
void listPlayers()
{
//create a pointer
struct PlayerNode *ptr = playerHead;
//inform the user as what is being printed
printf("The list of the players are:\n");
//create a while loop that traverses through the player list
while (ptr != NULL)
{
//print out the player names
printf("%s \n", ptr->playerName);
//move the pointer to the next node
ptr = ptr->playerNext;
}//end of the while loop that goes through the player list
}//end of the function that displays list of players
//create a function that searches the pokemon list for via its name
struct PokemonNode* findPokemon (char pokemonName[50])
{
//begin searching from the first link
struct PokemonNode* pokemonCurrent = pokemonHead;
//create a while loop that iterates through the list
while (pokemonCurrent != NULL)
{
//create an if statement for when the pokemon exsists
if (strcmp(pokemonName, pokemonCurrent->pokemonName) == 0)
{
//return the pointer to the pokemeon
return pokemonCurrent;
}//end of the while loop if the pokemon exsists
//if not move to the next node
pokemonCurrent = pokemonCurrent ->next;
}//end of the while loop
//return NULL if the pokemon is not found
return NULL;
}//end of the method that searches the pokemon list for an entry
//create a function that searches the player list via the player name
struct PlayerNode* findPlayer(char playerName[50])
{
//begin searching from the first link
struct PlayerNode* playerCurrent = playerHead;
//create a while loop that iterates through the list
while (playerCurrent != NULL)
{
//create an if statement for when the player exsists
if (strcmp(playerName, playerCurrent->playerName) == 0)
{
//return the pointer of the player
return playerCurrent;
}//end oof the if statement if the player exists
//move to the next node of the list
playerCurrent = playerCurrent ->playerNext;
}//end of the while loop
//return NULL if the pokemon is not found
return NULL;
}//end of the function that searches the player via their name
//create a function that adds pokemon to the list if it doesn't exsists
void addPokemonToList(char pokemonName[50], char pokemonType[50], char pokemonAbility[50])
{
//create a String variable for the pokemon name
char intermediatePokemonName[50];
//give the variable the value of the pokemon name
strcpy(intermediatePokemonName, pokemonName);
//create an if statement for when the pokemon does not exsist
if (findPokemon(intermediatePokemonName) == NULL)
{
//call the function that adds a new node to the pokemon list
addPokemonList(pokemonName, pokemonType, pokemonAbility);
//tell the user the pokemon has been added to the list
printf("The pokemon has been added to the pokemon list\n");
}//end of the if statement for when the pokemon does exsist
//create an else statement for when the pokemon doesn't exsist
else
{
//tell the user the pokemon does exsist
printf("The pokemon does exsist\n");
}//end of the else statement
}//end of the add pokemon to list function
//create a function that adds the player to the list if it doesn't already exists
void addPlayerToList(char playerName[50])
{
//create a string variable that will carry the player name
char intermediatePlayerName[50];
//give value to the intermediate varriable
strcpy(intermediatePlayerName, playerName);
//create an if statement for when the pokemon does not exsist
if (findPlayer(intermediatePlayerName) == NULL)
{
//call the function that creates new nodes in the player list
addPlayerList(playerName);
//inform the user that the player has been added to the list
printf("The player has been added to the list\n");
}//end of the if statement for when the player does not exsist
//create an else statement if the player does not exsists
else
{
printf("The player does exsist\n");
}//end of the else statement
}//end of the function that adds the player to the list
//create a function that adds the pokemon of the players list
void addPokemonToPlayer(char playerName[50], char pokemonName[50])
{
//create a pointer for the player list
struct PlayerNode *playerPtr = findPlayer(playerName);
//create an int variable for the number of pokemon owned
int intermediateNumberOfPokemons = playerPtr->pokemonsOwned;
//create an if statement for when the player or pokemon is not part of the list
if (findPlayer(playerName) == NULL && findPokemon(pokemonName) == NULL)
{
//tell the user that they are not present
printf("The player or the pokemon is not part of their respective lists\n");
}//end of the if statement for when the player is part of the list
//create an else statement for when they are present
else
{
//add the pointer of the pokemon node to the player's array
playerPtr->nameOfPokemonsOwned[intermediateNumberOfPokemons] = findPokemon(pokemonName);
//increment the number of pokemon value by 1
playerPtr->pokemonsOwned = intermediateNumberOfPokemons + 1;
}//end of the else statement
}//end of the function that adds pokemons to players list
//create a function that will display the details of a specific pokemon
void displayPokemonDetails(char pokemonName[50])
{
//create a pointer pointing to that specific pokemon node
struct PokemonNode *pokemonPtr = findPokemon(pokemonName);
//print out the details
printf("Name: ");
printf("%s \n", pokemonPtr->pokemonName);
printf("Type: ");
printf("%s \n", pokemonPtr->pokemonType);
printf("Ability: ");
printf("%s \n", pokemonPtr->pokemonAbility);
}//end of the method that displays the details of a certain animal
//create a function that will display all the details of a player
void displayPlayerDetails(char playerName[50])
{
//create a pointer which points to the specific player node
struct PlayerNode *playerPointer = findPlayer(playerName);
//print out the player name and the number of pokemons owned
printf("Name: ");
printf("%s \n", playerPointer->playerName);
printf("Number of Pokemons Owned: ");
printf("%d \n", playerPointer->pokemonsOwned);
//tell the user that now the pokemons owned will be printed
printf("The pokemons owned by this player are: \n");
//create a variable which will be used to iterate through a loop
int iterateVariable = 0;
//create a variable repesenting the size of the array
int arraySize = sizeof(playerPointer->nameOfPokemonsOwned)/sizeof(playerPointer->nameOfPokemonsOwned[0]);
//create a for loop that will iterate through the array holding the pointers
for (iterateVariable; iterateVariable < arraySize; iterateVariable++)
{
//create a pointer for the pokemon linked list
struct PokemonNode *pokemonPointer = playerPointer->nameOfPokemonsOwned[iterateVariable];
//create an if statement to ensure the array has values
if (playerPointer->nameOfPokemonsOwned[iterateVariable] != NULL)
{
//use that pointer to print out the pokemon name
printf("%s \n", pokemonPointer->pokemonName);
}//end of the if statement that ensures the array has values
//create an else statement that exits out for the looop
else
{
//exit out for the for loop
break;
}//end of the else statement
}//end of the loop that will iterate through array
}//end of the function that will display all the details of the player
//create the main function
int main(){
//add the pokemons to the pokemon list
addPokemonList("Charmander", "Fire", "Blaze");
addPokemonList("Squirtle", "Water", "Torrent");
addPokemonList("Bulbasaur", "Grass", "Overgrow");
addPokemonList("Butterfree", "Bug", "Compound Eyes");
addPokemonList("Pikipek", "Flying", "Keen Eyes");
addPokemonList("Venomoth", "Bug", "Sheild Dust");
addPokemonList("Piplup", "Water", "Torrent");
addPokemonList("Ducklett", "Water", "Keen Eyes");
//add the player to the player list
addPlayerList("Ash");
addPlayerList("Misty");
addPlayerList("Dawn");
//add the pokemons ash owns to his node
addPokemonToPlayer("Ash", "Charmander");
addPokemonToPlayer("Ash", "Squirtle");
addPokemonToPlayer("Ash", "Venomoth");
//add the pokemons that are owned by Misty
addPokemonToPlayer("Misty", "Butterfree");
addPokemonToPlayer("Misty", "Piplup");
//add the pokemon that is owned by dawn
addPokemonToPlayer("Dawn", "Ducklett");
//create a while loop so the user can keep determine whats right
while (1)
{
//ask the user what they'd like to do
printf("Which of the following would you like to do: \n");
printf("1. Display the list of pokemons present in the pokedox\n");
printf("2. Display a list of players present in a pokedox\n");
printf("3. Display details of a specific pokemon\n");
printf("4. Display details of a speicfic player\n");
printf("Please enter the corresponding number to your choice below: \n");
//create a int variable that will act as the user input
int userInput;
//use the scanner to get the user input
scanf("%d", &userInput);
//create an if statement for when the user input is 1
if (userInput == 1)
{
//call the function that prints the pokemons present
listPokemons();
}//end of the if statement for when the user input is 1
//create an if statement for when the user input is 2
if (userInput == 2)
{
//call the function that prints out the list of players
listPlayers();
}//end of the if statement if the user input is 2
//create an if statement for when the user input is 3
if (userInput == 3)
{
//print out the list of the pokemons
listPokemons();
//ask the user which of the pokemons details they want printed
printf("Out of these pokemons, which one's details would you like to see?\n");
printf("Please enter the Pokemon's name below:\n");
//create a variable that get the user input
char userInputPokemonName[50];
//get the pokemon name from the user
scanf("%s", userInputPokemonName);
//use this variable to call the function that displays the details of that pokemon
displayPokemonDetails(userInputPokemonName);
}//end of the if statement for when the user input is 3
//create an if statement for when the user input is 4
if (userInput == 4)
{
//print out the list of players
listPlayers();
//ask the user which of the player details they'd like to see
printf("Out of these players, whose details would you like to see?\n");
printf("Please enter the Player's name below:\n");
//create a variable that gets the input from the user
char userInputPlayerName[50];
//get the player name from the user
scanf("%s", userInputPlayerName);
//use this variable to call the function that displays the list of the players
displayPlayerDetails(userInputPlayerName);
}//end of the if statement for when the user input is 4
}//end of the while loop
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment