Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following files have been given to you, rememeber that only the pokemon.cpp and pokedex.cpp are to be made. Main.cpp and both .h files are
The following files have been given to you, rememeber that only the pokemon.cpp and pokedex.cpp are to be made. Main.cpp and both .h files are given and not to be changed, unless the header files need to to be included. Only use very basic c++ statements, very basic syntax.
1. A C++ header file (pokemon.h) declaring the Pokemon class.
#ifndef POKEMON_H#define POKEMON_H#include using namespace std;class Pokemon{ public: enum Type {Normal, Fighting, Flying, Poison}; // 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(string summary); // Returns the summary string of the Pokemon string summary(); Pokemon(string name, int ndex, Type type1); Pokemon(string name, int ndex, Type type1, Type type2); string name(); int Ndex(); Type type1(); bool is_multitype(); Type type2(); float damage_multiplier(Type attack_type); private: int _ndex; // Stores the Pokemon's Ndex string _name; // Stores the Pokemon's name Type types[2]; // Stores the Pokemon's types (1 or 2 of them) int type_count; // Stores how many types the Pokemon has};// 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);// 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);#endif2. A C++ header file (pokedex.h) declaring the Pokedex class.
#ifndef POKEDEX_H#define POKEDEX_H#include #include \"pokemon.h\"using namespace std;class Pokedex{ public: // A file containing a Pokedex should contain a line // for each Pokemon in the Pokedex. This line // should be the summary string of the Pokemon. // See pokemon.txt for an example. // Constructs an empty pokedex. Pokedex(); // Constructs a Pokedex with the Pokemon found in // the specified file. Pokedex(string filename); // Writes the Pokedex to the file. void save(string filename); // Adds a pokemon to the pokedex. void add(Pokemon* p); // Removes a pokemon from the pokedex. void remove(Pokemon* p); // Returns a (pointer to a) pokemon in the pokedex with the given name. // If none exists, returns nullptr. // // Hint: loop through all of A, searching for a Pokemon with // the given name. Return the first one found. Pokemon* lookup_by_name(string name); // Returns a (pointer to a) pokemon in the pokedex with the given name. // If none exists, returns nullptr. // // Hint: look in A[ndex]. Pokemon* lookup_by_Ndex(int ndex); // Returns the number of pokemon in the pokedex. int size(); private: // The pokedex is represented as an array of Pokemon pointers. // // Hint: // 1. Initialize the values in A to nullptr. // 2. When adding a Pokemon('s pointer), store the pointer in // the array index equal to the Pokemon's Ndex. // 3. When removing a Pokemon('s pointer), do so by setting the // corresponding array index equal to nullptr. Pokemon* A[1000];};#endif3. A C++ source file (main.cpp) containing a main function with tests.
#include #include #include #include #include #include \"pokedex.h\"using namespace std;inline void _test(const char* expression, const char* file, int line){ cerr abort();}#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))int main(){ // ***** Test Pokemon ***** // Test type_to_string() test(type_to_string(Pokemon::Normal) == \"Normal\"); test(type_to_string(Pokemon::Fighting) == \"Fighting\"); test(type_to_string(Pokemon::Flying) == \"Flying\"); test(type_to_string(Pokemon::Poison) == \"Poison\") // Test string_to_type() test(string_to_type(\"Normal\") == Pokemon::Normal); test(string_to_type(\"Fighting\") == Pokemon::Fighting); test(string_to_type(\"Flying\") == Pokemon::Flying); test(string_to_type(\"Poison\") == Pokemon::Poison); string_to_type(\"Sleepy\"); // Test string-based constructor via // checking that instance variables were set correctly. Pokemon bouffalant(\"Bouffalant, #626, Normal,\"); Pokemon mankey(\"Mankey, #056, Fighting,\"); Pokemon tornadus(\"Tornadus, #641, Flying,\"); Pokemon grimer(\"Grimer, #088, Poison,\"); Pokemon timburr(\"Timburr, #532, Fighting,\"); Pokemon tonkamon(\"Tonkamon, #001, Flying,\"); Pokemon pidgey(\"Pidgey, #016, Normal, Flying,\"); Pokemon fletchling(\"Fletchling, #661, Normal, Flying,\"); Pokemon zubat(\"Zubat, #041, Poison, Flying,\"); Pokemon toxicroak(\"Toxicroak, #454, Poison, Fighting,\"); Pokemon golbat(\"Golbat, #042, Poison, Flying,\"); Pokemon hoothoot(\"Hoothoot, #163, Flying, Normal,\"); // Check that _name was set correctly test(bouffalant.name() == \"Bouffalant\"); test(mankey.name() == \"Mankey\"); test(tornadus.name() == \"Tornadus\"); test(grimer.name() == \"Grimer\"); test(timburr.name() == \"Timburr\"); test(tonkamon.name() == \"Tonkamon\"); test(pidgey.name() == \"Pidgey\"); test(fletchling.name() == \"Fletchling\"); test(zubat.name() == \"Zubat\"); test(toxicroak.name() == \"Toxicroak\"); test(golbat.name() == \"Golbat\"); test(hoothoot.name() == \"Hoothoot\"); // Check that _ndex was set correctly test(bouffalant.Ndex() == 626); test(mankey.Ndex() == 56); test(tornadus.Ndex() == 641); test(grimer.Ndex() == 88); test(timburr.Ndex() == 532); test(tonkamon.Ndex() == 1); test(pidgey.Ndex() == 16); test(fletchling.Ndex() == 661); test(zubat.Ndex() == 41); test(toxicroak.Ndex() == 454); test(golbat.Ndex() == 42); test(hoothoot.Ndex() == 163); // Check that types were set correctly test(bouffalant.type1() == Pokemon::Normal); test(!bouffalant.is_multitype()); test(mankey.type1() == Pokemon::Fighting); test(!mankey.is_multitype()); test(tornadus.type1() == Pokemon::Flying); test(!tornadus.is_multitype()); test(grimer.type1() == Pokemon::Poison); test(!grimer.is_multitype()); test(timburr.type1() == Pokemon::Fighting); test(!timburr.is_multitype()); test(tonkamon.type1() == Pokemon::Flying); test(!tonkamon.is_multitype()); test(pidgey.type1() == Pokemon::Normal); test(pidgey.is_multitype()); test(pidgey.type2() == Pokemon::Flying); test(fletchling.type1() == Pokemon::Normal); test(fletchling.is_multitype()); test(fletchling.type2() == Pokemon::Flying); test(zubat.type1() == Pokemon::Poison); test(zubat.is_multitype()); test(zubat.type2() == Pokemon::Flying); test(toxicroak.type1() == Pokemon::Poison); test(toxicroak.is_multitype()); test(toxicroak.type2() == Pokemon::Fighting); test(golbat.type1() == Pokemon::Poison); test(golbat.is_multitype()); test(golbat.type2() == Pokemon::Flying); test(hoothoot.type1() == Pokemon::Flying); test(hoothoot.is_multitype()); test(hoothoot.type2() == Pokemon::Normal); // Test summary() test(bouffalant.summary() == \"Bouffalant, #626, Normal,\"); test(mankey.summary() == \"Mankey, #056, Fighting,\"); test(tornadus.summary() == \"Tornadus, #641, Flying,\"); test(grimer.summary() == \"Grimer, #088, Poison,\"); test(timburr.summary() == \"Timburr, #532, Fighting,\"); test(tonkamon.summary() == \"Tonkamon, #001, Flying,\"); test(pidgey.summary() == \"Pidgey, #016, Normal, Flying,\"); test(fletchling.summary() == \"Fletchling, #661, Normal, Flying,\"); test(zubat.summary() == \"Zubat, #041, Poison, Flying,\"); test(toxicroak.summary() == \"Toxicroak, #454, Poison, Fighting,\"); test(golbat.summary() == \"Golbat, #042, Poison, Flying,\"); test(hoothoot.summary() == \"Hoothoot, #163, Flying, Normal,\"); // ***** Test Pokedex ***** Pokedex D1; // Test add() and size() test(D1.size() == 0); D1.add(&bouffalant); test(D1.size() == 1); D1.add(&mankey); test(D1.size() == 2); D1.add(&tornadus); test(D1.size() == 3); D1.add(&grimer); test(D1.size() == 4); D1.add(&timburr); test(D1.size() == 5); D1.add(&tonkamon); test(D1.size() == 6); D1.add(&pidgey); test(D1.size() == 7); D1.add(&fletchling); test(D1.size() == 8); D1.add(&zubat); test(D1.size() == 9); D1.add(&toxicroak); test(D1.size() == 10); D1.add(&golbat); test(D1.size() == 11); D1.add(&hoothoot); test(D1.size() == 12); D1.add(&bouffalant); test(D1.size() == 12); D1.add(&hoothoot); test(D1.size() == 12); // Test add(), lookup_by_Ndex() test(D1.lookup_by_Ndex(626) == &bouffalant); test(D1.lookup_by_Ndex(56) == &mankey); test(D1.lookup_by_Ndex(641) == &tornadus); test(D1.lookup_by_Ndex(88) == &grimer); test(D1.lookup_by_Ndex(532) == &timburr); test(D1.lookup_by_Ndex(1) == &tonkamon); test(D1.lookup_by_Ndex(16) == &pidgey); test(D1.lookup_by_Ndex(661) == &fletchling); test(D1.lookup_by_Ndex(41) == &zubat); test(D1.lookup_by_Ndex(454) == &toxicroak); test(D1.lookup_by_Ndex(42) == &golbat); test(D1.lookup_by_Ndex(163) == &hoothoot); test(D1.lookup_by_Ndex(12) == nullptr); test(D1.lookup_by_Ndex(34) == nullptr); test(D1.lookup_by_Ndex(567) == nullptr); // Test lookup_by_name() test(D1.lookup_by_name(\"Bouffalant\") == &bouffalant); test(D1.lookup_by_name(\"Mankey\") == &mankey); test(D1.lookup_by_name(\"Tornadus\") == &tornadus); test(D1.lookup_by_name(\"Grimer\") == &grimer); test(D1.lookup_by_name(\"Timburr\") == &timburr); test(D1.lookup_by_name(\"Tonkamon\") == &tonkamon); test(D1.lookup_by_name(\"Pidgey\") == &pidgey); test(D1.lookup_by_name(\"Fletchling\") == &fletchling); test(D1.lookup_by_name(\"Zubat\") == &zubat); test(D1.lookup_by_name(\"Toxicroak\") == &toxicroak); test(D1.lookup_by_name(\"Golbat\") == &golbat); test(D1.lookup_by_name(\"Hoothoot\") == &hoothoot); test(D1.lookup_by_name(\"Lemon\") == nullptr); test(D1.lookup_by_name(\"Fakemon\") == nullptr); test(D1.lookup_by_name(\"Notarealmon\") == nullptr); // Test remove() test(D1.size() == 12); test(D1.lookup_by_name(\"Bouffalant\") == &bouffalant); test(D1.lookup_by_Ndex(626) == &bouffalant); test(D1.lookup_by_name(\"Zubat\") == &zubat); test(D1.lookup_by_Ndex(41) == &zubat); test(D1.lookup_by_name(\"Hoothoot\") == &hoothoot); test(D1.lookup_by_Ndex(163) == &hoothoot); D1.remove(&bouffalant); test(D1.size() == 11); test(D1.lookup_by_name(\"Bouffalant\") == nullptr); test(D1.lookup_by_Ndex(626) == nullptr); D1.remove(&zubat); test(D1.size() == 10); test(D1.lookup_by_name(\"Zubat\") == nullptr); test(D1.lookup_by_Ndex(41) == nullptr); D1.remove(&hoothoot); test(D1.size() == 9); test(D1.lookup_by_name(\"Hoothoot\") == nullptr); test(D1.lookup_by_Ndex(163) == nullptr); test(D1.lookup_by_name(\"Mankey\") == &mankey); test(D1.lookup_by_name(\"Tornadus\") == &tornadus); test(D1.lookup_by_name(\"Grimer\") == &grimer); test(D1.lookup_by_name(\"Timburr\") == &timburr); test(D1.lookup_by_name(\"Tonkamon\") == &tonkamon); test(D1.lookup_by_name(\"Pidgey\") == &pidgey); test(D1.lookup_by_name(\"Fletchling\") == &fletchling); test(D1.lookup_by_name(\"Golbat\") == &golbat); test(D1.lookup_by_name(\"Toxicroak\") == &toxicroak); // Test save() and load(). // This will create and modify files on your computer // in the directory where the program lives. // Delete old version of test_pokedex1.txt if it exists remove(\"./test_pokedex1.txt\"); // Save Mankey, Tornadus, Grimer, Timburr, Tonkamon, // Pidgey, Fletchling, Golbat, Toxicroak D1.save(\"./test_pokedex1.txt\"); // Create a Pokedex with: // Mankey, Tornadus, Grimer, Timburr, Tonkamon, // Pidgey, Fletchling, Golbat, Toxicroak Pokedex D2(\"./test_pokedex1.txt\"); test(D2.size() == 9); test(D2.lookup_by_name(\"Bouffalant\") == nullptr); Pokemon* p = D2.lookup_by_name(\"Mankey\"); test(p != nullptr); test(p->name() == mankey.name()); test(p->Ndex() == mankey.Ndex()); test(p->type1() == mankey.type1()); test(p->is_multitype() == mankey.is_multitype()); p = D2.lookup_by_name(\"Tornadus\"); test(p != nullptr); test(p->Ndex() == tornadus.Ndex()); p = D2.lookup_by_name(\"Grimer\"); test(p != nullptr); test(p->Ndex() == grimer.Ndex()); p = D2.lookup_by_name(\"Timburr\"); test(p != nullptr); test(p->Ndex() == timburr.Ndex()); p = D2.lookup_by_name(\"Tonkamon\"); test(p != nullptr); test(p->Ndex() == tonkamon.Ndex()); p = D2.lookup_by_name(\"Pidgey\"); test(p != nullptr); test(p->name() == pidgey.name()); test(p->Ndex() == pidgey.Ndex()); test(p->type1() == pidgey.type1()); test(p->is_multitype() == pidgey.is_multitype()); test(p->type2() == pidgey.type2()); p = D2.lookup_by_name(\"Fletchling\"); test(p != nullptr); test(p->Ndex() == fletchling.Ndex()); test(D2.lookup_by_name(\"Zubat\") == nullptr); p = D2.lookup_by_name(\"Toxicroak\"); test(p != nullptr); test(p->Ndex() == toxicroak.Ndex()); p = D2.lookup_by_name(\"Golbat\"); test(p != nullptr); test(p->Ndex() == golbat.Ndex()); test(D2.lookup_by_name(\"Hoothoot\") == nullptr); D2.add(&bouffalant); D2.add(&zubat); D2.add(&hoothoot); D2.remove(&mankey); D2.remove(&toxicroak); test(D2.size() == 10); // Remove file made during testing remove(\"./test_pokedex1.txt\"); // Delete old version of test_pokedex2.txt if it exists remove(\"./test_pokedex2.txt\"); // Save Bouffalant, Tornadus, Grimer, Timburr, Tonkamon, // Pidgey, Fletchling, Zubat, Golbat, Hoothoot D2.save(\"./test_pokedex2.txt\"); // Create a Pokedex with: // Bouffalant, Tornadus, Grimer, Timburr, Tonkamon, // Pidgey, Fletchling, Zubat, Golbat, Hoothoot Pokedex D3(\"./test_pokedex2.txt\"); test(D3.size() == 10); p = D3.lookup_by_name(\"Bouffalant\"); test(p->name() == bouffalant.name()); test(p->Ndex() == bouffalant.Ndex()); test(p->type1() == bouffalant.type1()); test(p->is_multitype() == bouffalant.is_multitype()); test(D3.lookup_by_name(\"Mankey\") == nullptr); p = D3.lookup_by_name(\"Tornadus\"); test(p != nullptr); test(p->Ndex() == tornadus.Ndex()); p = D3.lookup_by_name(\"Grimer\"); test(p != nullptr); test(p->Ndex() == grimer.Ndex()); p = D3.lookup_by_name(\"Timburr\"); test(p != nullptr); test(p->Ndex() == timburr.Ndex()); p = D3.lookup_by_name(\"Tonkamon\"); test(p != nullptr); test(p->Ndex() == tonkamon.Ndex()); p = D3.lookup_by_name(\"Pidgey\"); test(p != nullptr); test(p->name() == pidgey.name()); test(p->Ndex() == pidgey.Ndex()); test(p->type1() == pidgey.type1()); test(p->is_multitype() == pidgey.is_multitype()); test(p->type2() == pidgey.type2()); p = D3.lookup_by_name(\"Fletchling\"); test(p != nullptr); test(p->Ndex() == fletchling.Ndex()); p = D3.lookup_by_name(\"Zubat\"); test(p != nullptr); test(p->Ndex() == zubat.Ndex()); p = D3.lookup_by_name(\"Golbat\"); test(p != nullptr); test(p->Ndex() == golbat.Ndex()); test(D3.lookup_by_name(\"Toxicroak\") == nullptr); p = D3.lookup_by_name(\"Hoothoot\"); test(p != nullptr); test(p->Ndex() == hoothoot.Ndex()); // Remove file made during testing remove(\"./test_pokedex2.txt\"); ifstream f; f.open(\"./pokedex.txt\"); assert(f.is_open()); // If this fails, you're missing pokedex.txt f.close(); Pokedex D4(\"./pokedex.txt\"); test(D4.size() == 125); p = D4.lookup_by_name(\"Staraptor\"); test(p != nullptr); test(p->name() == \"Staraptor\"); test(p->Ndex() == 398); test(p->type1() == Pokemon::Normal); test(p->is_multitype()); test(p->type2() == Pokemon::Flying); p = D4.lookup_by_Ndex(143); test(p != nullptr); test(p->name() == \"Snorlax\"); test(p->Ndex() == 143); test(p->type1() == Pokemon::Normal); test(!p->is_multitype()); test(D4.lookup_by_name(\"Pikachu\") == nullptr); test(D4.lookup_by_Ndex(25) == nullptr); cout }4. A text file (pokedex.txt) containing a list of all normal, fighting, flying, and poison Pokemon, one per line, in the summary string format described in pokemon.h.Reading and writing to files is done using the library, specifically the ofstream and ifstream classes. Objects of these classes work roughly like cout and cin, but for files instead of the terminal.Aipom, #190, Normal,Ambipom, #424, Normal,Arbok, #024, Poison,Arceus, #493, Normal,Audino, #531, Normal,Bidoof, #399, Normal,Blissey, #242, Normal,Bouffalant, #626, Normal,Braviary, #628, Normal, Flying,Buneary, #427, Normal,Bunnelby, #659, Normal,Castform, #351, Normal,Chansey, #113, Normal,Chatot, #441, Normal, Flying,Cinccino, #573, Normal,Conkeldurr, #534, Fighting,Croagunk, #453, Poison, Fighting,Crobat, #169, Poison, Flying,Delcatty, #301, Normal,Ditto, #132, Normal,Dodrio, #085, Normal, Flying,Doduo, #084, Normal, Flying,Dunsparce, #206, Normal,Eevee, #133, Normal,Ekans, #023, Poison,Exploud, #295, Normal,Farfetch'd, #083, Normal, Flying,Fearow, #022, Normal, Flying,Fletchling, #661, Normal, Flying,Furfrou, #676, Normal,Furret, #162, Normal,Garbodor, #569, Poison,Glameow, #431, Normal,Golbat, #042, Poison, Flying,Grimer, #088, Poison,Gulpin, #316, Poison,Gurdurr, #533, Fighting,Happiny, #440, Normal,Hariyama, #297, Fighting,Hawlucha, #701, Fighting, Flying,Herdier, #507, Normal,Hitmonchan, #107, Fighting,Hitmonlee, #106, Fighting,Hitmontop, #237, Fighting,Hoothoot, #163, Flying, Normal,Kangaskhan, #115, Normal,Kecleon, #352, Normal,Koffing, #109, Poison,Lickilicky, #463, Normal,Lickitung, #108, Normal,Lillipup, #506, Normal,Linoone, #264, Normal,Lopunny, #428, Normal,Loudred, #294, Normal,Machamp, #068, Fighting,Machoke, #067, Fighting,Machop, #066, Fighting,Makuhita, #296, Fighting,Mankey, #056, Fighting,Meowth, #052, Normal,Mienfoo, #619, Fighting,Mienshao, #620, Fighting,Miltank, #241, Normal,Minccino, #572, Normal,Muk, #089, Poison,Munchlax, #446, Normal,Nidoran, #029, Poison,Nidoran, #032, Poison,Nidorina, #030, Poison,Nidorino, #033, Poison,Noctowl, #164, Normal, Flying,Pancham, #674, Fighting,Patrat, #504, Normal,Persian, #053, Normal,Pidgeot, #018, Normal, Flying,Pidgeotto, #017, Normal, Flying,Pidgey, #016, Normal, Flying,Pidove, #519, Normal, Flying,Porygon, #137, Normal,Porygon-Z, #474, Normal,Porygon2, #233, Normal,Primeape, #057, Fighting,Purugly, #432, Normal,Raticate, #020, Normal,Rattata, #019, Normal,Regigigas, #486, Normal,Riolu, #447, Fighting,Rufflet, #627, Normal, Flying,Sawk, #539, Fighting,Sentret, #161, Normal,Seviper, #336, Poison,Skitty, #300, Normal,Slaking, #289, Normal,Slakoth, #287, Normal,Smeargle, #235, Normal,Snorlax, #143, Normal,Spearow, #021, Normal, Flying,Spinda, #327, Normal,Stantler, #234, Normal,Staraptor, #398, Normal, Flying,Staravia, #397, Normal, Flying,Starly, #396, Normal, Flying,Stoutland, #508, Normal,Swablu, #333, Normal, Flying,Swalot, #317, Poison,Swellow, #277, Normal, Flying,Taillow, #276, Normal, Flying,Tauros, #128, Normal,Teddiursa, #216, Normal,Throh, #538, Fighting,Timburr, #532, Fighting,Tornadus, #641, Flying,Toxicroak, #454, Poison, Fighting,Tranquill, #520, Normal, Flying,Trubbish, #568, Poison,Tyrogue, #236, Fighting,Unfezant, #521, Normal, Flying,Ursaring, #217, Normal,Vigoroth, #288, Normal,Watchog, #505, Normal,Weezing, #110, Poison,Whismur, #293, Normal,Zangoose, #335, Normal,Zigzagoon, #263, Normal,Zubat, #041, Poison, Flying,
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started