请在你所编辑的代码中再多加入一些关于传播通信的内容并加以解释
时间: 2024-02-11 16:07:36 浏览: 20
以下是对之前的代码进行修改,加入了传播通信的功能:
```c++
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
// 船舶结构体
struct Ship {
int id; // 船舶编号
string name; // 船名
string type; // 船型
double length; // 长度
double width; // 宽度
double depth; // 深度
};
// 通信结构体
struct Communication {
int id; // 通信编号
int from; // 发送方编号
int to; // 接收方编号
string content; // 通信内容
};
// 定义一个船舶集合类
class ShipCollection {
private:
vector<Ship> ships; // 存储船舶信息的向量
vector<Communication> communications; // 存储通信信息的向量
public:
ShipCollection() {} // 默认构造函数
~ShipCollection() {} // 析构函数
void addShip(const Ship& ship); // 添加船舶
void removeShip(int id); // 删除船舶
Ship getShip(int id); // 获取船舶信息
void printAllShips(); // 打印所有船舶信息
void sendCommunication(int from, int to, const string& content); // 发送通信
void printAllCommunications(); // 打印所有通信信息
};
// 添加船舶
void ShipCollection::addShip(const Ship& ship) {
ships.push_back(ship);
}
// 删除船舶
void ShipCollection::removeShip(int id) {
for (vector<Ship>::iterator it = ships.begin(); it != ships.end(); it++) {
if (it->id == id) {
ships.erase(it);
break;
}
}
}
// 获取船舶信息
Ship ShipCollection::getShip(int id) {
for (vector<Ship>::iterator it = ships.begin(); it != ships.end(); it++) {
if (it->id == id) {
return *it;
}
}
return Ship();
}
// 打印所有船舶信息
void ShipCollection::printAllShips() {
for (vector<Ship>::iterator it = ships.begin(); it != ships.end(); it++) {
cout << "船舶编号:" << it->id << endl;
cout << "船名:" << it->name << endl;
cout << "船型:" << it->type << endl;
cout << "长度:" << it->length << endl;
cout << "宽度:" << it->width << endl;
cout << "深度:" << it->depth << endl;
cout << endl;
}
}
// 发送通信
void ShipCollection::sendCommunication(int from, int to, const string& content) {
Communication communication;
communication.id = communications.size() + 1;
communication.from = from;
communication.to = to;
communication.content = content;
communications.push_back(communication);
}
// 打印所有通信信息
void ShipCollection::printAllCommunications() {
for (vector<Communication>::iterator it = communications.begin(); it != communications.end(); it++) {
cout << "通信编号:" << it->id << endl;
cout << "发送方编号:" << it->from << endl;
cout << "接收方编号:" << it->to << endl;
cout << "通信内容:" << it->content << endl;
cout << endl;
}
}
int main() {
ShipCollection collection;
Ship ship1 = {1, "船1", "型号1", 100.0, 20.0, 8.0};
Ship ship2 = {2, "船2", "型号2", 200.0, 30.0, 10.0};
Ship ship3 = {3, "船3", "型号3", 300.0, 40.0, 12.0};
collection.addShip(ship1);
collection.addShip(ship2);
collection.addShip(ship3);
collection.printAllShips();
collection.sendCommunication(1, 2, "你好,船2!");
collection.sendCommunication(2, 1, "你好,船1!");
collection.sendCommunication(2, 3, "你好,船3!");
collection.printAllCommunications();
collection.removeShip(2);
collection.printAllShips();
collection.printAllCommunications();
return 0;
}
```
在之前的基础上,我们新增了一个 `Communication` 通信结构体,包括通信编号、发送方编号、接收方编号和通信内容等属性。并将它加入到 `ShipCollection` 船舶集合类中,在其中新增了发送通信和打印所有通信信息的函数。
在主函数中,我们分别给船舶之间发送了几条通信,然后打印了所有通信信息。这样,我们就可以通过这个程序对船舶之间的信息进行传播和通信,实现更好的协作和协调。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)