1. 程式人生 > >C++課程設計之模擬一個微型生態系統

C++課程設計之模擬一個微型生態系統

題目: 模擬一個微型生態系統。有青草、兔子、狐狸三種生物。可以用網格表示整個環境,每個格子內用不同字母表示不同事物:空地E青草G兔子R狐狸F。系統規則如下:

1)初始狀態隨機出現GRF;

2)若格子為G,下一輪其周邊八個格子中為E的格子中會至少有一個變為G;

3)若格子為R,若其周圍八個格子都沒有G或其中一個出現F,則下一輪此格子為E;否則其中一個為G的格子變為R;

4)若格子為F,其周圍八個格子都沒有R,則下一輪此格子為E;否則其中一個為R的格子變成F;

5)若格子為E,則下一輪會以一定的概率變為G。

請分析並畫出類結構圖,利用面向物件程式設計思想,完成模擬過程。(必須用到類的繼承、虛擬函式)

問題分析:

感覺沒有很大的必要對每一種生物建立一個類,並進行派生,所以這裡我只建立了兩個類,一個世界(world)作為基類,一個生態系統(ShengTai)作為派生類,繼承於基類(world),輸出函式(Display)作為虛擬函式,建立基類指標呼叫派生類的虛擬函式。類結構圖如下:


程式碼實現:(執行環境為Microsoft Visual Studio 2010)

