第一个问题是让shipWeapons成为船舶的一部分...当我将其主要放在外面时,它可以识别它,但是后来我认为每艘船都没有自己的载体
我也很确定读取数据时会出错...我们的老师说所有内容都必须以字符的形式读取?
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector <ship> ships;
//vector <weapons> shipWeapons;
void printAll();
void strongestWeapon();
void bestAllAround();
void weakestShip();
void unArmed();
class weapons {
public:
string weaponName;
int firePower;
float consumeFloat;
};
class ship {
public:
//void read(ifstream &file)
string name;
string classType;
short length;
int shield;
float speed;
vector <weapons> shipWeapons; // how to make ship weapons part of ships
///int *weaponsPtr = shipWeapons[0];
};
void read(ifstream &file) {
ship singleShip;
weapons singleWeapon;
int nameLength;
int classLength;
int tempClass;
int tempLength;
int tempShield;
int tempSpeed;
int weaponsNum;
while (!file.eofbit) {
file.read((char *)nameLength, 4);
char *tempName = new char[nameLength]; //to allocate without tknowing the size
file.read(tempName, nameLength);
file.read((char *)classLength, 4);
char *tempClass = (char *)malloc(classLength);
file.read(tempClass, classLength);
file.read((char *)tempLength, 2);
file.read((char *)tempShield, 4);
file.read((char *)tempSpeed, 4);
file.read((char *)weaponsNum, 4);
singleShip.name = tempName;
delete tempName;
singleShip.classType = tempClass;
delete tempClass;
singleShip.length = tempLength;
singleShip.shield = tempShield; //but if change it here it will lose information
singleShip.speed = tempSpeed;
for (int i = 0; i < weaponsNum; i++) {
int weaponNameLength;
int tempWeaponPower;
int tempWeaponConsume;
file.read((char *)weaponNameLength, 4);
char *tempWeaponName = new char[weaponNameLength];
file.read(tempWeaponName, weaponNameLength);
file.read((char*)tempWeaponPower, 4);
file.read((char *)tempWeaponConsume, 4);
singleWeapon.weaponName = tempName;
singleWeapon.firePower = tempWeaponPower;
singleWeapon.consumeFloat = tempWeaponConsume;
shipWeapons.push_back(singleWeapon); //has to be a part of ships though
}
ships.push_back(singleShip);
}
};
void printAll() {
for (int i = 0; i < size(ships); i++) {
cout << "Name: " << ships[i].name << endl;
cout << "Class: " << ships[i].classType << endl;
cout << "Length: " << ships[i].length << endl;
cout << "Shield Capacity: " << ships[i].shield << endl;
cout << "Maximum Warp: " << ships[i].speed << endl;
cout << "Armaments: " << endl;
if (shipWeapons.empty()) {
cout << "Unarmed" << endl << endl;
}
else {
int totalFirePower = 0;
for (int i = 0; i < shipWeapons.size(); i++) {
cout << shipWeapons[i].weaponName << ", ";
cout << shipWeapons[i].firePower << ", ";
cout << shipWeapons[i].consumeFloat << endl;
totalFirePower += shipWeapons[i].firePower;
}
cout << "Total firepower: " << totalFirePower << endl << endl;
}
}
}
void strongestWeapon() {
//store the strongest weapon to compare to next weapons
weapons strongestWeapon = ships[0].shipWeapons[0];
for (int i = 0; i < ships.size(); i++) {
for (int j = 0; j < shipWeapons.size(); i++) {
if (ships[i].shipWeapons[j].firepower > strongestWeapon.firePower) {
strongestWeapon = ships[i].shipWeapons[j].firepower;
}
}
}
//wait that is just the weapon....
cout << strongestWeapon << endl; //no good??
}
//THIS ONE IS ROUGH
void bestAllAround() {
//highest combined power rating of all weapons
int strongestArsenal = 0;
int myArsenal = 0;
ship bestAA;
for (int i = 0; i < ships.size(); i++) {
for (int j = 0; j < shipWeapons.size(); i++) {
myArsenal += ships[i].shipWeapons[j].firepower;
if (myArsenal > strongestArsenal) {
bestAA = ships[i];
}
}
}
}
void weakestShip() {
//weakest not including ones without weapons
vector <ship> armed;
weapons weakest = ships[0].shipweapons[0];
for (int i = 0; i < ships.size(); i++) {
for (int j = 0; j < shipWeapons.size(); i++) {
if (ships[i].shipWeapons != NULL) {
armed.push_back(ships[i]);
}
if (armed[i].shipWeapons[j].firepower < weakest.firePower) {
weakest = armed[i].shipWeapons[j];
}
}
}
}
void unArmed() {
//if unarmed
vector <ship> unarmed;
for (int i = 0; i < ships.size(); i++) {
for (int j = 0; j < shipWeapons.size(); i++) {
if (ships[i].shipWeapons == NULL) {
unarmed.push_back(ships[i]);
}
}
}
}
int main()
{
cout << "Which file(s) to open?\n";
cout << "1. friendlyships.shp" << endl;
cout << "2. enemyships.shp" << endl;
cout << "3. Both files" << endl;
int option;
cin >> option;
/* Load files here */
ifstream fileEnemy;
ifstream fileFriendly;
fileEnemy.open("enemyships.shp");
fileFriendly.open("friendlyships.shp");
if (option == 1) {
read(fileEnemy); // , vector <ship> enemyShips);
}
if (option == 2) {
read(fileFriendly); //, vector <ship> friendlyShips);
}
if (option == 3) {
read(fileFriendly);
read(fileEnemy);
}
cout << "1. Print all ships" << endl;
cout << "2. Starship with the strongest weapon" << endl;
cout << "3. Strongest starship overall" << endl;
cout << "4. Weakest ship (ignoring unarmed)" << endl;
cout << "5. Unarmed ships" << endl;
cin >> option;
/* Work your magic here */
switch (option)
{
case 1:
printAll();
break;
case 2:
strongestWeapon();
break;
case 3:
bestAllAround();
break;
case 4:
weakestShip();
break;
case 5:
unArmed();
break;
default:
break;
}
return 0;
}