结构体数组与传递结构体变量

说明:

结构体:敌舰

函数:初始化敌舰,升级敌舰

代码:

#include 
using namespace std;

//敌舰结构体
struct EnemySpaceShip
{
	int x_coordinate;
	int y_coordinate;
	int weapon_power;
};
//生产敌舰
EnemySpaceShip getNewEnemy()
{
	EnemySpaceShip ship;
	ship.x_coordinate = 0;
	ship.y_coordinate = 0;
	ship.weapon_power = 10;
	return ship;
}

//升级敌舰
EnemySpaceShip upgradeWeapons(EnemySpaceShip ship)
{
	ship.weapon_power += 10;
	return ship;
}

int main()
{
	//敌舰被创建
	EnemySpaceShip ship[5] = { getNewEnemy(), getNewEnemy(), getNewEnemy(), getNewEnemy(), getNewEnemy() };
	cout << "Enemy Space Ships have been built!" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Ship[" << i + 1 << "]: (" << ship[i].x_coordinate << "," << ship[i].y_coordinate << ") Weapons: " << ship[i].weapon_power << endl;
	}
	//升级编号为偶数的敌舰
	ship[1] = upgradeWeapons(ship[0]);
	ship[3] = upgradeWeapons(ship[2]);
	//升级后的敌舰情况
	cout << "----------UPGRADE WEAPONS----------" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "Ship[" << i + 1 << "]: (" << ship[i].x_coordinate << "," << ship[i].y_coordinate << ") Weapons: " << ship[i].weapon_power << endl;
	}
}


你可能感兴趣的:(杂小程序)