/*初始化生態系統大小為10*10(可以改變),空地變為青草地的概率為60%(可以改變),規則中某些地方指出某些情況下,至少一個改變或者其中一個地方改變,這裡均按照從上到下從左到右的順序,以出現滿足條件的第一個進行改變,其餘不變。*/

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int col = 10;
const int row = 10;//初始化生態系統大小<col*row>
const int TurnGrass = 60;//空地變為青草地的概率
class world
{
public:
	void Init();
	virtual void Display();
};
void world::Init()
{
	cout<<"world init"<<endl;
}
void world::Display()
{
	cout<<"nothing found,world empty!"<<endl;
}
class ShengTai:public world
{
public:
	void Init();//初始化
	void Develop();//演化
	void Display();//輸出狀態
	void Traverse();//遍歷
private:
	int s[col][row];//存放0,1,2,3,分別代表空地青草兔子狐狸
	char life;//存放生物或者空地
};
void ShengTai::Init()
{
	int i,j;
	srand((unsigned)time(NULL));
	for(i=0;i<col;i++)
		for(j=0;j<row;j++)
			s[i][j] = rand()%(3-0+1)+0;
	Traverse();
}
void ShengTai::Traverse()
{
	int i,j;
	for(i=0;i<col;i++)
		for(j=0;j<row;j++)
			switch(s[i][j])
		{
			case 0:life = 'E';break;
			case 1:life = 'G';break;
			case 2:life = 'R';break;
			case 3:life = 'F';break;
			default:cout<<"error traverse"<<endl;
		}
}
void ShengTai::Display()
{
	int i,j;
	for(i=0;i<col;i++)
	{
		for(j=0;j<row;j++)
			switch(s[i][j])
		{
			case 0:cout<<"E"<<" ";break;
			case 1:cout<<"G"<<" ";break;
			case 2:cout<<"R"<<" ";break;
			case 3:cout<<"F"<<" ";break;
			default:cout<<"error display"<<endl;
		}
		cout<<endl;
	}
	cout<<endl;
}
void ShengTai::Develop()
{
	int i,j,turned = 0;
	int tmp[col][row];
	for(i=0;i<col;i++)
		for(j=0;j<row;j++)
			tmp[i][j] = 5;
	for(i=0;i<col;i++)
		for(j=0;j<row;j++)
			switch(s[i][j])
		{
			case 0:
				{
					int k;
					k = rand()%(100-1+1)+1;
					if(k >= TurnGrass)
						tmp[i][j] = 1;
					break;
				}
			case 1:
				{
					turned = 0;
					if(s[i-1][j-1] == 0)
					{
						tmp[i-1][j-1] = 1;
						turned = 1;
					}
					if(s[i-1][j] == 0&&turned == 0)
					{
						tmp[i-1][j] = 1;
						turned = 1;
					}
					if(s[i-1][j+1] == 0&&turned == 0)
					{
						tmp[i-1][j+1] = 1;
						turned = 1;
					}
					if(s[i][j-1] == 0&&turned == 0)
					{
						tmp[i][j-1] = 1;
						turned = 1;
					}
					if(s[i][j+1] == 0&&turned == 0)
					{
						tmp[i][j+1] = 1;
						turned = 1;
					}
					if(s[i+1][j-1] == 0&&turned == 0)
					{
						tmp[i+1][j-1] = 1;
						turned = 1;
					}
					if(s[i+1][j] == 0&&turned == 0)
					{
						tmp[i+1][j] = 1;
						turned = 1;
					}
					if(s[i+1][j+1] == 0&&turned == 0)
					{
						tmp[i+1][j+1] = 1;
						turned = 1;
					}
					break;
				}
			case 2:
				{
					turned = 0;
					if(s[i-1][j-1] == 3)
					{
						tmp[i-1][j-1] = 3;
						turned = 1;
					}
					if(s[i-1][j] == 3&&turned == 0)
					{
						tmp[i-1][j] = 3;
						turned = 1;
					}
					if(s[i-1][j+1] == 3&&turned == 0)
					{
						tmp[i-1][j+1] = 3;
						turned = 1;
					}
					if(s[i][j-1] == 3&&turned == 0)
					{
						tmp[i][j-1] = 3;
						turned = 1;
					}
					if(s[i][j+1] == 3&&turned == 0)
					{
						tmp[i][j+1] = 3;
						turned = 1;
					}
					if(s[i+1][j-1] == 3&&turned == 0)
					{
						tmp[i+1][j-1] = 3;
						turned = 1;
					}
					if(s[i+1][j] == 3&&turned == 0)
					{
						tmp[i+1][j] = 3;
						turned = 1;
					}
					if(s[i+1][j+1] == 3&&turned == 0)
					{
						tmp[i+1][j+1] = 3;
						turned = 1;
					}
					if(turned == 0&&
						s[i-1][j-1] != 1&&s[i-1][j] != 1&&
						s[i-1][j+1] != 1&&s[i][j-1] != 1&&
						s[i][j+1] != 1&&s[i+1][j-1] != 1&&
						s[i+1][j] != 1&&s[i+1][j+1] != 1)
					{
						if(s[i-1][j-1] == 1)
						{
							tmp[i-1][j-1] = 2;
							turned = 1;
						}
						if(s[i-1][j] == 1&&turned == 0)
						{
							tmp[i-1][j] = 2;
							turned = 1;
						}
						if(s[i-1][j+1] == 1&&turned == 0)
						{
							tmp[i-1][j+1] = 2;
							turned = 1;
						}
						if(s[i][j-1] == 1&&turned == 0)
						{
							tmp[i][j-1] = 2;
							turned = 1;
						}
						if(s[i][j+1] == 1&&turned == 0)
						{
							tmp[i][j+1] = 2;
							turned = 1;
						}
						if(s[i+1][j-1] == 1&&turned == 0)
						{
							tmp[i+1][j-1] = 2;
							turned = 1;
						}
						if(s[i+1][j] == 1&&turned == 0)
						{
							tmp[i+1][j] = 2;
							turned = 1;
						}
						if(s[i+1][j+1] == 1&&turned == 0)
						{
							tmp[i+1][j+1] = 2;
							turned = 1;
						}
					}
					break;
				}
			case 3:
				{
					turned = 0;
					if(i == 0)
					{
						if(j == 0)
						{
							if(s[i][j+1] != 2&&s[i+1][j] != 2&&s[i+1][j+1] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
						else if(j == row-1)
						{
							if(s[i][j-1] != 2&&s[i+1][j-1] != 2&&s[i+1][j] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
						else
						{
							if(s[i][j-1] != 2&&s[i][j+1] != 2&&s[i+1][j-1] != 2&&s[i+1][j] != 2&&s[i+1][j+1] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
					}
					else if(i == col-1)
					{
						if(j == 0)
						{
							if(s[i-1][j] != 2&&s[i-1][j-1] != 2&&s[i][j+1] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
						else if(j == row-1)
						{
							if(s[i-1][j-1] != 2&&s[i-1][j] != 2&&s[i][j-1] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
						else
						{
							if(s[i-1][j-1] != 2&&s[i-1][j] != 2&&s[i-1][j+1] != 2&&s[i][j-1] != 2&&s[i][j+1] != 2&&turned == 0)
							{
								tmp[i][j] = 0;
								turned = 1;
							}
						}
					}
					else
					{
						if(s[i-1][j-1] != 2&&s[i-1][j] != 2&&s[i-1][j+1] != 2&&s[i][j-1] != 2&&s[i][j+1] != 2&&s[i+1][j-1] != 2&&s[i+1][j] != 2&&s[i+1][j+1] != 2)
						{
							tmp[i][j] = 0;
							turned = 1;
						}
					}

					if(s[i-1][j-1] == 2&&turned == 0)
					{
						tmp[i-1][j-1] = 3;
						turned = 1;
					}
					if(s[i-1][j] == 2&&turned == 0)
					{
						tmp[i-1][j] = 3;
						turned = 1;
					}
					if(s[i-1][j+1] == 2&&turned == 0)
					{
						tmp[i-1][j+1] = 3;
						turned = 1;
					}
					if(s[i][j-1] == 2&&turned == 0)
					{
						tmp[i][j-1] = 3;
						turned = 1;
					}
					if(s[i][j+1] == 2&&turned == 0)
					{
						tmp[i][j+1] = 3;
						turned = 1;
					}
					if(s[i+1][j-1] == 2&&turned == 0)
					{
						tmp[i+1][j-1] = 3;
						turned = 1;
					}
					if(s[i+1][j] == 2&&turned == 0)
					{
						tmp[i+1][j] = 3;
						turned = 1;
					}
					if(s[i+1][j+1] == 2&&turned == 0)
					{
						tmp[i+1][j+1] = 3;
						turned = 1;
					}
					break;
				}
			default:cout<<"error develop"<<endl;
		}
		for(i=0;i<col;i++)
			for(j=0;j<row;j++)
				if(tmp[i][j] != 5)
				{
					s[i][j] = tmp[i][j];
				}
        Traverse();
        Display();
}
int main()
{
	world me;
	me.Init();
	ShengTai your;
	your.Init();
	world *p = &me;
	p->Display();
	p = &your;
	p->Display();//通過基類指標呼叫派生類的虛擬函式

	ShengTai w;
	w.Init();
	int i;
	cout<<"input 1 to display,2 to develop,3 to exit."<<endl;
	cin>>i;
	while(i != 3)
	{
		if(i == 1)
			w.Display();
		if(i == 2)
			w.Develop();
		cout<<"input 1 to display,2 to develop,3 to exit."<<endl;
		cin>>i;
	}
	return 0;
}

執行結果:

1.     類的繼承與虛擬函式的實現


2.     生態系統中空地、青草、兔子、狐狸演化的過程

演化一次


演化二次


相關推薦

C++課程設計模擬一個微型生態系統

題目: 模擬一個微型生態系統。有青草、兔子、狐狸三種生物。可以用網格表示整個環境,每個格子內用不同字母表示不同事物:空地E青草G兔子R狐狸F。系統規則如下: 1)初始狀態隨機出現GRF; 2)若格子為G,下一輪其周邊八個格子中為E的格子中會至少有一個變為G; 3)若格子為R

c++課程設計貪食蛇

這個程式是windows環境下完成的,為了更好的做出動畫效果,定義了gotoxy函式,主要用了三個 類Game_Map,Snake,和Snake_Game類,在第一次遊戲時需要在選單中初始化玩家資料,用了vector來儲存蛇身。 //Windows環境下c++貪食蛇

C++/C課程設計(2)工資管理系統功能說明

工資管理系統 一,     基本功能要求: 1)以密碼登入系統 密碼登入時讀取檔案info.txt(儲存員工資訊)和Admin.txt(儲存管理員息) 如果密碼錯誤或者使用者賬號不存在,會給出相應提示 二,     主功能介面 選單佈

課程設計四位加法計算器(2)(C程式碼)

#include<reg52.h> typedef unsigned char uint8; typedef unsigned int uint16; sbit rw=P2^5; sbit rs=P2^6; sbit e=P2^7; sbit led=P3

C語言課程設計學生資訊管理系統

#include"stdio.h" //標準的輸入輸出函式檔案頭部說明 #include"math.h" // 數學函式頭部說明 #include"string.h" #include"stdlib.h" //通過該函式頭部裡的

代寫System Simulator作業、代寫OS/CS編程語言作業、C/C++課程設計作業代做、代做OS/CS作業

The fault dom cut cas shm deadline memory testing Semaphores and Operating System SimulatorOperating System SimulatorThis will be your ma

代寫COMP 3023作業、代做c++課程設計作業、代寫Software Development作業、代做C++程式設計作業

代寫COMP 3023作業、代做c++課程設計作業、代寫Software Development作業、代做C++程式設計作業School of Information Technology and Mathematical SciencesCOMP 3023 Software Development with

代寫COMP/2013作業、代做python/c++作業、代做UML DESIGN作業、代寫Python, C/C++課程設計作業

代寫COMP/2013作業、代做python/c++作業、代做UML DESIGN作業、代寫Python, C/C++課程設計作業COMP/2013 (Lab 03 - 2018)1LAB 3: UML DESIGN GROUP PROJECTAims: Practice your object orient

代做EEEN20010作業、代寫C/C++課程設計作業、代做Computer Engineering作業、代寫C/C++程式語言作業

代做EEEN20010作業、代寫C/C++課程設計作業、代做Computer Engineering作業、代寫C/C++程式語言作業EEEN20010 Computer Engineering I“Find root of a cubic function”(root)You are expected to

代寫HCI Project作業、代做C/C++課程設計作業、linux程式作業代寫代做、C/C++程式設計作業代做

  代寫HCI Project作業、代做C/C++課程設計作業、linux程式作業代寫代做、C/C++程式設計作業代做HCI Project DescriptionWe will be designing a notepad application for linux, as we found t

代寫CS4115留學生作業、代做C/C++程式設計作業、代寫matrix compression program作業、代做C/C++課程設計作業

CS4115 Week06 Lab ExerciseLab Objective: The objective of this week’s lab is to consider a matrix compression program.Matrices are used in a huge variety o

代做CSE 232作業、代寫C/C++程式設計作業、代做Steganography作業、代寫C/C++課程設計作業

CSE 232 Fall 2018Programming Project 06Assignment OverviewThis assignment is worth 50 points (5.0% of the course grade) and must be completed and turnedin

代寫ENGO 333作業、代做C++程式設計作業、代寫geometric留學生作業、代做C++課程設計作業

代寫ENGO 333作業、代做C++程式設計作業、代寫geometric留學生作業、代做C++課程設計作業Lab Assignment 8 ENGO 3331Lab Assignment #8 (Bonus)Where would we be after a couple of geometric trans

代寫CS320留學生作業、代做C/C++課程設計作業、代寫tokenizers and parsers作業、代做C/C++實驗作業

Assignment #2CS320 Assignment #2PurposeThis assignment is designed to familiarize you with C++ programming, tokenizersand parsers.RequirementsThis assignme

火星時代反思遊戲課程設計遊戲程式設計(1)

上兩週,我花了大量時間回顧了下學期的三門課程。課程與上學期一樣,儘管改了一些主題。經過艱難的開始,我現在對結果很滿意。在本文中,我將描述我對遊戲程式設計課程的校正情況,這是電腦科學專業的本科生和研究生的選修課。這門課的實際變動很小,但代表了我本人的研究和學習情況

代做C++程式設計作業、代寫geometric留學生作業、代做C++課程設計作業 代寫R語言程式|幫做C/C++程式設計

Introduction In this assignment, you are going to develop the Ludo Game, a board game that runs in the command line environment. Lu

【JAVA 課程設計 萬年曆】

距離2017年還有30多個小時~轉眼間2016只剩一個尾巴了,大學生活也過了快一半了,自己卻依舊那麼笨手笨腳,不會的知識永遠那麼多,該看的書永遠沒機會去看,2017願一切如昨天抽的籤: 遠方不一定有詩,但有更好的自己~明天你好,請多關照~ 2017希望我的家人

C++課程設計:學生管理系統

(一)新生基本資訊統計軟體 有新生來報到,要逐個錄入其資訊,如:學生姓名,性別,專業,出生日期,家庭地址,英語入學成績。要求設計連結串列類來實現,並統計學生人數。文字介面為: 1. 新增學生資訊 2. 刪除學生資訊 3. 匯入學生資訊(已經保存於的檔案資訊) 4. 學生資訊搜尋

大四課程設計基於RFID技術的考勤管理系統(一)專案介紹

---------------------------------------------------------------------------------------- 原始碼下載地址: RFID原始碼下載: --------------------

Java多執行緒模擬一個阻塞佇列

import java.util.LinkedList; import java.util.concurrent.atomic.AtomicInteger; public class MyQueue { private final LinkedLi