user-0 提问: C程序调试

回复:
user: user-1
content: 找个wintc  快得很啊

user: user-2
content: 用printf调试吧

user: user-3
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试是程序员必须掌握的技能之一。

user: user-0
content: 请大神们帮个忙

user: user-0
content: //领域自适应交叉 void cross() { 	double pc1=0.9,pc2=0.6,pc; 	double favg,fmax,temp,p; 	int i,j,t,start,end,point; 	srand((unsigned)time(NULL)); 	for(i=0;i<10;i++){                 //求适应值 		for(j=0;j<10;j++) 		{ 		  TSP[i][j].Distance=0; 		  for(t=1;t<=CITY_NUM;t++) 		 { 			start=TSP[i][j].colony[t-1];end=TSP[i][j].colony[t]; 			 TSP[i][j].Distance= TSP[i][j].Distance+CityDistance[start][end]; 		 } 		TSP[i][j].fitness=1/(TSP[i][j].Distance); 		printf("TSP[%d][%d].fitness=%f,i,j,TSP[i][j].fitness\n"); 		} 	}    Agent sBest=TSP[0][0];           //找出最大适应值 	for(i=0;i<10;i++) 	{ 		for(j=0;j<10;j++) 		{ 			if(sBest.fitness<TSP[i][j].fitness) 			{ 				sBest.fitness=TSP[i][j].fitness; 			} 		} 	}   fmax=sBest.fitness;    double sum=0;                      //找出平均适应值    for(i=0;i<10;i++) 	{ 		for(j=0;j<10;j++) 		{ 		  sum+=TSP[i][j].fitness; 		} 	}    favg=sum/POPSIZE;    for(i=0;i<10;i++)    { 	   for(j=0;j<10;j++) 	   { 	      if(TSP[i][j].fitness>=favg)   //自适应交叉概率 		       pc=pc1-(pc1-pc2)*(TSP[i][j].fitness-favg)/(fmax-favg); 	      else 		       pc=pc1; 	      p=rand()%1000/1000.0; 	      if(p<pc)   //单点交叉 	     { 		    point=rand()%9+1; 		    for (t=point;t<CITY_NUM;t++) 				{                 temp=TSP[i][j].colony[t];                 TSP[i][j].colony[t]=maxagent.colony[t];                 maxagent.colony[t]=temp 				} 			} 	   }    } }      //自适应变异 void cross() {     double pm1=0.1,pm2=0.001,pm; 	double favg,fmax,temp,p; 	int i,j,t,start,end,point1,point2; 	srand((unsigned)time(NULL)); 	for(i=0;i<10;i++){                 //求适应值 		for(j=0;j<10;j++) 		{ 		  TSP[i][j].Distance=0; 		  for(t=1;t<=CITY_NUM;t++) 		 { 			start=TSP[i][j].colony[t-1];end=TSP[i][j].colony[t]; 			 TSP[i][j].Distance= TSP[i][j].Distance+CityDistance[start][end]; 		 } 		TSP[i][j].fitness=1/(TSP[i][j].Distance); 		printf("TSP[%d][%d].fitness=%f,i,j,TSP[i][j].fitness\n"); 		} 	}    Agent sBest=TSP[0][0];           //找出最大适应值 	for(i=0;i<10;i++) 	{ 		for(j=0;j<10;j++) 		{ 			if(sBest.fitness<TSP[i][j].fitness) 			{ 				sBest.fitness=TSP[i][j].fitness; 			} 		} 	}   fmax=sBest.fitness;    double sum=0;                      //找出平均适应值    for(i=0;i<10;i++) 	{ 		for(j=0;j<10;j++) 		{ 		  sum+=TSP[i][j].fitness; 		} 	}    favg=sum/POPSIZE;     for(i=0;i<10;i++)    { 	   for(j=0;j<10;j++) 	   { 	      if(TSP[i][j].fitness>=favg)   //自适应变异概率 		       pm=pm1-(pm1-pm2)*(fmax-TSP[i][j].fitness)/(fmax-favg); 		  else 			  pm=pm1; 		   p=rand()%1000/1000.0; 		   if(p<pm)   //对换变异 	      { 	        point1=rand()%10+1; 	        srand((unsigned)time(NULL)); 	        point2=rand()%10+1; 			temp=TSP[i][j].colony[point1]; 			TSP[i][j].colony[point1]=TSP[i][j].colony[point2]; 			TSP[i][j].colony[point2]=temp; 		   } 	   } 	} }      //找出最优智能体 Agent findbest() { 	int i,j,start,end,t; 	for(i=0;i<10;i++){                 //求适应值 		for(j=0;j<10;j++) 		{ 		  TSP[i][j].Distance=0; 		  for(t=1;t<=CITY_NUM;t++) 		 { 			start=TSP[i][j].colony[t-1];end=TSP[i][j].colony[t]; 			 TSP[i][j].Distance= TSP[i][j].Distance+CityDistance[start][end]; 		 } 		TSP[i][j].fitness=1/(TSP[i][j].Distance); 		printf("TSP[%d][%d].fitness=%f,i,j,TSP[i][j].fitness\n"); 		} 	}    Agent sBest=TSP[0][0];           //找出最大适应值 	for(i=0;i<10;i++) 	{ 		for(j=0;j<10;j++) 		{ 			if(sBest.fitness<TSP[i][j].fitness) 			{ 				sBest.fitness=TSP[i][j].fitness; 			} 		} 	}   return sBest;  }     //自学习初始种群 void sinitpopulation() { 	int i,j,m,n,t,p; 	Agent spop[9];     srand((unsigned)time(NULL)); 	n=(rand()%5+8)/10; 	for(i=0;i<3;i++) 	{ 	 spop[i].colony[0]=0;      spop[i].colony[10]=0; 	}    Agent  findbest();    spop[5]=findbest();    	for(i=4;i>=0;i--) 	{ 		for(j=1;j<CITY_NUM;j++) 		{          	t=sBest.colony[j]*n; 			if(t<0) 			   spop[i].colony[j]=0; 		    else if(t>9) 			   spop[i].colony[j]=9; 			else 				spop[i].colony[j]=(int)t;            printf(" spop[%d].colony[%d]=%d \n",i,j,spop[i].colony[j]);  		} 	} 	for(m=6;m<9;m++) 	{ 		for(p=1;p<CITY_NUM;p++) 		{           	t=sBest.colony[p]*n; 			if(t<0) 			   spop[m].colony[p]=0; 		    else if(t>9) 			   spop[m].colony[p]=9; 			else 				spop[m].colony[p]=(int)t;            printf(" spop[%d].colony[%d]=%d \n",m,p,spop[m].colony[p]);  		} 	} }    //自学习网格初始化 void sinit() {   int j,k;   for(j=0;j<3;j++)   { 	for(k=0;k<3;k++) 	{	 		tsp[j][k]=spop[j*3+k];         printf("tsp[%d][%d]=%d\n",j,k,tsp[j][k]); 	}   } }   	  //找出自学习种群中最优智能体 Agent  sfindbest() {  	int i,j,start,end,t; 	for(i=0;i<3;i++){                 //求适应值 		for(j=0;j<3;j++) 		{ 		  tsp[i][j].Distance=0; 		  for(t=1;t<=CITY_NUM;t++) 		 { 			start=tsp[i][j].colony[t-1];end=tsp[i][j].colony[t]; 			 tsp[i][j].Distance=tsp[i][j].Distance+CityDistance[start][end]; 		 } 		tsp[i][j].fitness=1/(tsp[i][j].Distance); 		printf("tsp[%d][%d].fitness=%f,i,j,tsp[i][j].fitness\n"); 		} 	}    Agent sBest=tsp[0][0];           //找出最大适应值 	for(i=0;i<3;i++) 	{ 		for(j=0;j<3;j++) 		{ 			if(sBest.fitness<tsp[i][j].fitness) 			{ 				sBest.fitness=tsp[i][j].fitness; 			} 		} 	}   return sBest; }     //自学习           void slearn() {    srand((unsigned)time(NULL));    struct agent  sBest, CBest;    int  r=0,sGen=10;    double sPm=0.05;    sinitpopulation();    sinit();     sBest=sfindbest();    while(r<sGen) 	{ 	 		r++;         scomplete();         smutation();         scaluateenergy();          CBest.energy=sfindbest(); 	}  if(CBest.energy>sBest.energy) 	 sBest=CBest; }    //自学习邻域竞争 [b]void scomplete() { 	int i,j,point1,point2,t; 	double p,po=0.15; 	Agent maxagent;     srand((unsigned)time(NULL)); 	point1=rand()%8+2; 	srand((unsigned)time(NULL)); 	point2=rand()%8+2; 	if(point1>point2) 	{ 		t=point1; 		point1=point2; 		point2=t; 	} 	p=rand()%1000/1000.0;     for(i=0;i<3;i++) 	{ 		for(j=0;j<3;j++) 		{           maxagent=smaxnb(i,j); 			              if(tsp[i][j].fitness<maxagent.fitness) 			 { 				 if(p<po) 				 { 					t=maxagent.colony[point1]; 					maxagent.colony[point1]=maxagent.colony[point2]; 					maxagent.colony[point2]=t; 					tsp[i][j]=maxagent; 				 } 				 else 				 { 					 for(i=point1,j=point2;i<j;i++,j--) 					 { 						 t=maxagent.colony[i]; 					     maxagent.colony[i]=maxagent.colony[j]; 					     maxagent.colony[j]=t; 					 } 					 tsp[i][j]=maxagent; 				 }  	}      Agent smaxnb(int i,int j) {     Agent maxagent;     if(i==0&&j==0) 		maxagent=max(tsp[i][j+1],tsp[i+1][j],tsp[i+2][j],tsp[i][j+2]);      else if(i==0&&j==2) 	    maxagent=max(tsp[i][j-1],tsp[i+1][j],tsp[i][j-2],tsp[i+2][j]);  	else if(i==2&&j==0) 		maxagent=max(tsp[i-1][j],tsp[i][j+1],tsp[i-2][j],tsp[i][j+2]);     	else if(i==2&&j==2)     	maxagent=max(tsp[i][j-1],tsp[i-1][j],tsp[i][j-2],tsp[i-2][j]);      else  if(i==2&&j==1)         maxagent=max(tsp[i][j+1],tsp[i][j-1],tsp[i-1][j],tsp[i-2][j]);      else  if(i==0&&j==1)         maxagent=max(tsp[i][j-1],tsp[i][j+1],tsp[i+1][j],tsp[i+2][j]);      else  if(j==0&&i==1) 	    maxagent=max(tsp[i-1][j],tsp[i+1][j],tsp[i][j+1],tsp[i][j+2]);      else  if(j==2&&i==1) 	   maxagent=max(tsp[i-1][j],tsp[i+1][j],tsp[i][j-1],tsp[i][j-2]);      else 		maxagent=max(tsp[i-1][j],tsp[i+1][j],tsp[i][j-1],tsp[i][j+1]);  	return maxagent; }     //自学习变异  void smutation() {   int i,j,point1,point2,temp;   double p;   for(i=0;i<3;i++)    { 	   for(j=0;j<3;j++) 	   { 		   p=rand()%1000/1000.0; 		   if(p<sPm)   //对换变异 	      { 	        point1=rand()%10+1; 	        srand((unsigned)time(NULL)); 	        point2=rand()%10+1; 			temp=tsp[i][j].colony[point1]; 			tsp[i][j].colony[point1]=tsp[i][j].colony[point2]; 			tsp[i][j].colony[point2]=temp; 		   } 	   } 	} }    //输出 void OutPut(Agent city )    { 	int i,j;  	printf("The population is:\n"); 	for(i=0;i<POPSIZE;i++) 	{ 		for(j=0;j<=CITY_NUM;j++) 		{ 			printf("%5d",TSP[i].colony[j]); 		} 		printf("    %f\n",TSP[i].Distance); 	}  	 	printf("The best rooting is:\n"); 	for(i=0;i<=CITY_NUM;i++) 		printf("%5d",city.BestRooting[i]);  	printf("\nIt's cost value is %f\n",(city.BestValue));  	printf("\n\n\n\n");  }