70,020
社区成员




#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
typedef struct person
{
int age;
int gene[32];
int Tc;
struct person *link;
}per;
per *creat()
{
per *head,*p1,*p2;
int n=0,i,j,k;
srand((unsigned)time(NULL));
p1=p2=(per *)malloc(sizeof(per));
p1->age=rand()%32+1;//随机生成年龄0-32
for(j=0;j<p1->age;j++) p1->gene[j]=rand()%2;//随机生成基因
p1->Tc=0;
for(k=0;k<p1->age;k++)//统计致病基因数量
{
if (p1->gene[k]==1) p1->Tc++;
}
head=NULL;
for(i=0;i<1000;i++)
{
n=n+1;
if(n==1) head=p1;
else p2->link=p1;
p2=p1;
p1=(per *)malloc(sizeof(per));
p1->age=rand()%32+1;//随机生成年龄0-32
for(j=0;j<p1->age;j++) p1->gene[j]=rand()%2;//随机生成基因
p1->Tc=0;
for(k=0;k<p1->age;k++)//统计致病基因数量
{
if (p1->gene[k]==1) p1->Tc++;
}
}
p2->link=NULL;
return(head);
}
int main()
{
per *p,*q,*head,*p1,*p2;
FILE *fp;
int x1,x2,x3,m,t,l,x,r;
srand((unsigned)time(NULL));
p=creat();
head=p;
fp=fopen("penna.txt","w");
for(t=0;t<1000;t++)//1000个时间步
{
p=head;
while(p->link!=0)
{
if(p->age>=32)//老死
{
if(head==p) head=p->link;
else
{
q=p->link;
p->link=q->link;
free(q);
}
}
else if(p->Tc>=2)//病死
{
if(head==p) head=p->link;
else
{
q=p->link;
p->link=q->link;
free(q);
}
}
p=p->link;
}
p=head;
l=1;
while(p->link!=NULL)//统计存活个数
{
l++,p=p->link;
}
if(l>=100000) //判断是否超环境容量
{
printf("all dead");
break;
}
x=ceil((float)l/100000*1000);//计算死亡率
p=head;
srand((unsigned)time(NULL));
while(p->link!=NULL)
{
r=rand()%1000+1;
if(r<x)//满足条件则当前个体死亡
{
if(head==p) head=p->link;
else
{
q=p->link;
p->link=q->link;
free(q);
}
}
p=p->link;
}
p=head;
while(p->link!=NULL)
{
if(p->age>=8)
{
p1=(per *)malloc(sizeof(per));//新个体
p1->age=p->age;//复制过程
for(m=0;m<p->age;m++) p1->gene[m]=p->gene[m];
x1=rand()%p->age;//突变过程M=2
x2=rand()%p->age;
p1->gene[x1]=(!p->gene[x1]);
p1->gene[x2]=(!p->gene[x2]);
p2=head;
while(p2->link!=0) p2=p2->link;//找寻链表的末尾
p2->link=p1;//在链表末尾加上新个体
p1->link=NULL;
}
}
p=head,x3=1;
srand((unsigned)time(NULL));
while(p->link!=NULL)
{
p->gene[p->age]=rand()%2,p->age++;
x3++,p=p->link;
}
fprintf(fp,"%d\n",x3);
}
fclose(fp);
return 0;
}
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
typedef struct person
{
int age;
int gene[32];
int Tc;
struct person *link;
}per;
per *creat()
{
per *head, *p;
int n = 0, i, j, k;
//srand((unsigned)time(NULL)); //只需设置一次
head= (per*)malloc(sizeof(per)); //头节点
p = head;
for (i = 0; i < 1000; i++)
{
p->link = (per *)malloc(sizeof(per));
p = p->link;
p->age = rand() % 32 + 1;//随机生成年龄1-32
p->Tc = 0;
for (j = 0; j < p->age; j++)
{
//随机生成基因
if (1 == (p->gene[j] = rand() % 2)) p->Tc++; //统计致病基因数量
}
}
p->link = NULL;
return(head);
}
int main()
{
per *p, *q, *head, *p1, *p2;
FILE *fp;
int x1, x2, x3, m, t, l, x, r;
srand((unsigned)time(NULL));
//p = creat();
head = creat();
//head = p;
fp = fopen("penna.txt", "w");
for (t = 0; t < 1000; t++)//1000个时间步 //不明白为什么要有循环?
{
p = head;
q = p->link;
while (q)
{
if (q->age >= 32 || q->Tc >= 2) //老死||病死
{
p->link = q->link;
free(q);
q = p;
}
p = q;
q = q->link;
}
p = head->link;
l = 0;
while (p)//统计存活个数
{
l++, p = p->link;
}
if (l >= 100000) //判断是否超环境容量
{
printf("all dead");
break;
}
x = ceil((float)l / 100000 * 1000);//计算死亡率
p = head;
q = p->link;
//srand((unsigned)time(NULL));
while (q)
{
r = rand() % 1000 + 1;
if (r < x)//满足条件则当前个体死亡
{
p->link = q->link;
free(q);
q = p;
}
p = q;
q = q->link;
}
p = head;
q = p->link;
p1 = (per *)malloc(sizeof(per)); //头节点
p1->link = NULL;
p2 = p1;
while (q)
{
if (q->age >= 8)
{
p2->link = (per *)malloc(sizeof(per));//新个体
p2 = p2->link;
p2->link = NULL;
p2->age = q->age;//复制过程
for (m = 0; m < q->age; m++) p2->gene[m] = q->gene[m];
x1 = rand() % q->age;//突变过程M=2
x2 = rand() % q->age;
p2->gene[x1] = (!q->gene[x1]);
p2->gene[x2] = (!q->gene[x2]);
//p2 = head;
//while (p2->link != 0) p2 = p2->link;//找寻链表的末尾
//p2->link = p1;//在链表末尾加上新个体
//p1->link = NULL;
}
p = q;
q = q->link;
}
//把新个体添到链表的末尾
p->link = p1->link;
free(p1); //释放头节点
p = head->link, x3 = 1;
//srand((unsigned)time(NULL));
while (p)
{
p->gene[p->age] = rand() % 2, p->age++;
x3++, p = p->link;
}
fprintf(fp, "%d\n", x3);
}
fclose(fp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt||0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中