PDA

View Full Version : سوال: کمک کنید:تبدیل کد سی پلاس پلاس به سی شارپ



hellox
جمعه 08 اردیبهشت 1391, 10:59 صبح
سلام بر اساتید برنامه نویس
من میخوام کد زیر که به زبان ++C نوشته شده به زبان #C تبدیل کنم.خیلی ممنون میشم که در این مورد کمکم کنیدخیلی ضروریه
کار برنامه حل مساله n وزیر با روش ژنتیک هست

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct Chrom
{
short int bit[6];
int fit;
}chrom;
void *evpop(chrom popcurrent[4]);
int x(chrom popcurrent);
int y(int x);
void *pickchroms(chrom popcurrent[4]);
void *crossover(chrom popnext[4]);
void *mutation(chrom popnext[4]);
void main()
{
int num;
int i,j;
printf("find the maximum function [y=x*x]\n");
enter: printf("\nPlease enter the no. of iterations: ");
scanf("%d",&num);
chrom popcurrent[4];
chrom popnext[4];
if(num<1)
goto enter;
evpop(popcurrent);
for(i=0;i<num;i++)
{
printf("\ni = %d\n",i);
for(j=0;j<4;j++)
popnext[j]=popcurrent[j];
pickchroms(popnext);
crossover(popnext);
mutation(popnext);
for(j=0;j<4;j++)
popcurrent[j]=popnext[j];
}
printf("\nPress any key to end ! ");
flushall();
getche();
}
void *evpop(chrom popcurrent[4])
{
int i,j,value;
int random;
for(j=0;j<4;j++)
{
for(i=0;i<6;i++)
{
random=rand();
random=(random%2);
popcurrent[j].bit[i]=random;
} // end of for(i)

value=x(popcurrent[j]);
popcurrent[j].fit=y(x(popcurrent[j]));
printf("\n popcurrent[%d]=%d%d%d%d%d%d value=%d fitness = %d",j,popcurrent[j].bit[5],popcurrent[j].bit[4],popcurrent[j].bit[3],popcurrent[j].bit[2],popcurrent[j].bit[1],popcurrent[j].bit[0],value,popcurrent[j].fit);
}
return(0);
}
int x(chrom popcurrent)
{
int z;
z=(popcurrent.bit[0]*1)+(popcurrent.bit[1]*2)+(popcurrent.bit[2]*4)+(popcurrent.bit[3]*8)+(popcurrent.bit[4]*16);
return(z);
}
int y(int x)
{
int y;
y=(x*x);
return(y);
}
void *pickchroms(chrom popcurrent[4])
{
int i,j;
chrom temp;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(popcurrent[j+1].fit>popcurrent[j].fit)
{
temp=popcurrent[j+1];
popcurrent[j+1]=popcurrent[j];
popcurrent[j]=temp;
}
}
for(i=0;i<4;i++)
printf("\nSorting:popnext[%d] fitness=%d",i,popcurrent[i].fit);
printf("\n");
flushall();
return(0);
}
void *crossover(chrom popnext[4])
{
int random;
int i;
random=rand();
random=((random%5)+1);
for(i=0;i<random;i++)
{
popnext[2].bit[i]=popnext[0].bit[i];
popnext[3].bit[i]=popnext[1].bit[i];
} // end of for
for(i=random;i<6;i++)
{
popnext[2].bit[i]=popnext[1].bit[i];
popnext[3].bit[i]=popnext[0].bit[i];
} // end of for
for(i=0;i<4;i++)
popnext[i].fit=y(x(popnext[i]));
for(i=0;i<4;i++)
printf("\nCrossOver popnext[%d]=%d%d%d%d%d%d value=%d fitness = %d",i,popnext[i].bit[5],popnext[i].bit[4],popnext[i].bit[3],popnext[i].bit[2],popnext[i].bit[1],popnext[i].bit[0],x(popnext[i]),popnext[i].fit);
return(0);
}
void *mutation(chrom popnext[4])
{
int random;
int row,col,value;
random=rand()%50;
if (random==25)
{
col=rand()%6;
row=rand()%4;
if (popnext[row].bit[col]==0)
popnext[row].bit[col]=1 ;
else if (popnext[row].bit[col]==1)
popnext[row].bit[col]=0;
popnext[row].fit=y(x(popnext[row]));
value=x(popnext[row]);
printf("\nMutation occured in popnext[%d] bit[%d]:=%d%d%d%d%d%d value=%d fitness=%d",row,col,popnext[row].bit[5],popnext[row].bit[4],popnext[row].bit[3],popnext[row].bit[2],popnext[row].bit[1],popnext[row].bit[0],value,popnext[row].fit);
}
return(0);
}

bade saba
جمعه 08 اردیبهشت 1391, 13:00 عصر
http://www.softpedia.com/get/Programming/Other-Programming-Files/Convert-C-To-C.shtml