Автор работы: Пользователь скрыл имя, 07 Января 2012 в 15:51, курсовая работа
Рассмотрим упрощённую экологическую нишу “Остров”. На маленьком острове растёт только трава (в научной терминологии - продуцент). Травой питаются мыши и кролики (первичные консументы). На мышей охотятся хорьки (специализированный хищник), лисы едят мышей и кроликов (всеяд-ный хищник). Таким образом, хорьки и лисы – консументы вторичного уров-ня. Беркут (консумент следующего уровня) может питаться кроликами, хорь-ками и лисами.
1. Введение.
2. Постановка задачи.
3. Текст программы.
4. Результаты работа программы.
5. Вывод.
#define ADDFOODGREEN 12
#define DECOTHERGREEN0 0.7
#define DECOTHERGREEN1 0.3
#define ABSORBFOODGREEN 20
#define ABSORBVALUEGREEN 2
#define MAXCHILDSGREEN 5
struct mbuf
{
long mtype;
char mtext[MSGMAX];
} msg;
struct sub
{
int food;
int value;
int childs;
};
int fd, counter;
int NewFood,NewValue;
int OutFood,OutValue;
sub Subjects[MAXSUBJECTS];
int main()
{
int i;
fd = msgget(MSGID,0);
counter=0;
for(i=0;i<5;i++)
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
}
while(counter>0)
{
OutFood=0;
OutValue=0;
msgrcv(fd, &msg, MSGMAX,9,0);
sscanf(msg.mtext,"%d %d",&NewFood,&NewValue);
if (NewFood==-1)
return -1;
for(i=0;i<counter;i++)
{
if (Subjects[i].food<=0)
{
int j;
counter--;
Subjects[i].food=Subjects[
Subjects[i].value=Subjects[
Subjects[i].childs=Subjects[
}
if ((Subjects[i].childs<
{
if((Subjects[i].food>
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
Subjects[i].food-=
Subjects[i].value-=
Subjects[i].childs++;
}
}
if (Subjects[i].food>
{
OutFood=0;
OutValue+=ADDFOODGREEN*
Subjects[i].food-=
}
if (NewFood>ABSORBFOODGREEN)
{
Subjects[i].food+=
NewFood-=ABSORBFOODGREEN;
}
else
{
Subjects[i].food+=NewFood;
NewFood=0;
}
if (NewValue>ABSORBVALUEGREEN)
{
Subjects[i].value+=
NewValue-=ABSORBVALUEGREEN;
}
else
{
Subjects[i].value+=NewValue;
NewValue=0;
}
Subjects[i].food--;
}
msg.mtype=4;
sprintf(msg.mtext,"%d
%d %d\n",OutFood,OutValue,
msgsnd(fd,&msg,strlen(msg.
}
}
Blue.c
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/msg.h>
#define MSGID 205
#define MSGMAX 255
#define MAXSUBJECTS 1000
#define BEGINFOODBLUE 15
#define BEGINVALUEBLUE 10
#define REPRODUSEFOODBLUE 20
#define REPRODUSEVALUEBLUE 15
#define ADDFOODBLUE 12
#define DECOTHERBLUE0 0.7
#define DECOTHERBLUE1 0.3
#define ABSORBFOODBLUE 20
#define ABSORBVALUEBLUE 2
#define MAXCHILDSBLUE 3
struct mbuf
{
long mtype;
char mtext[MSGMAX];
} msg;
struct sub
{
int food;//еда
int value;//ценный ресурс
int childs;//дети
};
int fd, counter;
int NewFood,NewValue;
int OutFood,OutValue;
sub Subjects[MAXSUBJECTS];
int main()
{
int i;
fd = msgget(MSGID,0);
counter=0;
for(i=0;i<5;i++)
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
}
while(counter>0)
{
OutFood=0;
OutValue=0;
msgrcv(fd, &msg, MSGMAX,10,0);
sscanf(msg.mtext,"%d %d",&NewFood,&NewValue);
if (NewFood==-1)
return -1;
for(i=0;i<counter;i++)
{
if (Subjects[i].food<=0)
{
int j;
counter--;
Subjects[i].food=Subjects[
Subjects[i].value=Subjects[
Subjects[i].childs=Subjects[
}
if ((Subjects[i].childs<
{
if((Subjects[i].food>
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
Subjects[i].food-=
Subjects[i].value-=
Subjects[i].childs++;
}
}
if (Subjects[i].food>ADDFOODBLUE)
{
OutFood+=ADDFOODBLUE*
OutValue=0;
Subjects[i].food-=ADDFOODBLUE*
}
if (NewFood>ABSORBFOODBLUE)
{
Subjects[i].food+=
NewFood-=ABSORBFOODBLUE;
}
else
{
Subjects[i].food+=NewFood;
NewFood=0;
}
if (NewValue>ABSORBVALUEBLUE)
{
Subjects[i].value+=
NewValue-=ABSORBVALUEBLUE;
}
else
{
Subjects[i].value+=NewValue;
NewValue=0;
}
Subjects[i].food--;
}
msg.mtype=5;
sprintf(msg.mtext,"%d
%d %d\n",OutFood,OutValue,
msgsnd(fd,&msg,strlen(msg.
}
}
Violet.c
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/msg.h>
#define MSGID 205
#define MSGMAX 255
#define MAXSUBJECTS 1000
#define BEGINFOODVIOLET 15
#define BEGINVALUEVIOLET 10
#define REPRODUSEFOODVIOLET 20
#define REPRODUSEVALUEVIOLET 15
#define ADDFOODVIOLET 12
#define DECOTHERVIOLET 0 0.7
#define DECOTHERVIOLET 1 0.3
#define ABSORBFOODVIOLET 20
#define ABSORBVALUEVIOLET 2
#define MAXCHILDSVIOLET 3
struct mbuf
{
long mtype;
char mtext[MSGMAX];
} msg;
struct sub
{
int food;//еда
int value;//ценный ресурс
int childs;//дети
};
int fd, counter;
int NewFood,NewValue;
int OutFood,OutValue;
sub Subjects[MAXSUBJECTS];
int main()
{
int i;
fd = msgget(MSGID,0);
counter=0;
for(i=0;i<5;i++)
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
}
while(counter>0)
{
OutFood=0;
OutValue=0;
msgrcv(fd, &msg, MSGMAX,10,0);
sscanf(msg.mtext,"%d %d",&NewFood,&NewValue);
if (NewFood==-1)
return -1;
for(i=0;i<counter;i++)
{
if (Subjects[i].food<=0)
{
int j;
counter--;
Subjects[i].food=Subjects[
Subjects[i].value=Subjects[
Subjects[i].childs=Subjects[
}
if ((Subjects[i].childs<
{
if((Subjects[i].food>
{
Subjects[counter].food=
Subjects[counter].value=
Subjects[counter].childs=0;
counter++;
Subjects[i].food-=
Subjects[i].value-=
Subjects[i].childs++;
}
}
if (Subjects[i].food>
{
OutFood+=ADDFOODVIOLET*
OutValue=0;
Subjects[i].food-=
}
if (NewFood>ABSORBFOODVIOLET)
{
Subjects[i].food+=
NewFood-=ABSORBFOODVIOLET;
}
else
{
Subjects[i].food+=NewFood;
NewFood=0;
}
if (NewValue>ABSORBVALUEVIOLET)
{
Subjects[i].value+=
NewValue-=ABSORBVALUEVIOLET;