чтение C ++ в файле (извините, я отстой) - PullRequest
0 голосов
/ 11 марта 2019

Первая проблема заключается в том, что shipWeapons является частью корабля ... когда я выношу его наружу только в основном, он распознает его, но потом я думаю, что у каждого корабля нет своего вектора

и я довольно симпатичныйнаверняка есть ошибки при чтении данных ... наш учитель сказал, что все должно читаться как символ?

  1. Распечатать все корабли
  2. Распечатать звездолет с помощью самого мощногооружие
  3. Печать самого мощного корабля (максимальная суммарная мощность среди всех видов оружия)
  4. Печать самого слабого корабля (из кораблей, у которого действительно есть оружие)
  5. Печать безоружных кораблей

#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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...