YU-GI-OH! For this assignment, you will be provided one text file named monsters.txt containing the description of monsters in the following format: Blue-Eyes White Dragon 3000 2500 The monsters will be read into an array as a struct. Your task is to sort and search accordingly with a given feature (attack or defence).
procedure bubbleSort(A : list of sortable items)
n := length(A)
repeat
swapped = false
for i := 1 to n - 1 inclusive do
/* if this pair is out of order */
if A[i - 1] > A[i] then
/* swap them and remember something changed */
swap(A[i - 1], A[i])
swapped := true
end if
end for
until not swapped
end procedure
<CARD>
<NAME>Blue-Eyes White Dragon</NAME>
<ATK>3000</ATK>
<DEF>2500</DEF>
</CARD>
<CARD>
<NAME>Dark Magician</NAME>
<ATK>2500</ATK>
<DEF>2100</DEF>
</CARD>
<CARD>
<NAME>Exodia The Forbidden One</NAME>
<ATK>5000</ATK>
<DEF>5000</DEF>
</CARD>
<CARD>
<NAME>Celtic Guardian</NAME>
<ATK>1400</ATK>
<DEF>1200</DEF>
</CARD>
<CARD>
<NAME>Copycat</NAME>
<ATK>0</ATK>
<DEF>0</DEF>
</CARD>
<CARD>
<NAME>Time Wizard</NAME>
<ATK>500</ATK>
<DEF>400</DEF>
</CARD>
<CARD>
<NAME>Blue-Eyes White Dragon</NAME>
<ATK>3000</ATK>
<DEF>2500</DEF>
</CARD>
<CARD>
<NAME>Baby Dragon</NAME>
<ATK>1200</ATK>
<DEF>700</DEF>
</CARD>
<CARD>
<NAME>Kuriboh</NAME>
<ATK>300</ATK>
<DEF>200</DEF>
</CARD>
<CARD>
<NAME>Summoned Skull</NAME>
<ATK>2500</ATK>
<DEF>1200</DEF>
</CARD>
<CARD>
<NAME>Blue-Eyes White Dragon</NAME>
<ATK>3000</ATK>
<DEF>2500</DEF>
</CARD>
<CARD>
<NAME>Red-Eyes B. Dragon</NAME>
<ATK>2400</ATK>
<DEF>200</DEF>
</CARD>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
// A monster structure
struct Monster
{
string name;
int attack;
int diffence;
};
// Swap functions
void swap(Monster *xp, Monster *yp)
{
Monster temp = *xp;
*xp = *yp;
*yp = temp;
}
// Bubble sort
void bubbleSort(Monster arr[], int n , string x)
{
int i, j;
if(x == "diffence")
{
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].diffence < arr[j+1].diffence)
swap(&arr[j], &arr[j+1]);
}
else
{
if(x == "attack")
{
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].attack <
arr[j+1].attack)
swap(&arr[j],
&arr[j+1]);
}
}
}
int main(){
ifstream
file("/home/baderddine/Bureau/monsters.txt");
// For lines
int i=1;
// For the array
int j=0;
Monster monsters[12];
string str;
// Read from file and store in the array
while (std::getline(file, str)) {
if(i==7)
{
i = 1;
j++;
}
else
{
// This is the name line
if ( i == 2 )
{
monsters[j].name =
str.substr(6, str.length()-13);
}
// This is the attack line
if ( i == 3 )
{
stringstream
stream(str.substr(5, str.length()-11));
int x= 0;
stream >> x;
monsters[j].attack = x;
}
// This is the diffence line
if ( i == 4 )
{
stringstream
stream(str.substr(5, str.length()-11));
int x= 0;
stream >> x;
monsters[j].diffence =
x;
}
}
i++;
}
cout<< "************************ Sort by attack points
****************************" << endl;
bubbleSort(monsters,12,"attack");
for(int i=0;i<12;i++)
cout<< monsters[i].name
<< " " << monsters[i].attack <<
endl;
cout<<endl<<endl;
cout<< "************************ Sort by diffence points
****************************" << endl;
bubbleSort(monsters,12,"diffence");
for(int i=0;i<12;i++)
cout<< monsters[i].name
<< " " << monsters[i].diffence <<
endl;
return 0;
}
//Screenshot
Comment down if you have any queries related to this answer.
Please give a thumbs up.
YU-GI-OH! For this assignment, you will be provided one text file named monsters.txt containing the description...