有代码吗
时间: 2024-02-28 15:57:13 浏览: 18
以下是一个简单的人口结构与房地场模型的C++代码示例,仅供参考:
```cpp
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 住房类型
enum class HouseType {
Villa,
Apartment,
Townhouse
};
// 人口类
class Person {
public:
Person(string name, int age, string gender, string job)
: name_(name), age_(age), gender_(gender), job_(job) {}
// 设置住房信息
void SetHouseInfo(HouseType house_type, string address, double price) {
house_type_ = house_type;
address_ = address;
price_ = price;
}
private:
string name_;
int age_;
string gender_;
string job_;
// 住房信息
HouseType house_type_;
string address_;
double price_;
};
// 住房类
class House {
public:
House(HouseType house_type, string address, double price)
: house_type_(house_type), address_(address), price_(price) {}
// 添加居住人口
void AddResident(Person* person) {
residents_.push_back(person);
}
private:
HouseType house_type_;
string address_;
double price_;
// 居住人口
vector<Person*> residents_;
};
// 地图类
class Map {
public:
Map(int width, int height) : width_(width), height_(height) {
// 初始化地图
for (int i = 0; i < width_; i++) {
vector<House*> row;
for (int j = 0; j < height_; j++) {
row.push_back(nullptr);
}
map_.push_back(row);
}
}
// 添加住房
void AddHouse(House* house, int x, int y) {
map_[x][y] = house;
}
private:
int width_;
int height_;
// 地图
vector<vector<House*>> map_;
};
int main() {
// 初始化随机数生成器
srand(time(NULL));
// 初始化地图
Map map(10, 10);
// 添加住房
House house1(HouseType::Villa, "123 Main St.", 1000000);
map.AddHouse(&house1, 3, 4);
House house2(HouseType::Apartment, "456 Elm St.", 500000);
map.AddHouse(&house2, 6, 7);
// 添加人口
Person person1("Tom", 30, "Male", "Engineer");
person1.SetHouseInfo(HouseType::Villa, "123 Main St.", 1000000);
house1.AddResident(&person1);
Person person2("Jane", 25, "Female", "Teacher");
person2.SetHouseInfo(HouseType::Apartment, "456 Elm St.", 500000);
house2.AddResident(&person2);
// 模拟人口的行为
for (int i = 0; i < 10; i++) {
int x = rand() % 10;
int y = rand() % 10;
if (map[x][y] != nullptr) {
// 住房已经有人了,不进行操作
continue;
}
// 随机选择一个住房进行购买或租赁
HouseType house_type = static_cast<HouseType>(rand() % 3);
double price = rand() % 1000000;
House* house = new House(house_type, "", price);
map.AddHouse(house, x, y);
// 更新人口和住房信息
Person* person = new Person("New Resident", 20, "Male", "Student");
person->SetHouseInfo(house_type, "", price);
house->AddResident(person);
}
return 0;
}
```
这段代码实现了一个简单的人口结构与房地场模型,包括人口类、住房类、地图类等等。在主函数中,首先初始化了地图和一些住房,然后模拟了人口的行为,随机选择一个住房进行购买或租赁,并更新人口和住房的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)