算法专栏

作者在 2010-08-08 00:09:31 发布以下内容
/********************************************************\
*牛顿下山法
*非线性方程求根
*目标:
*用牛顿下山法,求非线性方程x*x*x-x-1=0,的根。
*要求:
*输入,初值,误差限,最大迭代次数,最大下山次数;
*输出,近似根以及下山因子
*评价:
*使用牛顿下山算法 目标就只能求出一个根 并且在初值附近.
*因为算法只设计了一个出口.即X1-X0<E.  满足此条件 即结束  
\******************************************************
*/
#include"iostream"
#include"stdlib.h"
#include"math.h"
#include"conio.h"
//#include"conio.c"

using namespace std;

double function(double x)
{
    return x*x*x-x-1;
}

double function_dao(double x)
{
    return 3*x*x-1;
}

void error_output(int p)
{
    switch(p)
    {
    case 1:cout<<"超出下山次数,请另选择初值!"<<endl;
    case 2:cout<<"超出迭代次数,失败!";
    }  
}


int main()
{
    
    double x,e,l=1,x1;
    int m,n,k=1,j;
    cout<<"请输入初值: ";
    cin>>x;
    cout<<"输入精度: ";
    cin>>e;
    cout<<"输入最大迭代次数 : ";
    cin>>m;
    cout<<"输入最大下山次数:  ";
    cin>>n;
    
loop:
    j=1;
    if(function(x)==0){
        cout<<"该初值就是方程的根!";
        cout<<endl;
        return 1;
    }
    x1=x-l*function(x)/function_dao(x);
    for(l=1,j=1;j<=n&&k==1;j++)
    {
        x1=x-l*function(x)/function_dao(x);
        if(fabs(function(x1))>fabs(function(x))){
            l=l/2;
            cout<<"x0 "<<x<<"  x1 "<<x1<<endl;
        }
        else break;
    }
    for(l=1,j=1;j<=n&&k!=1;j++)
    {
        x1=x-l*function(x)/function_dao(x);
        if(fabs(function(x1))>fabs(function(x))){
            l=l/2;
            cout<<"x0 "<<x<<"  x1 "<<x1<<endl;
        }
        else break;
    }
    if(j>n)error_output(1);
    if(fabs(x1-x)<e)cout<<"求得方程的根:"<<x1<<endl;
    else{
        if(k==m)error_output(2);
        else {
            k=k+1;
            x=x1;
            goto loop;
        }
    }
    getch();
    return 1;  
}

 
基础知识 | 阅读 2167 次
文章评论,共14条
vfdff(作者)
2010-08-08 00:12
1
/*这是一个非常简单的遗传算法源代码,<br />
是由Denis Cormier (North Carolina State University)开发的,<br />
Sita S.Raghavan (University of North Carolina at Charlotte)修正。<br />
代码保证尽可能少,实际上也不必查错。对一特定的应用修正此代码,<br />
用户只需改变常数的定义并且定义“评价函数”即可。<br />
注意代码的设计是求最大值,其中的目标函数只能取正值;<br />
且函数值和个体的适应值之间没有区别。<br />
该系统使用比率选择、精华模型、单点杂交和均匀变异。<br />
如果用Gaussian变异替换均匀变异,可能得到更好的效果。<br />
代码没有任何图形,甚至也没有屏幕输出,<br />
主要是保证在平台之间的高可移植性。读者可以从ftp.uncc.edu,<br />
目录 coe/evol中的文件prog.c中获得。要求输入的文件应该命名为‘gadata.txt’;<br />
系统产生的输出文件为‘galog.txt’。输入的文件由几行组成:数目对应于变量数。<br />
且每一行提供次序——对应于变量的上下界。如第一行为第一个变量提供上下界,<br />
第二行为第二个变量提供上下界,等等。*/<br />
<br />
/**************************************************************************/<br />
/* This is a simple genetic algorithm implementation where the */<br />
/* evaluation function takes positive values only and the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* fitness of an individual is the same as the value of the&nbsp; &nbsp; */<br />
/* objective function&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/**************************************************************************/<br />
<br />
#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;math.h&gt;<br />
<br />
/* Change any of these parameters to match your needs */<br />
<br />
#define POPSIZE 50&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* population size */<br />
#define MAXGENS 1000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* max. number of generations */<br />
#define NVARS 3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* no. of problem variables */<br />
#define PXOVER 0.8&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* probability of crossover */<br />
#define PMUTATION 0.15&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* probability of mutation */<br />
#define TRUE 1<br />
#define FALSE 0<br />
<br />
int generation;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* current generation no. */<br />
int cur_best;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* best individual */<br />
FILE *galog;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* an output file */<br />
<br />
struct genotype /* genotype (GT), a member of the population */<br />
{<br />
&nbsp;&nbsp;double gene[NVARS];&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* a string of variables */<br />
&nbsp;&nbsp;double fitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* GT's fitness */<br />
&nbsp;&nbsp;double upper[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables upper bound */<br />
&nbsp;&nbsp;double lower[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables lower bound */<br />
&nbsp;&nbsp;double rfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* relative fitness */<br />
&nbsp;&nbsp;double cfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* cumulative fitness */<br />
};<br />
<br />
struct genotype population[POPSIZE+1];&nbsp; &nbsp; /* population */<br />
struct genotype newpopulation[POPSIZE+1]; /* new population; */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* replaces the */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* old generation */<br />
<br />
/* Declaration of procedures used by this genetic algorithm */<br />
<br />
void initialize(void);<br />
double randval(double, double);<br />
void evaluate(void);<br />
void keep_the_best(void);<br />
void elitist(void);<br />
void select(void);<br />
void crossover(void);<br />
void Xover(int,int);<br />
void swap(double *, double *);<br />
void mutate(void);<br />
void report(void);<br />
<br />
/***************************************************************/<br />
/* Initialization function: Initializes the values of genes&nbsp; &nbsp; */<br />
/* within the variables bounds. It also initializes (to zero)&nbsp;&nbsp;*/<br />
/* all fitness values for each member of the population. It&nbsp; &nbsp; */<br />
/* reads upper and lower bounds of each variable from the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* input file `gadata.txt'. It randomly generates values&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* between these bounds for each gene of each genotype in the&nbsp;&nbsp;*/<br />
/* population. The format of the input file `gadata.txt' is&nbsp; &nbsp; */<br />
/* var1_lower_bound var1_upper bound&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* var2_lower_bound var2_upper bound ...&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void initialize(void)<br />
{<br />
FILE *infile;<br />
int i, j;<br />
double lbound, ubound;<br />
<br />
if ((infile = fopen(&quot;gadata.txt&quot;,&quot;r&quot;))==NULL)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;fprintf(galog,&quot;\nCannot open input file!\n&quot;);<br />
&nbsp; &nbsp;&nbsp; &nbsp;exit(1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
/* initialize variables within the bounds */<br />
<br />
for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;fscanf(infile, &quot;%lf&quot;,&amp;lbound);<br />
&nbsp; &nbsp;&nbsp; &nbsp;fscanf(infile, &quot;%lf&quot;,&amp;ubound);<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;for (j = 0; j &lt; POPSIZE; j++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].fitness = 0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].rfitness = 0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].cfitness = 0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].lower<i> = lbound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].upper<i>= ubound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].gene<i> = randval(population[j].lower<i>,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[j].upper<i>);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
fclose(infile);<br />
}<br />
<br />
/***********************************************************/<br />
/* Random value generator: Generates a value within bounds */<br />
/***********************************************************/<br />
<br />
double randval(double low, double high)<br />
{<br />
double val;<br />
val = ((double)(rand()%1000)/1000.0)*(high - low) + low;<br />
return(val);<br />
}<br />
<br />
/*************************************************************/<br />
/* Evaluation function: This takes a user defined function.&nbsp;&nbsp;*/<br />
/* Each time this is changed, the code has to be recompiled. */<br />
/* The current function is:&nbsp;&nbsp;x[1]^2-x[1]*x[2]+x[3]&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/*************************************************************/<br />
<br />
void evaluate(void)<br />
{<br />
int mem;<br />
int i;<br />
double x[NVARS+1];<br />
<br />
for (mem = 0; mem &lt; POPSIZE; mem++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;x[i+1] = population[mem].gene<i>;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
}<br />
<br />
/***************************************************************/<br />
/* Keep_the_best function: This function keeps track of the&nbsp; &nbsp; */<br />
/* best member of the population. Note that the last entry in&nbsp;&nbsp;*/<br />
/* the array Population holds a copy of the best individual&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void keep_the_best()<br />
{<br />
int mem;<br />
int i;<br />
cur_best = 0; /* stores the index of the best individual */<br />
<br />
for (mem = 0; mem &lt; POPSIZE; mem++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;if (population[mem].fitness &gt; population[POPSIZE].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cur_best = mem;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[POPSIZE].fitness = population[mem].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
/* once the best member in the population is found, copy the genes */<br />
for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;population[POPSIZE].gene<i> = population[cur_best].gene<i>;<br />
}<br />
<br />
/****************************************************************/<br />
/* Elitist function: The best member of the previous generation */<br />
/* is stored as the last in the array. If the best member of&nbsp; &nbsp; */<br />
/* the current generation is worse then the best member of the&nbsp;&nbsp;*/<br />
/* previous generation, the latter one would replace the worst&nbsp;&nbsp;*/<br />
/* member of the current population&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/****************************************************************/<br />
<br />
void elitist()<br />
{<br />
int i;<br />
double best, worst;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* best and worst fitness values */<br />
int best_mem, worst_mem; /* indexes of the best and worst member */<br />
<br />
best = population[0].fitness;<br />
worst = population[0].fitness;<br />
for (i = 0; i &lt; POPSIZE - 1; ++i)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;if(population<i>.fitness &gt; population[i+1].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (population<i>.fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best = population<i>.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (population[i+1].fitness &lt;= worst)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;worst = population[i+1].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;worst_mem = i + 1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (population<i>.fitness &lt;= worst)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;worst = population<i>.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;worst_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (population[i+1].fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best = population[i+1].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best_mem = i + 1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
/* if best individual from the new population is better than */<br />
/* the best individual from the previous population, then&nbsp; &nbsp; */<br />
/* copy the best from the new population; else replace the&nbsp; &nbsp;*/<br />
/* worst individual from the current population with the&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/* best one from the previous generation&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
<br />
if (best &gt;= population[POPSIZE].fitness)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp; population[POPSIZE].gene<i> = population[best_mem].gene<i>;<br />
&nbsp; &nbsp; population[POPSIZE].fitness = population[best_mem].fitness;<br />
&nbsp; &nbsp; }<br />
else<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp; population[worst_mem].gene<i> = population[POPSIZE].gene<i>;<br />
&nbsp; &nbsp; population[worst_mem].fitness = population[POPSIZE].fitness;<br />
&nbsp; &nbsp; }<br />
}<br />
/**************************************************************/<br />
/* Selection function: Standard proportional selection for&nbsp; &nbsp; */<br />
/* maximization problems incorporating elitist model - makes&nbsp;&nbsp;*/<br />
/* sure that the best member survives&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void select(void)<br />
{<br />
int mem, i, j, k;<br />
double sum = 0;<br />
double p;<br />
<br />
/* find total fitness of the population */<br />
for (mem = 0; mem &lt; POPSIZE; mem++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;sum += population[mem].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
/* calculate relative fitness */<br />
for (mem = 0; mem &lt; POPSIZE; mem++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;population[mem].rfitness =&nbsp;&nbsp;population[mem].fitness/sum;<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
population[0].cfitness = population[0].rfitness;<br />
<br />
/* calculate cumulative fitness */<br />
for (mem = 1; mem &lt; POPSIZE; mem++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;population[mem].cfitness =&nbsp;&nbsp;population[mem-1].cfitness +<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;population[mem].rfitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
/* finally select survivors using cumulative fitness. */<br />
<br />
for (i = 0; i &lt; POPSIZE; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;p = rand()%1000/1000.0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;if (p &lt; population[0].cfitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;newpopulation<i> = population[0];<br />
&nbsp; &nbsp;&nbsp; &nbsp;else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for (j = 0; j &lt; POPSIZE;j++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (p &gt;= population[j].cfitness &amp;&amp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;p&lt;population[j+1].cfitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;newpopulation<i> = population[j+1];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
/* once a new population is created, copy it back */<br />
<br />
for (i = 0; i &lt; POPSIZE; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;population<i> = newpopulation<i>;<br />
}<br />
<br />
/***************************************************************/<br />
/* Crossover selection: selects two parents that take part in&nbsp;&nbsp;*/<br />
/* the crossover. Implements a single point crossover&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void crossover(void)<br />
{<br />
int i, mem, one;<br />
int first&nbsp;&nbsp;=&nbsp;&nbsp;0; /* count of the number of members chosen */<br />
double x;<br />
<br />
for (mem = 0; mem &lt; POPSIZE; ++mem)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;x = rand()%1000/1000.0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;if (x &lt; PXOVER)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;++first;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (first % 2 == 0)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Xover(one, mem);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;one = mem;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
}<br />
/**************************************************************/<br />
/* Crossover: performs crossover of the two selected parents. */<br />
/**************************************************************/<br />
<br />
void Xover(int one, int two)<br />
{<br />
int i;<br />
int point; /* crossover point */<br />
<br />
/* select crossover point */<br />
if(NVARS &gt; 1)<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp;if(NVARS == 2)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;point = 1;<br />
&nbsp; &nbsp;else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;point = (rand() % (NVARS - 1)) + 1;<br />
<br />
&nbsp; &nbsp;for (i = 0; i &lt; point; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;swap(&amp;population[one].gene<i>, &amp;population[two].gene<i>);<br />
<br />
&nbsp; &nbsp;}<br />
}<br />
<br />
/*************************************************************/<br />
/* Swap: A swap procedure that helps in swapping 2 variables */<br />
/*************************************************************/<br />
<br />
void swap(double *x, double *y)<br />
{<br />
double temp;<br />
<br />
temp = *x;<br />
*x = *y;<br />
*y = temp;<br />
<br />
}<br />
<br />
/**************************************************************/<br />
/* Mutation: Random uniform mutation. A variable selected for */<br />
/* mutation is replaced by a random value between lower and&nbsp; &nbsp;*/<br />
/* upper bounds of this variable&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/**************************************************************/<br />
<br />
void mutate(void)<br />
{<br />
int i, j;<br />
double lbound, hbound;<br />
double x;<br />
<br />
for (i = 0; i &lt; POPSIZE; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;for (j = 0; j &lt; NVARS; j++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;x = rand()%1000/1000.0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (x &lt; PMUTATION)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* find the bounds on the variable to be mutated */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lbound = population<i>.lower[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;hbound = population<i>.upper[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population<i>.gene[j] = randval(lbound, hbound);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
}<br />
<br />
/***************************************************************/<br />
/* Report function: Reports progress of the simulation. Data&nbsp; &nbsp;*/<br />
/* dumped into the&nbsp;&nbsp;output file are separated by commas&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void report(void)<br />
{<br />
int i;<br />
double best_val;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* best population fitness */<br />
double avg;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* avg population fitness */<br />
double stddev;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* std. deviation of population fitness */<br />
double sum_square;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* sum of square for std. calc */<br />
double square_sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* square of sum for std. calc */<br />
double sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* total population fitness */<br />
<br />
sum = 0.0;<br />
sum_square = 0.0;<br />
<br />
for (i = 0; i &lt; POPSIZE; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;sum += population<i>.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;sum_square += population<i>.fitness * population<i>.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
avg = sum/(double)POPSIZE;<br />
square_sum = avg * avg * POPSIZE;<br />
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));<br />
best_val = population[POPSIZE].fitness;<br />
<br />
fprintf(galog, &quot;\n%5d,&nbsp; &nbsp;&nbsp; &nbsp;%6.3f, %6.3f, %6.3f \n\n&quot;, generation,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;best_val, avg, stddev);<br />
}<br />
<br />
/**************************************************************/<br />
/* Main function: Each generation involves selecting the best */<br />
/* members, performing crossover &amp; mutation and then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* evaluating the resulting population, until the terminating */<br />
/* condition is satisfied&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void main(void)<br />
{<br />
int i;<br />
<br />
if ((galog = fopen(&quot;galog.txt&quot;,&quot;w&quot;))==NULL)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;exit(1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
generation = 0;<br />
<br />
fprintf(galog, &quot;\n generation&nbsp;&nbsp;best&nbsp;&nbsp;average&nbsp;&nbsp;standard \n&quot;);<br />
fprintf(galog, &quot; number&nbsp; &nbsp;&nbsp; &nbsp;value fitness&nbsp;&nbsp;deviation \n&quot;);<br />
<br />
initialize();<br />
evaluate();<br />
keep_the_best();<br />
while(generation&lt;MAXGENS)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;generation++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;select();<br />
&nbsp; &nbsp;&nbsp; &nbsp;crossover();<br />
&nbsp; &nbsp;&nbsp; &nbsp;mutate();<br />
&nbsp; &nbsp;&nbsp; &nbsp;report();<br />
&nbsp; &nbsp;&nbsp; &nbsp;evaluate();<br />
&nbsp; &nbsp;&nbsp; &nbsp;elitist();<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
fprintf(galog,&quot;\n\n Simulation completed\n&quot;);<br />
fprintf(galog,&quot;\n Best member: \n&quot;);<br />
<br />
for (i = 0; i &lt; NVARS; i++)<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp;fprintf (galog,&quot;\n var(%d) = %3.3f&quot;,i,population[POPSIZE].gene<i>);<br />
&nbsp; &nbsp;}<br />
fprintf(galog,&quot;\n\n Best fitness = %3.3f&quot;,population[POPSIZE].fitness);<br />
fclose(galog);<br />
printf(&quot;Success\n&quot;);<br />
}<br />
/***************************************************************/
vfdff(作者)
2010-08-08 00:13
2
Floyd算法2.cpp<br />
<br />
/*&nbsp;&nbsp;遗传算法示例源程序&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* <a href="http://bingyusky.blogchina.com/2373885.html" target="_blank">http://bingyusky.blogchina.com/2373885.html</a>&nbsp; &nbsp; */<br />
<br />
<br />
/*转自:<a href="http://www.lasg.ac.cn/cgi-bin/forum/topic.cgi?forum=4&amp;topic=1535" target="_blank">http://www.lasg.ac.cn/cgi-bin/forum/topic.cgi?forum=4&amp;topic=1535</a>*/<br />
<br />
/***************************************************************/<br />
/* This is a simple genetic algorithm implementation where the */<br />
/* evaluation function takes positive values only and the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* fitness of an individual is the same as the value of the&nbsp; &nbsp; */<br />
/* objective function&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/***************************************************************/<br />
<br />
#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;math.h&gt;<br />
<br />
/* Change any of these parameters to match your needs */<br />
<br />
#define POPSIZE 50&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* population size */<br />
#define MAXGENS 1000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* max. number of generations */<br />
#define NVARS 3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* no. of problem variables */<br />
#define PXOVER 0.8&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* probability of crossover */<br />
#define PMUTATION 0.15&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* probability of mutation */<br />
#define TRUE 1<br />
#define FALSE 0<br />
<br />
int generation;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* current generation no. */<br />
int cur_best;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* best individual */<br />
FILE *galog;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* an output file */<br />
<br />
struct genotype /* genotype (GT), a member of the population */<br />
{<br />
double gene[NVARS];&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* a string of variables */<br />
double fitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* GT's fitness */<br />
double upper[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables upper bound */<br />
double lower[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables lower bound */<br />
double rfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* relative fitness */<br />
double cfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* cumulative fitness */<br />
};<br />
<br />
struct genotype population[POPSIZE+1];&nbsp; &nbsp; /* population */<br />
struct genotype newpopulation[POPSIZE+1]; /* new population; */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* replaces the */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* old generation */<br />
<br />
/* Declaration of procedures used by this genetic algorithm */<br />
<br />
void initialize(void);<br />
double randval(double, double);<br />
void evaluate(void);<br />
void keep_the_best(void);<br />
void elitist(void);<br />
void select(void);<br />
void crossover(void);<br />
void Xover(int,int);<br />
void swap(double *,double *);<br />
void mutate(void);<br />
void report(void);<br />
<br />
/***************************************************************/<br />
/* Initialization function: Initializes the values of genes&nbsp; &nbsp; */<br />
/* within the variables bounds. It also initializes (to zero)&nbsp;&nbsp;*/<br />
/* all fitness values for each member of the population. It&nbsp; &nbsp; */<br />
/* reads upper and lower bounds of each variable from the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* input file `gadata.txt'. It randomly generates values&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* between these bounds for each gene of each genotype in the&nbsp;&nbsp;*/<br />
/* population. The format of the input file `gadata.txt' is&nbsp; &nbsp; */<br />
/* var1_lower_bound var1_upper bound&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* var2_lower_bound var2_upper bound ...&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void initialize(void)<br />
{<br />
FILE *infile;<br />
int i, j;<br />
double lbound, ubound;<br />
<br />
if ((infile = fopen(&quot;gadata.txt&quot;,&quot;r&quot;))==NULL)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; fprintf(galog,&quot;nCannot open input file!n&quot;);<br />
&nbsp; &nbsp; exit(1);<br />
&nbsp; &nbsp; }<br />
<br />
/* initialize variables within the bounds */<br />
<br />
for (i = 0; i [color=#555555] = lbound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].upper= ubound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].gene = randval(population[j].lower,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].upper);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp; }<br />
<br />
fclose(infile);<br />
}<br />
<br />
/***********************************************************/<br />
/* Random value generator: Generates a value within bounds */<br />
/***********************************************************/<br />
<br />
double randval(double low, double high)<br />
{<br />
double val;<br />
val = ((double)(rand()%1000)/1000.0)*(high - low) + low;<br />
return(val);<br />
}<br />
<br />
/*************************************************************/<br />
/* Evaluation function: This takes a user defined function.&nbsp;&nbsp;*/<br />
/* Each time this is changed, the code has to be recompiled. */<br />
/* The current function is:&nbsp;&nbsp;x[1]^2-x[1]*x[2]+x[3]&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/*************************************************************/<br />
<br />
void evaluate(void)<br />
{<br />
int mem;<br />
int i;<br />
double x[NVARS+1];<br />
<br />
for (mem = 0; mem [color=#555555];<br />
<br />
&nbsp; &nbsp; population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];<br />
&nbsp; &nbsp; }<br />
}<br />
<br />
/***************************************************************/<br />
/* Keep_the_best function: This function keeps track of the&nbsp; &nbsp; */<br />
/* best member of the population. Note that the last entry in&nbsp;&nbsp;*/<br />
/* the array Population holds a copy of the best individual&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void keep_the_best()<br />
{<br />
int mem;<br />
int i;<br />
cur_best = 0; /* stores the index of the best individual */<br />
<br />
for (mem = 0; mem&nbsp;&nbsp;population[POPSIZE].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; cur_best = mem;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; population[POPSIZE].fitness = population[mem].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* once the best member in the population is found, copy the genes */<br />
for (i = 0; i [color=#555555] = population[cur_best].gene;<br />
}<br />
<br />
/****************************************************************/<br />
/* Elitist function: The best member of the previous generation */<br />
/* is stored as the last in the array. If the best member of&nbsp; &nbsp; */<br />
/* the current generation is worse then the best member of the&nbsp;&nbsp;*/<br />
/* previous generation, the latter one would replace the worst&nbsp;&nbsp;*/<br />
/* member of the current population&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/****************************************************************/<br />
<br />
void elitist()<br />
{<br />
int i;<br />
double best, worst;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* best and worst fitness values */<br />
int best_mem, worst_mem; /* indexes of the best and worst member */<br />
<br />
best = population[0].fitness;<br />
worst = population[0].fitness;<br />
for (i = 0; i [color=#555555].fitness &gt; population[i+1].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population.fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best = population.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population[i+1].fitness [color=#555555].fitness [color=#555555].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; worst_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population[i+1].fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best = population[i+1].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best_mem = i + 1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* if best individual from the new population is better than */<br />
/* the best individual from the previous population, then&nbsp; &nbsp; */<br />
/* copy the best from the new population; else replace the&nbsp; &nbsp;*/<br />
/* worst individual from the current population with the&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/* best one from the previous generation&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
<br />
if (best &gt;= population[POPSIZE].fitness)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;for (i = 0; i [color=#555555] = population[best_mem].gene;<br />
&nbsp;&nbsp;population[POPSIZE].fitness = population[best_mem].fitness;<br />
&nbsp;&nbsp;}<br />
else<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;for (i = 0; i [color=#555555] = population[POPSIZE].gene;<br />
&nbsp;&nbsp;population[worst_mem].fitness = population[POPSIZE].fitness;<br />
&nbsp;&nbsp;}<br />
}<br />
/**************************************************************/<br />
/* Selection function: Standard proportional selection for&nbsp; &nbsp; */<br />
/* maximization problems incorporating elitist model - makes&nbsp;&nbsp;*/<br />
/* sure that the best member survives&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void select(void)<br />
{<br />
int mem, i, j, k;<br />
double sum = 0;<br />
double p;<br />
<br />
/* find total fitness of the population */<br />
for (mem = 0; mem [color=#555555] = population[0];<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for (j = 0; j = population[j].cfitness &amp;&amp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; p[color=#555555] = population[j+1];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* once a new population is created, copy it back */<br />
<br />
for (i = 0; i [color=#555555] = newpopulation;<br />
}<br />
<br />
/***************************************************************/<br />
/* Crossover selection: selects two parents that take part in&nbsp;&nbsp;*/<br />
/* the crossover. Implements a single point crossover&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void crossover(void)<br />
{<br />
int i, mem, one;<br />
int first&nbsp;&nbsp;=&nbsp;&nbsp;0; /* count of the number of members chosen */<br />
double x;<br />
<br />
for (mem = 0; mem&nbsp;&nbsp;1)<br />
 {<br />
 if(NVARS == 2)<br />
&nbsp; &nbsp;&nbsp; &nbsp; point = 1;<br />
 else<br />
&nbsp; &nbsp;&nbsp; &nbsp; point = (rand() % (NVARS - 1)) + 1;<br />
<br />
 for (i = 0; i [color=#555555], &amp;population[two].gene);<br />
<br />
 }<br />
}<br />
<br />
/*************************************************************/<br />
/* Swap: A swap procedure that helps in swapping 2 variables */<br />
/*************************************************************/<br />
<br />
void swap(double *x, double *y)<br />
{<br />
double temp;<br />
<br />
temp = *x;<br />
*x = *y;<br />
*y = temp;<br />
<br />
}<br />
<br />
/**************************************************************/<br />
/* Mutation: Random uniform mutation. A variable selected for */<br />
/* mutation is replaced by a random value between lower and&nbsp; &nbsp;*/<br />
/* upper bounds of this variable&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/**************************************************************/<br />
<br />
void mutate(void)<br />
{<br />
int i, j;<br />
double lbound, hbound;<br />
double x;<br />
<br />
for (i = 0; i [color=#555555].lower[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; hbound = population.upper[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; population.gene[j] = randval(lbound, hbound);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
}<br />
<br />
/***************************************************************/<br />
/* Report function: Reports progress of the simulation. Data&nbsp; &nbsp;*/<br />
/* dumped into the&nbsp;&nbsp;output file are separated by commas&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void report(void)<br />
{<br />
int i;<br />
double best_val;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* best population fitness */<br />
double avg;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* avg population fitness */<br />
double stddev;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* std. deviation of population fitness */<br />
double sum_square;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* sum of square for std. calc */<br />
double square_sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* square of sum for std. calc */<br />
double sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* total population fitness */<br />
<br />
sum = 0.0;<br />
sum_square = 0.0;<br />
<br />
for (i = 0; i [color=#555555].fitness;<br />
&nbsp; &nbsp; sum_square += population.fitness * population.fitness;<br />
&nbsp; &nbsp; }<br />
<br />
avg = sum/(double)POPSIZE;<br />
square_sum = avg * avg * POPSIZE;<br />
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));<br />
best_val = population[POPSIZE].fitness;<br />
<br />
fprintf(galog, &quot;n%5d,&nbsp; &nbsp;&nbsp; &nbsp;%6.3f, %6.3f, %6.3f nn&quot;, generation,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best_val, avg, stddev);<br />
}<br />
<br />
/**************************************************************/<br />
/* Main function: Each generation involves selecting the best */<br />
/* members, performing crossover &amp; mutation and then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* evaluating the resulting population, until the terminating */<br />
/* condition is satisfied&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void main(void)<br />
{<br />
int i;<br />
<br />
if ((galog = fopen(&quot;galog.txt&quot;,&quot;w&quot;))==NULL)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; exit(1);<br />
&nbsp; &nbsp; }<br />
generation = 0;<br />
<br />
fprintf(galog, &quot;n generation&nbsp;&nbsp;best&nbsp;&nbsp;average&nbsp;&nbsp;standard n&quot;);<br />
fprintf(galog, &quot; number&nbsp; &nbsp;&nbsp; &nbsp;value fitness&nbsp;&nbsp;deviation n&quot;);<br />
<br />
initialize();<br />
evaluate();<br />
keep_the_best();<br />
while(generation[color=#555555]);<br />
 }<br />
fprintf(galog,&quot;nn Best fitness = %3.3f&quot;,population[POPSIZE].fitness);<br />
fclose(galog);<br />
printf(&quot;Successn&quot;);<br />
}<br />
/***************************************************************/
vfdff(作者)
2010-08-08 00:14
3
3元函数求解.cpp<br />
<br />
/*<br />
[问题描述]<br />
<br />
&nbsp; &nbsp; 有形如:ax3+bx2+cx+d=0这样的一个一元三次方程。给出该方程中各项的系数(a,<br />
<br />
b,c,d&nbsp;&nbsp;均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),<br />
<br />
且根与根之差的绝对值≥1。<br />
<br />
&nbsp; &nbsp; 要求由小到大依次在同一行输出这三个实根(根与根之间留有空格),并精确到小数点<br />
<br />
后2位。<br />
<br />
<br />
<br />
提示;记方程f(x)=0,若存在2个数x1和x2,且x1 &lt; x2,f(x1)*f(x2) &lt; 0,<br />
<br />
&nbsp; &nbsp; 则在(x1,x2)之间一定有一个根。<br />
<br />
[样例]<br />
<br />
&nbsp; &nbsp; 输入:&nbsp;&nbsp;1&nbsp;&nbsp;-5&nbsp;&nbsp;-4&nbsp;&nbsp;20<br />
<br />
&nbsp; &nbsp; 输出:-2.00&nbsp;&nbsp;2.00&nbsp;&nbsp;5.00<br />
<br />
问题分析<br />
<br />
根据题意,三次方程 ax^3+bx^2+cx+d=0在区间[-100,100]有3个不等实根,不妨设这3个实根分别为x1,x2,x3,且x1&lt;x2&lt;x3,则方程可表示为a(x-x1)(x-x2)(x-x3)=0;于是,求解这3个实根,可转化为求函数f(x)= a(x-x1)(x-x2)(x-x3)图像与x轴交点的横坐标。如图:<br />
<br />
<br />
<br />
<br />
二分法:<br />
<br />
<br />
<br />
1.&nbsp;&nbsp;确定根的区间<br />
<br />
考虑在什么样的区间内会有根,由于题目给出了所有的根都在-100到100之间,且根与根之间的差不小于1的限制条件,可知,在[-100,100],[-99,-98],…[99,100],[100,100]这201个区间内,每个区间内至多只能存在一个根,这样除去区间[100,100]外,其他区间[a,a+1],要么f(a)=0,要么f(a)×f(a+1)&lt;0时这个方程在此区间内才有解。<br />
<br />
2.&nbsp;&nbsp;求方程的根<br />
<br />
确定了方程f(x)=0在区间(a,b)内如果有且只有一个根,那么我们可以有逐步缩小根可能存在的范围的方法确定出某精度下根的数值。本题规定根的精度是0.01,下面采用二分法求区间(a,b)内方程的根,过程如下:<br />
<br />
(1)&nbsp; &nbsp;&nbsp; &nbsp; 取当前可能存在解的区间(a,b);<br />
<br />
(2)&nbsp; &nbsp;&nbsp; &nbsp; 若a+0.001&gt;b或 f((a+b)/2)=0,则可确定根为(a+b)/2并退出过程;<br />
<br />
(3)&nbsp; &nbsp;&nbsp; &nbsp; 若f(a)×f((a+b)/2)&lt;0,则根在区间(a, (a+b)/2)中,故对区间(a, (a+b)/2)重复该过程;<br />
<br />
(4)&nbsp; &nbsp;&nbsp; &nbsp; 若f(a)× f((a+b)/2)&gt;0,则必然有 f((a+b)/2)×f(b)&lt;0,也就是说根在((a+b)/2,b)中,应该对此区间重复该过程。<br />
<br />
二分法求根的算法如下:<br />
<br />
*/<br />
#include&lt;iostream&gt;<br />
<br />
#include&lt;cmath&gt;<br />
<br />
using namespace std;<br />
<br />
double x[3];<br />
<br />
double a,b,c,d,u,v;<br />
<br />
int i,t;<br />
<br />
double f(double x)<br />
<br />
 {<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;double temp;<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;temp=((a*x+b)*x+c)*x+d;<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;return temp;<br />
<br />
 }<br />
<br />
int main()<br />
<br />
 {<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while(cin&gt;&gt;a&gt;&gt;b&gt;&gt;c&gt;&gt;d)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; t=-1;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for(i=-100;i&lt;=100;i++)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;u=double(i);<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;v=u+0.99999;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(fabs(f(u))&lt;0.00001||f(u)*f(v)&lt;=0)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;t++;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(fabs(f(u))&lt;0.00001)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;x[t]=u;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;else<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while((u+0.001&lt;v)&amp;&amp;fabs(f((u+v)/2))&gt;=0.00001)<br />
<br />
<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(f(u)*f((u+v)/2)&lt;0)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;v=(u+v)/2;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; else<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;u=(u+v)/2;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;x[t]=(u+v)/2;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;cout.setf(ios::fixed);<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;cout.precision(2);<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;x[0]&lt;&lt;&quot; &quot;&lt;&lt;x[1]&lt;&lt;&quot; &quot;&lt;&lt;x[2]&lt;&lt;endl;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;return 0;<br />
<br />
 }
vfdff(作者)
2010-08-08 00:17
4
数值积分<br />
<br />
// 2006.6.13&nbsp;&nbsp;C&amp;C++ 实效编程百例 人民邮电出版社 page 167<br />
/*<br />
&nbsp;&nbsp;积分函数 ∫4/(1+x*x) dx&nbsp; &nbsp;&nbsp;&nbsp;0&lt;=x&lt;=1<br />
&nbsp;&nbsp;<br />
*/<br />
<br />
<br />
#include&nbsp; &nbsp; &lt;iostream.h&gt;<br />
#include&nbsp; &nbsp; &lt;iomanip.h&gt;<br />
#include&nbsp; &nbsp; &lt;math.h&gt;<br />
#define&nbsp;&nbsp;PI&nbsp;&nbsp;3.141592653589793238462643<br />
class CIntegral<br />
{<br />
&nbsp; &nbsp; public:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double a,b;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 成员变量,分别表示积分的上下界<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double epsilon,MaxD;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 成员变量,分别表示积分的误差限和倒数限<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;CIntegral(double ,double );&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 构造函数, 没有返回类型<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double f(double);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 成员函数,描述积分函数的运算函数<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double Simpson(void);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 成员函数,表示Simpson公式的运算过程,返回运算结果<br />
};<br />
<br />
CIntegral::CIntegral(double xa,double xb)&nbsp; &nbsp;// 构造函数 ,<br />
{<br />
&nbsp; &nbsp; epsilon=0.5E-8;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 事先规定的误差限<br />
&nbsp; &nbsp; MaxD=24.0;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 表示这个区间导数的最大值<br />
&nbsp; &nbsp; a=xa;<br />
&nbsp; &nbsp; b=xb;<br />
}<br />
<br />
double CIntegral::f(double x)&nbsp; &nbsp;// 成员函数,描述积分函数的运算函数<br />
{<br />
&nbsp; &nbsp; return 4.0/(1.0+x*x);<br />
}<br />
<br />
double CIntegral::Simpson(void) // 成员函数,表示Simpson公式的运算过程,返回运算结果<br />
{<br />
&nbsp; &nbsp; double h,s,x;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// y(0)初值为 1 ,h 第一段步长为 0.01<br />
&nbsp; &nbsp; int i,n;<br />
&nbsp; &nbsp; // 利用误差公式计算 h<br />
&nbsp; &nbsp; h=2*exp(log(epsilon*180/MaxD/(b-a))*0.25);<br />
&nbsp; &nbsp; n=(int)((b-a)/h+0.5);&nbsp; &nbsp;&nbsp; &nbsp; // n 表示分段数目<br />
&nbsp; &nbsp; h=(b-a)/double(n);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // h 表示积分步长<br />
&nbsp; &nbsp; // 计算 s<br />
&nbsp; &nbsp; s=0.0;<br />
&nbsp; &nbsp; for(x=a,i=0;i&lt;n;i++,x+=h)<br />
&nbsp; &nbsp; {&nbsp; &nbsp;// 复化辛普生求积分公式<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;s+=h/6*(f(x)+4*f(x+h/2)+f(x+h));<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; // 返回 s ,为最终的积分数值<br />
&nbsp; &nbsp; return s;<br />
}<br />
<br />
void main()<br />
{<br />
&nbsp; &nbsp; CIntegral test(0.0,1.0);&nbsp; &nbsp;// 生成 CIntegral 类的对象 test,并给出上下限<br />
&nbsp; &nbsp; double res=test.Simpson(); // 计算积分<br />
&nbsp; &nbsp; setprecision(16);<br />
&nbsp; &nbsp; cout&lt;&lt;res&lt;&lt;endl;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// 输出结果<br />
&nbsp; &nbsp; cout&lt;&lt;PI-res&lt;&lt;endl;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// 输出误差<br />
}
vfdff(作者)
2010-08-08 00:20
5
循环报数 <br />
<br />
<br />
#include &lt;stdio.h&gt;<br />
#define&nbsp;&nbsp;MAX&nbsp;&nbsp;54<br />
#define&nbsp;&nbsp;M&nbsp; &nbsp; 7<br />
int count =5;<br />
main()<br />
{<br />
&nbsp; &nbsp;int n=MAX, a[MAX],b[MAX],i,qn=0;<br />
&nbsp; &nbsp;for(i=0;i&lt;MAX;i++)&nbsp; &nbsp;a<i>=i+1;<br />
&nbsp; &nbsp;i=0;<br />
&nbsp; &nbsp;while(qn&lt;MAX)&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp; for(;i&lt;n;i++)&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;count++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(count == M)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{qn++; b[MAX-n]=a<i>;n--;a<i>=a[n];count=0;break; }<br />
&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp; if(n==i) i=0 ;<br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;for(i=0;i&lt;MAX;i++)&nbsp; &nbsp;printf(&quot;%d\t&quot;,b<i>);<br />
}
vfdff(作者)
2010-08-08 00:20
6
// 2006.6.13&nbsp;&nbsp;C&amp;C++ 实效编程百例 人民邮电出版社 page 168<br />
/*<br />
<br />
&nbsp;&nbsp;微分函数 y'=-20y+20sin(t)+cos(t)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;y(0)=1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;0&lt;=t&lt;=1<br />
&nbsp;&nbsp;原函数&nbsp; &nbsp;y(t)=exp(-20t)+sin(t)<br />
&nbsp;&nbsp;龙格-库塔算法:<br />
&nbsp;&nbsp;y(n+1)=y(n)+h/6(K1+2*K2+2*k3+K4)<br />
&nbsp;&nbsp;K1=f(x(n),y(n))<br />
&nbsp;&nbsp;k2=f(x(n)+h/2,y(n)+h/2*k1)<br />
&nbsp;&nbsp;k3=f(x(n)+h/2,y(n)+h/2*k2)<br />
&nbsp;&nbsp;k4=f(x(n)+h,y(n)+h*k3)<br />
<br />
*/<br />
<br />
#include&nbsp; &nbsp; &lt;stdio.h&gt;<br />
#include&nbsp; &nbsp; &lt;math.h&gt;<br />
<br />
class CDiff<br />
{<br />
&nbsp; &nbsp; public:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double h,y;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// 成员变量,分别表示运算步长和计算结果<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double k1,k2,k3,k4;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 成员变量,表示公式运行的中间结果<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double f(double,double);&nbsp; &nbsp; // 成员函数,描述微分方程的表达式<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double Runge(void);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 成员函数,进行龙格-库塔公式的计算,并输出最终结果<br />
};<br />
<br />
double CDiff::f(double x,double y)&nbsp;&nbsp;// 成员函数,描述微分方程的表达式<br />
{<br />
&nbsp; &nbsp; return -20*y+20*sin(x)+cos(x);<br />
}<br />
<br />
double CDiff::Runge(void)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 成员函数,进行龙格-库塔公式的计算,并输出最终结果<br />
{<br />
&nbsp; &nbsp; double y=1,h=0.01;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // y(0)初值为 1 ,h 第一段步长为 0.01<br />
&nbsp; &nbsp; double k1,k2,k3,k4;<br />
&nbsp; &nbsp; double t;<br />
&nbsp; &nbsp; for(t=0;t&lt;0.2;t+=h)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=f(t,y);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k2=f(t+h/2,y+h*k1/2);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k3=f(t+h/2,y+h*k2/2);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k4=f(t+h,y+h*k3);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;y+=h/6*(k1+2*k2+2*k3+k4);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; h=0.075;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;// h 第二段步长为 0.075<br />
&nbsp; &nbsp; for(t=0.2;t&lt;1;t+=h)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=f(t,y);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k2=f(t+h/2,y+h*k1/2);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k3=f(t+h/2,y+h*k2/2);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k4=f(t+h,y+h*k3);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;y+=h/6*(k1+2*k2+2*k3+k4);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; h=1-t;<br />
&nbsp; &nbsp; k1=f(t,y);<br />
&nbsp; &nbsp; k2=f(t+h/2,y+h*k1/2);<br />
&nbsp; &nbsp; k3=f(t+h/2,y+h*k2/2);<br />
&nbsp; &nbsp; k4=f(t+h,y+h*k3);<br />
&nbsp; &nbsp; y+=h/6*(k1+2*k2+2*k3+k4);<br />
&nbsp; &nbsp; // 返回y ,为最终积分数值<br />
&nbsp; &nbsp; return y ;<br />
}<br />
<br />
void main()<br />
{<br />
&nbsp; &nbsp; CDiff test;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; // 生成 CDiff 类的对象 test<br />
&nbsp; &nbsp; double res=test.Runge();&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 计算积分<br />
&nbsp; &nbsp; printf(&quot;y(1)=%.16f\n&quot;,res);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;// 输出结果<br />
&nbsp; &nbsp; printf(&quot;e=%.16f\n&quot;,exp(-20.0)+sin(1.0)-res);&nbsp; &nbsp; // 输出误差<br />
}
vfdff(作者)
2010-08-08 00:20
7
#include&lt;iostream.h&gt;<br />
<br />
float linesqr(float x1,float y1,float x2,float y2)<br />
{return (x2-x1)*(y1+y2)/2.0;}<br />
<br />
void main()<br />
{<br />
float fx,fy,x1,y1,x2,y2,s=0.0;<br />
int n,i;<br />
cout&lt;&lt;&quot;多边形的顶点数&quot;;<br />
do{cin&gt;&gt;n;}<br />
while(n&lt;3);<br />
cout&lt;&lt;&quot;第1个点坐标&quot;&lt;&lt;endl;<br />
cin&gt;&gt;x1&gt;&gt;y1;fx=x1;fy=y1;<br />
cout&lt;&lt;&quot;第2个点坐标&quot;&lt;&lt;endl;<br />
cin&gt;&gt;x2&gt;&gt;y2;<br />
s=linesqr(x1,y1,x2,y2);<br />
for(i=3;i&lt;=n;++i)<br />
{<br />
&nbsp;&nbsp;x1=x2;y1=y2;<br />
&nbsp;&nbsp;cout&lt;&lt;&quot;第&quot;&lt;&lt;i&lt;&lt;&quot;个点坐标&quot;&lt;&lt;endl;<br />
&nbsp;&nbsp;cin&gt;&gt;x2&gt;&gt;y2;<br />
&nbsp;&nbsp;s+=linesqr(x1,y1,x2,y2);<br />
}<br />
s+=linesqr(x2,y2,fx,fy);//首尾相连<br />
cout&lt;&lt;&quot;多边形的面积为&quot;&lt;&lt;s&lt;&lt;endl;<br />
}<br />
<br />
/*******************************************************************************|<br />
今天在论坛看到这样一个问题,程序输入一系列的点,按输入顺序首尾相连构成一个多边形,如何求这个多边形的面积?<br />
<br />
  其实方法很简单,定积分。我还是简单解释一下,如果是没有读过高等数学的朋友,也让你大致明白。<br />
  定积分的本质是求和,计算f(x)在积分区间[a,b]上的一个和S,首先把积分区间分成n份,这样的分法记为λ,<br />
记Δ(λ)=max{Δx|[xi-1,xi]},也就是所有这些分成的小段中长度最大的一段的长,如果当Δ→0的时候,<br />
和式S=∑f(θ)Δx (θ∈[xi-1,xi])的极限如果存在的话,就称其为f(x)在[a,b]上的定积分,记为<br />
b<br />
∫f(x)dx<br />
a<br />
  其意义从几何上解释,就是f(x)的曲线与x轴、直线x=a,x=b围成的图形的面积。<br />
  现在要求的多边形是由线段组成的,只要把所有的线段都求定积分,最后把和加起来,就是多边形的面积。<br />
这个推论的证明从略。值得注意的是,用定积分求的面积有正负之分,即:<br />
b&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;a<br />
∫f(x)dx=-∫f(x)dx<br />
a&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;b<br />
从a积到b,与从b积到a只相差一个负号。<br />
<br />
  线段定积分的计算公式的推导<br />
<br />
给出两个点,如何求这两点连成的线段的定积分值呢?<br />
直线的方程可以用y=kx+b表示,所以围成的面积<br />
S=<br />
x2<br />
∫(kx+b)dx<br />
x1<br />
=k/2(x2^2-x1^2)+b(x2-x1)<br />
<br />
而斜率k=(y2-y1)/(x2-x1)<br />
截距b=y1-kx1=y1-x1(y2-y1)/(x2-x1),代入前式得<br />
S=(y2-y1)(x2+x1)/2+y1(x2-x1)-x1(y2-y1)<br />
=(x2-x1)(y1+y2)/2<br />
这让我想到一个初等公式,梯形面积公式,y1,y2看成上下底,(x2-x1)看成是高,上底加下底乘高除二,<br />
对直线定积分得到的正是这个梯形的面积。这样走了一个大弯又回到初中了。<br />
\*******************************************************************************/
vfdff(作者)
2010-08-08 00:21
8
24点算法<br />
<br />
#include&lt;iostream.h&gt;<br />
#include&lt;conio.h&gt;<br />
//#include&lt;conio.c&gt;<br />
#include&lt;math.h&gt;<br />
#include&lt;string.h&gt;<br />
#include&lt;stdio.h&gt;<br />
#include&lt;stdlib.h&gt;<br />
<br />
char sig[4]=<br />
{<br />
&nbsp; &nbsp; '+','-','*','/'<br />
}<br />
;<br />
<br />
void fun(float n[],char ch[][50],int m)<br />
{<br />
&nbsp; &nbsp; int x[2],y,i,j ;<br />
&nbsp; &nbsp; float num[4];<br />
&nbsp; &nbsp; char cc[4][50];<br />
&nbsp; &nbsp; if(m==1)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(fabs(n[0]-24.0)&lt;0.0001)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;\n%s=24&quot;&lt;&lt;ch[0]; getch(); exit(0);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(x[0]=0;x[0]&lt;m;x[0]++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for(x[1]=0;x[1]&lt;m;x[1]++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(x[1]==x[0]) continue ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for(y=0;y&lt;4;y++)&nbsp; &nbsp; /* + - * / 四种运算 */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;switch(y)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case 0 : num[0]=n[x[0]]+n[x[1]]; break ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case 1 : num[0]=n[x[0]]-n[x[1]]; break ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case 2 : num[0]=n[x[0]]*n[x[1]]; break ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case 3 : if(n[x[1]]==0) break ;&nbsp;&nbsp;/* 分母不能是 0 */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; num[0]=n[x[0]]/n[x[1]]; break ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;default : break ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(y!=3||n[x[1]]!=0)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sprintf(cc[0],&quot;(%s%c%s)&quot;,ch[x[0]],sig[y],ch[x[1]]);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(i=0,j=1;i&lt;m;i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(i!=x[0]&amp;&amp;i!=x[1])<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;num[j]=n<i>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;strcpy(cc[j],ch<i>);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;j++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fun(num,cc,m-1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp; }<br />
}<br />
<br />
/***********************/<br />
main()<br />
{<br />
&nbsp; &nbsp; float num[4];<br />
&nbsp; &nbsp; int i ;<br />
&nbsp; &nbsp; char cx[4][50];<br />
<br />
&nbsp; &nbsp; //clrscr();<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;请输入4个数&quot; ;<br />
&nbsp; &nbsp; for(i=0;i&lt;4;i++)<br />
&nbsp; &nbsp; //cin&gt;&gt;&quot;%f&quot;&gt;&gt;num<i>;<br />
&nbsp; &nbsp; scanf(&quot;%f&quot;,num<i>);<br />
<br />
&nbsp; &nbsp; for(i=0;i&lt;4;i++)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sprintf(cx<i>,&quot;%d&quot;,(int)num<i>);<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; fun(num,cx,4);<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;无解&quot; ;<br />
&nbsp; &nbsp; getch();<br />
}
vfdff(作者)
2010-08-08 00:24
9
// Tc 下菜单设计<br />
<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;graphics.h&gt;<br />
#include &lt;dos.h&gt;<br />
#include &lt;conio.h&gt;<br />
#include &lt;alloc.h&gt;<br />
int m=1,k=1,ak1[5],ak2[5]={1,1,1,1,1};<br />
invel(int);<br />
sav(char*abn[])<br />
{<br />
&nbsp; &nbsp; void*buf ;<br />
&nbsp; &nbsp; unsigned s ;<br />
&nbsp; &nbsp; union scan<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int c ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char ch[2];<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; sc ;<br />
&nbsp; &nbsp; int j=ak1[k],i,I ;<br />
&nbsp; &nbsp; int x1=10+(k-1)*120 ;<br />
&nbsp; &nbsp; int x2=x1+100 ;<br />
&nbsp; &nbsp; int y1=28 ;<br />
&nbsp; &nbsp; int y2=y1+j*10+17 ;<br />
&nbsp; &nbsp; s=imagesize(x1,y1,x2,y2);<br />
&nbsp; &nbsp; buf=malloc(s);<br />
&nbsp; &nbsp; getimage(x1,y1,x2,y2,buf);<br />
&nbsp; &nbsp; setviewport(x1,y1,x2,y2,0);<br />
&nbsp; &nbsp; clearviewport();<br />
&nbsp; &nbsp; bar(0,0,100,y2-y1);<br />
&nbsp; &nbsp; setcolor(0);<br />
&nbsp; &nbsp; rectangle(3,3,96,j*10+13);<br />
&nbsp; &nbsp; rectangle(6,5,93,j*10+11);<br />
&nbsp; &nbsp; for(i=0;i&lt;j;i++)<br />
&nbsp; &nbsp; outtextxy(12,i*10+9,abn<i>);<br />
&nbsp; &nbsp; I=ak2[k];<br />
&nbsp; &nbsp; inve2(1);<br />
&nbsp; &nbsp; i=1 ;<br />
&nbsp; &nbsp; for(;i==1;)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sc.c=get key();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[1]==80)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;ak2[k]=ak2[k]+1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(ak2[k]&gt;ak1[k])<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;ak2[k]=1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=ak2[k]-1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(I==0)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=ak2[k];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inve2(I);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=ak2[k];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inve2(1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[1]==72)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;ak2[k]-=1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(ak2[k]==0)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;ak2[k]=ak1[k];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=ak2[k]+1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if(1&gt;ak1[k])<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=1 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inve2(I);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;I=ak2[k];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;inve2(I);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[0]==13)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;i=0 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;m=0 ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[0]==27)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;i=0 ;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; setcolor(1);<br />
&nbsp; &nbsp; clearviewport();<br />
&nbsp; &nbsp; setviewport(0,0,639,199,0);<br />
&nbsp; &nbsp; putimage(x1,y1,buf,COPY PUT);<br />
}<br />
<br />
invel(int k1)<br />
{<br />
&nbsp; &nbsp; void *buf1;<br />
&nbsp; &nbsp; unsigned s1;<br />
&nbsp; &nbsp; int x1,x2;<br />
&nbsp; &nbsp; x1=10+(k1-1)*120;<br />
&nbsp; &nbsp; x2=x1+45;<br />
&nbsp; &nbsp; s1+imagesize(x1,13,x2,21);<br />
&nbsp; &nbsp; buf1=malloc(s1);<br />
&nbsp; &nbsp; getimage(x1,13,x2,21,buf1);<br />
&nbsp; &nbsp; putimage(x1,12,buf1,NOT_PUT);<br />
}<br />
<br />
inve2(int I)<br />
{<br />
&nbsp; &nbsp; void *buf2;<br />
&nbsp; &nbsp; unsigned s2;<br />
&nbsp; &nbsp; int y1=(I-1)*10+8;<br />
&nbsp; &nbsp; s2=imagesize(6,y1,93,y1+8);<br />
&nbsp; &nbsp; buf2=malloc(s2);<br />
&nbsp; &nbsp; getimage(6,y1,93,y1+8,buf2);<br />
&nbsp; &nbsp; putimage(6,y1,buf2,NOT PUT);<br />
}<br />
<br />
int get key()<br />
{<br />
&nbsp; &nbsp; union REGS r;<br />
&nbsp; &nbsp; r.h.ah=0;<br />
&nbsp; &nbsp; return int86(0x16,&amp;r,&amp;r);<br />
}<br />
<br />
mat(char *ar[],int n,char *ar1[],char *ar2[],char *ar3[],char *ar4[])<br />
{<br />
&nbsp; &nbsp; char *arn[10][10];<br />
&nbsp; &nbsp; int k1;<br />
&nbsp; &nbsp; union scan{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int c;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char ch[2];<br />
&nbsp; &nbsp; }sc ;<br />
&nbsp; &nbsp; int i,as=DETECT,bs;<br />
&nbsp; &nbsp; initgraph(&amp;as,&amp;bs,&quot;&quot;);<br />
&nbsp; &nbsp; cleardevice();<br />
&nbsp; &nbsp; setactivepage(0);<br />
&nbsp; &nbsp; setvisualpage(0);<br />
&nbsp; &nbsp; rectangle(5,10,635,195);<br />
&nbsp; &nbsp; Iine(5,23,635,23);<br />
&nbsp; &nbsp; Iine(5,25,635,25);<br />
&nbsp; &nbsp; for(i=0;i&lt;=n;i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;outtextxy(20+i*120,14,ar<i>);<br />
&nbsp; &nbsp; inve1(k);<br />
&nbsp; &nbsp; i=1;<br />
&nbsp; &nbsp; for(;i==1;){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;sc.c=get key();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[0]==13){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;switch(k){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; case 1:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; strcpy(arn,ar1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; sav(arn);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(m==0) i=0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; case 2:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; strcpy(arn,ar2);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; sav(arn);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(m==0) i=0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; case 3:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; strcpy(arn,ar3);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; sac(arn);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(m==0) i=0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; case 4:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; strcpy(arn,ar4);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; sav(arn);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(m==0) i=0;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[1]==72){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k=k+1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(k&gt;(n+1))&nbsp;&nbsp;k=1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=k-1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(k1==0)&nbsp; &nbsp;k1=n+1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;invel(k1);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=k;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;invel(k1);<br />
&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;&nbsp;&nbsp;if(sc.ch[1]==75){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k=k-1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(k==0) k=n+1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=k+1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(k1&gt;(n+1)) k1=1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;invel(k1) ;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;k1=k;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;invel(k1);<br />
&nbsp; &nbsp;&nbsp;&nbsp;}<br />
&nbsp; &nbsp;}<br />
<br />
}
vfdff(作者)
2010-08-08 00:25
10
简单的计算器<br />
<br />
#include&lt;math.h&gt;<br />
#include&lt;stdlib.h&gt;<br />
#include&lt;iostream.h&gt;<br />
class Calculator<br />
{<br />
&nbsp; &nbsp; double a,b;<br />
public:<br />
&nbsp; &nbsp; Calculator(){a=0;b=0;}; //could be omitted<br />
&nbsp; &nbsp; void newa()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double num;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Input the number:&quot;;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cin&gt;&gt;num;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;a=num;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; void newab()<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;double num1,num2;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Input the numbers.the first number:&quot;;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cin&gt;&gt;num1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Input the senond number:&quot;;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cin&gt;&gt;num2;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;a=num1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;b=num2;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; double Geta(){return a;}<br />
&nbsp; &nbsp; double Getb(){return b;}<br />
&nbsp; &nbsp; double Add(Calculator &amp;A);<br />
&nbsp; &nbsp; double Sub(Calculator *A);<br />
&nbsp; &nbsp; double Mul(Calculator &amp;A);<br />
&nbsp; &nbsp; double Div(Calculator &amp;A);<br />
&nbsp; &nbsp; double Sin(Calculator &amp;A);<br />
&nbsp; &nbsp; double Cos(Calculator &amp;A);<br />
&nbsp; &nbsp; double Tan(Calculator &amp;A);<br />
&nbsp; &nbsp; double Exp(Calculator &amp;A);<br />
&nbsp; &nbsp; double Fabs(Calculator &amp;A);<br />
&nbsp; &nbsp; double LogE(Calculator &amp;A);<br />
&nbsp; &nbsp; double Log10(Calculator &amp;A);<br />
&nbsp; &nbsp; double Pow(double x,double y);<br />
&nbsp; &nbsp; double Sqrt(double x);<br />
};<br />
double Calculator::Add(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return A.a+A.b;<br />
}<br />
double Calculator::Sub(Calculator *A)<br />
{<br />
&nbsp; &nbsp; return A-&gt;a-A-&gt;b;<br />
}<br />
double Calculator::Mul(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return A.a*A.b;<br />
}<br />
double Calculator::Div(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; if(A.b==0)<br />
&nbsp; &nbsp; {cout&lt;&lt;&quot;Error! The program will be terminated!&quot;&lt;&lt;endl;exit(0);}<br />
&nbsp; &nbsp; return A.a/A.b;<br />
}<br />
double Calculator::Sin(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return sin(A.a);<br />
}<br />
double Calculator::Cos(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return cos(A.a);<br />
}<br />
double Calculator::Tan(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return tan(A.a);<br />
}<br />
double Calculator::Exp(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return exp(A.a);<br />
}<br />
double Calculator::Fabs(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return fabs(A.a);<br />
}<br />
double Calculator::LogE(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return log(A.a);<br />
}<br />
double Calculator::Log10(Calculator &amp;A)<br />
{<br />
&nbsp; &nbsp; return log10(A.a);<br />
}<br />
double Calculator::Pow(double x,double y)<br />
{<br />
&nbsp; &nbsp; return pow(x,y);<br />
}<br />
double Calculator::Sqrt(double x)<br />
{<br />
&nbsp; &nbsp; if(x&lt;0)<br />
&nbsp; &nbsp; {cout&lt;&lt;&quot;Error! The program will be terminated!&quot;&lt;&lt;endl;exit(0);}<br />
&nbsp; &nbsp; return sqrt(x);<br />
}<br />
<br />
void main()<br />
{<br />
&nbsp; &nbsp; int sel;<br />
&nbsp; &nbsp; Calculator cal;<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;Welcome to use the calculator!please select.&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;1:\'+\'&nbsp; &nbsp; 2:\'-\'&nbsp; &nbsp;&nbsp;&nbsp;3:\'*\'&nbsp; &nbsp;&nbsp; &nbsp;4:\'/\'&nbsp; &nbsp;&nbsp;&nbsp;5:\'sin\'&nbsp; &nbsp;6:\'cos\'&nbsp;&nbsp;7:\'tan\'&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;8:\'exp\'&nbsp;&nbsp;9:\'fabs\'&nbsp;&nbsp;10:\'logE\'&nbsp;&nbsp;11:\'log10\' 12:\'pow\'&nbsp;&nbsp;13:\'sqrt\'&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp; do<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; cout&lt;&lt;&quot;Please select:(0--exit)&quot;;<br />
&nbsp; &nbsp; cin&gt;&gt;sel;<br />
&nbsp; &nbsp; switch (sel)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; case 0: break;<br />
&nbsp; &nbsp; case 1: cal.newab();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Add(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 2: cal.newab();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Sub(&amp;cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 3: cal.newab();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Mul(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 4: cal.newab();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Div(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 5: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Sin(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 6: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Cos(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 7: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Tan(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 8: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Exp(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 9: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Fabs(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 10: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.LogE(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 11: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Log10(cal)&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 12: cal.newab();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Pow(cal.Geta(),cal.Getb())&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; case 13: cal.newa();<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cout&lt;&lt;&quot;The result is:&quot;&lt;&lt;cal.Sqrt(cal.Geta())&lt;&lt;endl;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br />
&nbsp; &nbsp; default: cout&lt;&lt;&quot;Your selection is ERROR! Select again!&quot;&lt;&lt;endl;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }while(sel!=0);<br />
}
vfdff(作者)
2010-08-08 00:26
11
产生随机数<br />
<br />
// 利用时间time 做种子,这样每次运行的种子均不一样, 从而可以得到真正的随机数<br />
// 2006.6.13&nbsp;&nbsp;C&amp;C++ 实效编程百例 人民邮电出版社 page 153<br />
#include&nbsp; &nbsp; &lt;iostream.h&gt;<br />
#include&nbsp; &nbsp; &lt;stdlib.h&gt;<br />
#include&nbsp; &nbsp; &lt;time.h&gt;<br />
<br />
int main()<br />
{<br />
&nbsp; &nbsp; srand(time(0));&nbsp; &nbsp; // 时间 time 做种子<br />
&nbsp; &nbsp; for(int i=0;i&lt;9;i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cout&lt;&lt;rand()&lt;&lt;endl;<br />
&nbsp; &nbsp; return 0;<br />
}
vfdff(作者)
2010-08-08 00:34
12
二分法<br />
<br />
#define&nbsp; &nbsp;&nbsp;&nbsp;MAX&nbsp; &nbsp;&nbsp;&nbsp;3<br />
#include&nbsp; &nbsp; &lt;stdio.h&gt;<br />
static double&nbsp;&nbsp;data[MAX];<br />
<br />
double Renew(double data[],double x)<br />
{<br />
&nbsp; &nbsp; int i;<br />
&nbsp; &nbsp; double sum = 0.0;<br />
&nbsp; &nbsp; for(i=0; i&lt;MAX-1; i++) /* 前MAX-1个数前移 */<br />
&nbsp; &nbsp;&nbsp; &nbsp; data<i> = data[i+1];<br />
&nbsp; &nbsp; data[MAX-1] = x;<br />
&nbsp; &nbsp; for(i=0; i&lt;MAX; i++)<br />
&nbsp; &nbsp;&nbsp; &nbsp; sum =sum + data<i>;<br />
&nbsp; &nbsp; printf(&quot;sum=%7.3f&quot;,sum);<br />
&nbsp; &nbsp; return sum/MAX;<br />
}<br />
<br />
<br />
<br />
<br />
/* 一元全区间不等距插值 */<br />
<br />
double enlgr(double x[],double y[],int n,double t)<br />
{<br />
&nbsp; &nbsp; int i;&nbsp; &nbsp;/* 这里假设单调变化,且t在x<i>和x[i-1]之间成立 */<br />
&nbsp; &nbsp; double z;<br />
&nbsp; &nbsp; if(t&lt;x[0]+1e-03&amp;&amp;t&gt;x[0]-1e-03)&nbsp;&nbsp;return y[0];<br />
&nbsp; &nbsp; for(i=1;i&lt;n;i++)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if((x<i>-t)*(x[i-1]-t)&lt;0) break; /* 找到t所在区间 */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if(t&lt;x<i>+1e-03&amp;&amp;t&gt;x<i>-1e-03)&nbsp;&nbsp;return y<i>;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; z=(y<i>-y[i-1])*(t-x[i-1])/(x<i>-x[i-1]);<br />
&nbsp; &nbsp; return&nbsp;&nbsp;z+y[i-1];<br />
}<br />
<br />
main()<br />
{<br />
&nbsp; &nbsp; double t,z;<br />
&nbsp; &nbsp; static double x[10]={0.10,0.15,0.25,0.40,0.50,0.57,0.70,0.85,0.93,1.00};<br />
&nbsp; &nbsp; static double y[10]={0.904837,0.860708,0.778801,0.670320,0.606531,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 0.565525,0.496585,0.427415,0.394554,0.367879};<br />
&nbsp; &nbsp; int i&nbsp;&nbsp;;<br />
&nbsp; &nbsp; for(i=0;i&lt;10;i++){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;t=Renew(data,x<i>);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;z=enlgr(x,y,10,t);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;t=%6.3f,\tz=%e\n&quot;,t,z);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;\n&quot;);<br />
&nbsp; &nbsp; }<br />
}
vfdff(作者)
2010-08-08 00:35
13
//遗传算法示例源程序<br />
//http://www.lasg.ac.cn/cgi-bin/forum/topic.cgi?forum=4&amp;topic=1535<br />
<br />
/***************************************************************/<br />
/* This is a simple genetic algorithm implementation where the */<br />
/* evaluation function takes positive values only and the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* fitness of an individual is the same as the value of the&nbsp; &nbsp; */<br />
/* objective function&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/***************************************************************/<br />
<br />
#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;math.h&gt;<br />
<br />
/* Change any of these parameters to match your needs */<br />
<br />
#define POPSIZE 50&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* population size */<br />
#define MAXGENS 1000&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* max. number of generations */<br />
#define NVARS 3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* no. of problem variables */<br />
#define PXOVER 0.8&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* probability of crossover */<br />
#define PMUTATION 0.15&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* probability of mutation */<br />
#define TRUE 1<br />
#define FALSE 0<br />
<br />
int generation;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* current generation no. */<br />
int cur_best;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* best individual */<br />
FILE *galog;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* an output file */<br />
<br />
struct genotype /* genotype (GT), a member of the population */<br />
{<br />
double gene[NVARS];&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* a string of variables */<br />
double fitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* GT's fitness */<br />
double upper[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables upper bound */<br />
double lower[NVARS];&nbsp; &nbsp;&nbsp; &nbsp; /* GT's variables lower bound */<br />
double rfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* relative fitness */<br />
double cfitness;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* cumulative fitness */<br />
};<br />
<br />
struct genotype population[POPSIZE+1];&nbsp; &nbsp; /* population */<br />
struct genotype newpopulation[POPSIZE+1]; /* new population; */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* replaces the */<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* old generation */<br />
<br />
/* Declaration of procedures used by this genetic algorithm */<br />
<br />
void initialize(void);<br />
double randval(double, double);<br />
void evaluate(void);<br />
void keep_the_best(void);<br />
void elitist(void);<br />
void select(void);<br />
void crossover(void);<br />
void Xover(int,int);<br />
void swap(double *,double *);<br />
void mutate(void);<br />
void report(void);<br />
<br />
/***************************************************************/<br />
/* Initialization function: Initializes the values of genes&nbsp; &nbsp; */<br />
/* within the variables bounds. It also initializes (to zero)&nbsp;&nbsp;*/<br />
/* all fitness values for each member of the population. It&nbsp; &nbsp; */<br />
/* reads upper and lower bounds of each variable from the&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* input file `gadata.txt'. It randomly generates values&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* between these bounds for each gene of each genotype in the&nbsp;&nbsp;*/<br />
/* population. The format of the input file `gadata.txt' is&nbsp; &nbsp; */<br />
/* var1_lower_bound var1_upper bound&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/* var2_lower_bound var2_upper bound ...&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void initialize(void)<br />
{<br />
FILE *infile;<br />
int i, j;<br />
double lbound, ubound;<br />
<br />
if ((infile = fopen(&quot;gadata.txt&quot;,&quot;r&quot;))==NULL)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; fprintf(galog,&quot;nCannot open input file!n&quot;);<br />
&nbsp; &nbsp; exit(1);<br />
&nbsp; &nbsp; }<br />
<br />
/* initialize variables within the bounds */<br />
<br />
for (i = 0; i [color=0x555555] = lbound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].upper= ubound;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].gene = randval(population[j].lower,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;population[j].upper);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<br />
&nbsp; &nbsp; }<br />
<br />
fclose(infile);<br />
}<br />
<br />
/***********************************************************/<br />
/* Random value generator: Generates a value within bounds */<br />
/***********************************************************/<br />
<br />
double randval(double low, double high)<br />
{<br />
double val;<br />
val = ((double)(rand()%1000)/1000.0)*(high - low) + low;<br />
return(val);<br />
}<br />
<br />
/*************************************************************/<br />
/* Evaluation function: This takes a user defined function.&nbsp;&nbsp;*/<br />
/* Each time this is changed, the code has to be recompiled. */<br />
/* The current function is:&nbsp;&nbsp;x[1]^2-x[1]*x[2]+x[3]&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/*************************************************************/<br />
<br />
void evaluate(void)<br />
{<br />
int mem;<br />
int i;<br />
double x[NVARS+1];<br />
<br />
for (mem = 0; mem [color=#555555];<br />
<br />
&nbsp; &nbsp; population[mem].fitness = (x[1]*x[1]) - (x[1]*x[2]) + x[3];<br />
&nbsp; &nbsp; }<br />
}<br />
<br />
/***************************************************************/<br />
/* Keep_the_best function: This function keeps track of the&nbsp; &nbsp; */<br />
/* best member of the population. Note that the last entry in&nbsp;&nbsp;*/<br />
/* the array Population holds a copy of the best individual&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void keep_the_best()<br />
{<br />
int mem;<br />
int i;<br />
cur_best = 0; /* stores the index of the best individual */<br />
<br />
for (mem = 0; mem&nbsp;&nbsp;population[POPSIZE].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; cur_best = mem;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; population[POPSIZE].fitness = population[mem].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* once the best member in the population is found, copy the genes */<br />
for (i = 0; i [color=#555555] = population[cur_best].gene;<br />
}<br />
<br />
/****************************************************************/<br />
/* Elitist function: The best member of the previous generation */<br />
/* is stored as the last in the array. If the best member of&nbsp; &nbsp; */<br />
/* the current generation is worse then the best member of the&nbsp;&nbsp;*/<br />
/* previous generation, the latter one would replace the worst&nbsp;&nbsp;*/<br />
/* member of the current population&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/****************************************************************/<br />
<br />
void elitist()<br />
{<br />
int i;<br />
double best, worst;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* best and worst fitness values */<br />
int best_mem, worst_mem; /* indexes of the best and worst member */<br />
<br />
best = population[0].fitness;<br />
worst = population[0].fitness;<br />
for (i = 0; i [color=#555555].fitness &gt; population[i+1].fitness)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population.fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best = population.fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population[i+1].fitness [color=#555555].fitness [color=#555555].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; worst_mem = i;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if (population[i+1].fitness &gt;= best)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best = population[i+1].fitness;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; best_mem = i + 1;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* if best individual from the new population is better than */<br />
/* the best individual from the previous population, then&nbsp; &nbsp; */<br />
/* copy the best from the new population; else replace the&nbsp; &nbsp;*/<br />
/* worst individual from the current population with the&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/* best one from the previous generation&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
<br />
if (best &gt;= population[POPSIZE].fitness)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;for (i = 0; i [color=#555555] = population[best_mem].gene;<br />
&nbsp;&nbsp;population[POPSIZE].fitness = population[best_mem].fitness;<br />
&nbsp;&nbsp;}<br />
else<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;for (i = 0; i [color=#555555] = population[POPSIZE].gene;<br />
&nbsp;&nbsp;population[worst_mem].fitness = population[POPSIZE].fitness;<br />
&nbsp;&nbsp;}<br />
}<br />
/**************************************************************/<br />
/* Selection function: Standard proportional selection for&nbsp; &nbsp; */<br />
/* maximization problems incorporating elitist model - makes&nbsp;&nbsp;*/<br />
/* sure that the best member survives&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void select(void)<br />
{<br />
int mem, i, j, k;<br />
double sum = 0;<br />
double p;<br />
<br />
/* find total fitness of the population */<br />
for (mem = 0; mem [color=#555555] = population[0];<br />
&nbsp; &nbsp; else<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; for (j = 0; j = population[j].cfitness &amp;&amp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; p[color=#555555] = population[j+1];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
/* once a new population is created, copy it back */<br />
<br />
for (i = 0; i [color=#555555] = newpopulation;<br />
}<br />
<br />
/***************************************************************/<br />
/* Crossover selection: selects two parents that take part in&nbsp;&nbsp;*/<br />
/* the crossover. Implements a single point crossover&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/***************************************************************/<br />
<br />
void crossover(void)<br />
{<br />
int i, mem, one;<br />
int first&nbsp;&nbsp;=&nbsp;&nbsp;0; /* count of the number of members chosen */<br />
double x;<br />
<br />
for (mem = 0; mem&nbsp;&nbsp;1)<br />
 {<br />
 if(NVARS == 2)<br />
&nbsp; &nbsp;&nbsp; &nbsp; point = 1;<br />
 else<br />
&nbsp; &nbsp;&nbsp; &nbsp; point = (rand() % (NVARS - 1)) + 1;<br />
<br />
 for (i = 0; i [color=#555555], &amp;population[two].gene);<br />
<br />
 }<br />
}<br />
<br />
/*************************************************************/<br />
/* Swap: A swap procedure that helps in swapping 2 variables */<br />
/*************************************************************/<br />
<br />
void swap(double *x, double *y)<br />
{<br />
double temp;<br />
<br />
temp = *x;<br />
*x = *y;<br />
*y = temp;<br />
<br />
}<br />
<br />
/**************************************************************/<br />
/* Mutation: Random uniform mutation. A variable selected for */<br />
/* mutation is replaced by a random value between lower and&nbsp; &nbsp;*/<br />
/* upper bounds of this variable&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;*/<br />
/**************************************************************/<br />
<br />
void mutate(void)<br />
{<br />
int i, j;<br />
double lbound, hbound;<br />
double x;<br />
<br />
for (i = 0; i [color=#555555].lower[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; hbound = population.upper[j];<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; population.gene[j] = randval(lbound, hbound);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<br />
}<br />
<br />
/***************************************************************/<br />
/* Report function: Reports progress of the simulation. Data&nbsp; &nbsp;*/<br />
/* dumped into the&nbsp;&nbsp;output file are separated by commas&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;*/<br />
/***************************************************************/<br />
<br />
void report(void)<br />
{<br />
int i;<br />
double best_val;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;/* best population fitness */<br />
double avg;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* avg population fitness */<br />
double stddev;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* std. deviation of population fitness */<br />
double sum_square;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* sum of square for std. calc */<br />
double square_sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /* square of sum for std. calc */<br />
double sum;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;/* total population fitness */<br />
<br />
sum = 0.0;<br />
sum_square = 0.0;<br />
<br />
for (i = 0; i [color=#555555].fitness;<br />
&nbsp; &nbsp; sum_square += population.fitness * population.fitness;<br />
&nbsp; &nbsp; }<br />
<br />
avg = sum/(double)POPSIZE;<br />
square_sum = avg * avg * POPSIZE;<br />
stddev = sqrt((sum_square - square_sum)/(POPSIZE - 1));<br />
best_val = population[POPSIZE].fitness;<br />
<br />
fprintf(galog, &quot;n%5d,&nbsp; &nbsp;&nbsp; &nbsp;%6.3f, %6.3f, %6.3f nn&quot;, generation,<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;best_val, avg, stddev);<br />
}<br />
<br />
/**************************************************************/<br />
/* Main function: Each generation involves selecting the best */<br />
/* members, performing crossover &amp; mutation and then&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/* evaluating the resulting population, until the terminating */<br />
/* condition is satisfied&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; */<br />
/**************************************************************/<br />
<br />
void main(void)<br />
{<br />
int i;<br />
<br />
if ((galog = fopen(&quot;galog.txt&quot;,&quot;w&quot;))==NULL)<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; exit(1);<br />
&nbsp; &nbsp; }<br />
generation = 0;<br />
<br />
fprintf(galog, &quot;n generation&nbsp;&nbsp;best&nbsp;&nbsp;average&nbsp;&nbsp;standard n&quot;);<br />
fprintf(galog, &quot; number&nbsp; &nbsp;&nbsp; &nbsp;value fitness&nbsp;&nbsp;deviation n&quot;);<br />
<br />
initialize();<br />
evaluate();<br />
keep_the_best();<br />
while(generation[color=#555555]);<br />
 }<br />
fprintf(galog,&quot;nn Best fitness = %3.3f&quot;,population[POPSIZE].fitness);<br />
fclose(galog);<br />
printf(&quot;Successn&quot;);<br />
}<br />
/***************************************************************/
vfdff(作者)
2010-08-08 10:04
14
#include &lt;stdio.h&gt;<br />
/* 把 n 用m 进制显示 */<br />
<br />
<br />
// #include &lt;WATLIB.H&gt; 测试表明lib库不通用<br />
#include &lt;bitset&gt;<br />
#include &lt;vcLIB.H&gt;<br />
typedef&nbsp;&nbsp;char BOOL ;<br />
typedef&nbsp;&nbsp;unsigned char UCHAR8 ;<br />
<br />
#define&nbsp;&nbsp;NUM&nbsp;&nbsp;10<br />
<br />
void PN(int n,int m)&nbsp;&nbsp;/* 把 n 用m 进制显示 */<br />
{<br />
&nbsp; &nbsp; char temp[10]={0};<br />
&nbsp; &nbsp; int i=0;<br />
&nbsp; &nbsp; do {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;temp<i> = n%m;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;i++;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;n /= m;<br />
&nbsp; &nbsp; }while(n&gt;0);<br />
&nbsp; &nbsp; for(i -= 1;i&gt;=0;i--){<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;printf(&quot;%x&quot;,temp<i>);<br />
&nbsp; &nbsp; }<br />
}<br />
<br />
int main()<br />
{<br />
&nbsp; &nbsp; int i= 3;<br />
&nbsp; &nbsp; &nbsp; &nbsp; bitset&lt; 32 &gt; bitvec = 4;<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;%x&quot;,bitvec.set());<br />
<br />
&nbsp; &nbsp; return 0;<br />
}
游客请输入验证码
浏览1943036次