Exemplo n.º 1
0
int main()
{

	system("color f0");
	shuoming();
    
    short cuo=0;
    node *tou1=NULL,*tou2=NULL,* result=NULL;
    while(1)
	{  
		do
		{
			cout<<"请选择功能:"<<endl;
			cin>>yunsuan;
			if(!cin)
			{
				cout<<"您输入的不是数字,请重新运行程序"<<endl;
			    exit(0);
			}//if
			switch(yunsuan)
			{
			     case 1:cuo=0;jia(tou1,tou2,result);break;
		         case 2:cuo=0;jian(tou1,tou2,result);break;
	    	     case 3:cuo=0;cheng(tou1,tou2,result);break;
	             case 4:cuo=0;jifen(tou1,result);break;
			     case 5:cuo=0;weifen(tou1,result);break;
		         case 6:cuo=0;qiuzhi(tou1,result);break;
                 case 7:cuo=0;init(tou1);cout<<"输入各项:"<<endl;cout<<"y="<<endl;fuzhi(tou1);outxu(tou1,result);break;
				 default:cuo=1;cout<<"超出功能选择范围,请重新选择:"<<endl;
			}//switch
			
		} while(cuo==1);//do
        
		out(result);
		destory(tou1);destory(tou2);destory(result);
	
		short i=0;
		cout<<"是否清屏(1:是)(0:否):";
		cin>>i;
		if(i==1) {system("cls");shuoming();}
		else     cout<<"*********************"<<endl;
	    
	}//while
    return 0;
}
Exemplo n.º 2
0
void generation()
{
    individual fuqin1,fuqin2,*pipeiguodu,*pipeichi;
    int *peiduishuzu;//用来存放产生的随机配对
    pipeiguodu=new individual[zhongqunshu1];
    pipeichi=new individual[zhongqunshu1];
    peiduishuzu=new int[zhongqunshu1];
    int member1,member2,j=0,fuzhijishu=0,i=0,temp=0,tt=0;
    float zhizhen;
    //随机遍历的实现
    for(zhizhen=suijibianli();zhizhen<1;(zhizhen=zhizhen+zhizhenjuli))//设定指针1/66
    {
        pipeichi[fuzhijishu]=nowpop[fuzhi(zhizhen)];
        fuzhijishu++;
    }

    //交叉与变异的实现
    //交叉
    for(i=0;i<zhongqunshu1;i++)
    {
        peiduishuzu[i]=-1;
    }
    for (i=0; i<zhongqunshu1; i++)
    { 
        temp =rnd(0,zhongqunshu1-1); //产生值在0-zhongqunshu1-1的随机数 
        while(Exist(temp, i, peiduishuzu))//判断产生的随机数是否已经产生过,如果是,则再产生一个随机数 
        {
            temp =rnd(0,zhongqunshu1-1);
        }
        //如果没有的话,则把产生的随机数放在peiduishuzu中 
        *(peiduishuzu+i) = temp; 
    }
    for(i=0;i<zhongqunshu1-1;i=i+2)
    {
        fuqin1=pipeichi[peiduishuzu[i]];
        fuqin2=pipeichi[peiduishuzu[i+1]];
        crossover(fuqin1,fuqin2,newpop[i],newpop[i+1]);
    }
    for(j=0;j<zhongqunshu1;j++)
    {
        //if(newpop[j].geti<-1000)
        //cout<<"个体数值小于下界了";
        nowpop[j].geti=newpop[j].geti;
    }
    //
    guanjiancanshujisuan();
    //变异的实现
    for(j=0;j<zhongqunshu;j++)
    {
        bianyi(nowpop[j]);
    } 
    //
    guanjiancanshujisuan();
    //精英保留的实现
    jingyingbaoliu();
    //
    guanjiancanshujisuan();
    delete [] peiduishuzu;
    delete [] pipeichi;
    delete [] pipeiguodu;
}