pokemon.cpp file: Not to be changed #include \"pokemon2.h\" #include \"pokedex.h
ID: 3872798 • Letter: P
Question
pokemon.cpp file: Not to be changed
#include "pokemon2.h"
#include "pokedex.h"
#include
#include
using namespace std;
// A summary string is a single string that contains all of a Pokemon's information.
// A Pokemon with one type and Ndex 1 has a summary string of the form:
// "Name, #001, type1,"
// Similarly, a Pokemon with two types and Ndex 2 has a summary string of the form:
// "Name, #002, type1, type2,"
// Initializes a Pokemon from a summary string
Pokemon::Pokemon(string summary)
{
string name;
int index[3];
string type1;
string type2;
int count = 0;
int comma = 0;
while (summary[count] != '')
{
if (summary[count] == ',')
{
++comma;
if (comma == 1)
{
for (int i = 0; i < count; ++i)
{
name += summary[i];
}
index[0] = summary[count + 3] - 48;
index[1] = summary[count + 4] - 48;
index[2] = summary[count + 5] - 48;
}
if (comma == 2)
{
for (int i = count + 2; summary[i] != ','; ++i)
{
type1 += summary[i];
}
}
if (comma == 3 && summary[count + 1] == ' ')
{
for (int i = count + 2; summary[i] != ','; ++i)
{
type2 += summary[i];
}
}
}
++count;
}
if (comma == 3)
{
this->type_count = 1;
}
else
{
this->type_count = 2;
}
this->types[0] = string_to_type(type1);
this->types[1] = string_to_type(type2);
this->_name = name;
this->_ndex = index[0] * 100 + index[1] * 10 + index[2];
}
string Pokemon::summary()
{
string summary;
string separate = ", ";
string num = "#";
string name = this->_name;
string index;
string type1;
string type2;
type1 = type_to_string(this->types[0]);
if (is_multitype())
{
type2 = type_to_string(this->types[1]);
type2 += ",";
}
type1 += ",";
if (this->_ndex < 100)
{
index = "0" + to_string(this->_ndex);
if (this->_ndex < 10)
{
index = "00" + to_string(this->_ndex);
}
}
else
{
index = to_string(this->_ndex);
}
summary = name + separate + num + index + separate + type1;
if (is_multitype())
{
summary += " " + type2;
}
return summary;
}
// Same as hwPKM1
Pokemon::Pokemon(string name, int ndex, Type type1)
{
this->_name = name;
this->_ndex = ndex;
this->types[0] = type1;
this->types[1] = type1;
this->type_count = 1;
}
Pokemon::Pokemon(string name, int ndex, Type type1, Type type2)
{
this->_name = name;
this->_ndex = ndex;
this->types[0] = type1;
this->types[1] = type2;
this->type_count = 2;
}
string Pokemon::name()
{
return _name;
}
int Pokemon::Ndex()
{
return _ndex;
}
Pokemon::Type Pokemon :: type1()
{
return types[0];
}
bool Pokemon::is_multitype()
{
if (type_count == 1)
{
return false;
}
return true;
}
Pokemon::Type Pokemon :: type2()
{
return types[1];
}
float Pokemon::damage_multiplier(Type attack_type)
{
float array[4][4] = { { 1.0,1.0,1.0,1.0 },{ 2.0,1.0,0.5,0.5 },{ 1.0,2.0,1.0,1.0 },{ 1.0,1.0,1.0,0.5 } };
float multiplier = array[attack_type][types[0]];
if (is_multitype() == true)
{
multiplier = multiplier * array[attack_type][types[1]];
}
return multiplier;
}
// Returns a string corresponding to the type. Examples:
// 1. type_to_string(Pokemon::Poison) returns "Poison".
// 2. type_to_string(Pokemon::Normal) returns "Normal".
string type_to_string(Pokemon::Type t)
{
if (t == Pokemon::Normal)
{
return "Normal";
}
if (t == Pokemon::Fighting)
{
return "Fighting";
}
if (t == Pokemon::Flying)
{
return "flying";
}
if (t == Pokemon::Poison)
{
return "Poison";
}
}
// Returns the type corresponding to a string. Examples:
// 1. string_to_type("Poison") returns Pokemon::Poison.
// 2. string_to_type("Normal") returns Pokemon::Normal.
// 3. Allowed to return anything if given string doesn't
// correspond to a Pokemon type.
Pokemon::Type string_to_type(string s)
{
if (s == "Normal")
{
return Pokemon::Normal;
}
if (s == "Fighting")
{
return Pokemon::Fighting;
}
if (s == "Flying")
{
return Pokemon::Flying;
}
if (s == "Poison")
{
return Pokemon::Poison;
}
}
pokedex.txt file: not to be changed just used for input/output file and read/write to implement pokedex.cpp
Explanation / Answer
Please find the code below :
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
Pokedex::Pokedex()
{
for(int i=0; i<1000; i++)
A[i] = nullptr;
}
Pokedex::Pokedex(string filename)
{
string summary;
Pokemon *p;
ifstream myfile (filename);
if (myfile.is_open())
{
while ( getline (myfile,summary) )
{
p = new Pokemon(summary);
A[p->_ndex] = p;
}
myfile.close();
}
}
void Pokedex::save(string filename)
{
ofstream myfile (filename);
if (myfile.is_open())
{
for(int i=0; i<1000; i++)
{
if(A[i] != nullptr)
myfile << A[i];
}
myfile.close();
}
else cout << "Unable to open file";
}
void Pokedex::add(Pokemon* p)
{
A[p->_ndex] = p;
}
void Pokedex::remove(Pokemon* p)
{
A[p->_ndex] = nullptr;
}
Pokemon* Pokedex::lookup_by_name(string name)
{
for(int i=0; i<1000; i++)
{
if(strcmpi(A[i]->_name,name) == 0)
{
return A[i];
}
}
return nullptr;
}
Pokemon* Pokedex::lookup_by_Ndex(int ndex)
{
for(int i=0; i<1000; i++)
{
if(strcmpi(A[i]->_ndex,ndex) == 0)
{
return A[i];
}
}
return nullptr;
}
int Pokedex::size()
{
for(int i=0,cnt=0; i<1000; i++)
{
if(A[i] != nullptr)
cnt++;
}
return cnt;
}
Please integrate it with your current code and let me know if you face any error.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.