具体程序如下:
void CalculateObjectValue(void)
{
int i;
long temp1,temp2,temp3;
double x1,x2;
for(i=0;i<POPSIZE;i++)
{
temp1=DecodeChromosome(population[i].chrom,0,LENGTH1);
temp2=DecodeChromosome(population[i].chrom,0,LENGTH2);
x1=4.096*temp1/1023.0-2.048;
x2=4.096*temp2/1023.0-2.048;
temp3=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)(1-x1);
population[i].value=temp3;
}
}
显示的错误位置是: temp3=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)(1-x1);
请高手指教,分数不是问题,只要是正确答案,分数你自己说要多少就是多少(我自己所有的最高分哦)
long DecodeChromosome(char * string ,int point ,int length)
{
int i;
long decimal=0;
char *pointer;
for(i=0,pointer=string+point;i<length;i++,point++)
{
decimal+=(*pointer-'0')<<(length-1-i);
}
return(decimal);
}
这是DecodeChromosome函数,应该没有问题呀!不过还是谢谢你的回答!
具体程序如下:
void CalculateObjectValue(void)
{
nt i;
long temp1,temp2,temp3;
double x1,x2;
for(i=0;i<POPSIZE;i++)
{
temp1=DecodeChromosome(population[i].chrom,0,LENGTH1);
temp2=DecodeChromosome(population[i].chrom,0,LENGTH2);
x1=4.096*temp1/1023.0-2.048;
x2=4.096*temp2/1023.0-2.048;
temp3=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)(1-x1);
population[i].value=temp3.
控制资料:
花括号内为函数体。
如果没有返回值类型名为"void", 整数类型int 类型返回值为整数类型int,以此类推……
类型名有:void int long float int* long* float* ……
C++中函数的调用:函数必须声明后才可以被调用。调用格式为:函数名(实参)
调用时函数名后的小括号中的实参必须和声明函数时的函数括号中的形参个数相同。
有返回值的函数可以进行计算,也可以做为右值进行赋值。
参考资料来源:百度百科-函数