| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 764 人关注过本帖
标题:一个C++的程序怎么移植成C#
只看楼主 加入收藏
qzhq1984
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-4-8
收藏
 问题点数:0 回复次数:2 
一个C++的程序怎么移植成C#

#include<iostream.h>
#include <math.h>
#define N 64
#define m 6 //2^m=N
double twiddle[N/2]={1,0.9951,0.9808,0.9570,0.9239,0.8820,0.8317,0.7733,
0.7075,0.6349,0.5561,0.4721,0.3835,0.2912,0.1961,0.0991,
0.0000,-0.0991,-0.1961,-0.2912,-0.3835,-0.4721,-0.5561,-0.6349,
-0.7075,-0.7733,0.8317,-0.8820,-0.9239,-0.9570,-0.9808,-0.9951,}; //N=64
double x_r[N]={1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,};
double x_i[N];

void fft_init() //为数组X_i赋值
{
int i;
for(i=0;i<N;i++)
x_i[i]=0.0;
}
void bitrev()
{
int p=1,q,i;
int bit_rev[N];
double xx_r[N];
bit_rev[0]=0;
while(p<N)
{
for(q=0;q<p;q++)
{
bit_rev[q]=bit_rev[q]*2;
bit_rev[q+p]=bit_rev[q]+1;
}
p=p*2;
}
for(i=0;i<N;i++)
{
xx_r[i]=x_r[i];
}
for(i=0;i<N;i++)
{
x_r[i]=xx_r[bit_rev[i]];
}
}

void display()
{
int i;
for(i=0;i<N;i++)
cout<<x_r[i]<<","<<x_i[i]<<endl;
}

void fft1()
{
int L,i,b,j,p,k,tx1,tx2;
double TI,TR,temp;
double tw1,tw2;

for(L=1;L<=m;L++)
{ /* for(1) */
b=1; i=L-1;
while(i>0)
{b=b*2;i--;} /* b= 2^(L-1) */
for(j=0;j<=b-1;j++) /* for (2) */
{ p=1; i=m-L;
while(i>0) /* p=pow(2,7-L)*j; */
{p=p*2; i--;}
p=p*j;
tx1=p%(N);
tx2=tx1+(3*N)/4;
tx2=tx2%(N);
if (tx1>=(N/2))
tw1=-twiddle[tx1-(N/2)];
else
tw1=twiddle[tx1];
if (tx2>=(N/2))
tw2=-twiddle[tx2-(N/2)];
else
tw2=twiddle[tx2];
for(k=j;k<N;k=k+2*b) /* for (3) */
{TR=x_r[k]; TI=x_i[k]; temp=x_r[k+b];
x_r[k]=x_r[k]+x_r[k+b]*tw1+x_i[k+b]*tw2;
x_i[k]=x_i[k]-x_r[k+b]*tw2+x_i[k+b]*tw1;
x_r[k+b]=TR-x_r[k+b]*tw1-x_i[k+b]*tw2;
x_i[k+b]=TI+temp*tw2-x_i[k+b]*tw1;
}
}
}

}


int main(int argc, char* argv[])
{

fft_init();
bitrev();
fft1();
display(); //FFT
return 0;
}

把他变为C# 的类

搜索更多相关主题的帖子: 移植 
2006-04-27 22:27
tpy9527
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-5-2
收藏
得分:0 

一个C++的程序怎么移植成C#

bp算法的vc++代码
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define Beta 1.0
#define NAME_MAX 80
double
bp(w_ih,w_ho,patt,targ,nn,hh,pp,mm,fin,fileW,
resultF,crition,total,sa)
double **w_ih;/* weight from input to hidden */
double **w_ho;/* weight from hidden to output */
double **patt;/* input patterns */
double **targ;/* desired outputs */
intnn;/* input dim */
int hh;/* hidden dim */
int pp;/* output dim */
int mm;/* training patterns number */
int*fin;/* flag for terminate */
char*fileW;/* weightFile */
char*resultF;/* resultFile Name */
doublecrition;/* minimum mean square */
int total;
int sa;/* flag for saving weights */
{
double *output;
int i,j,s,num_p,num_d,mis,mis_tr,mis_te;
double err;
char Msg[NAME_MAX],Msg1[NAME_MAX],Msg2[NAME_MAX],
Msg3[NAME_MAX],Msg4[NAME_MAX],Msg5[NAME_MAX];
FILE *fp;
FILE *cor;
extern int maximum(double *,int);
//extern void drawbp(double **,double **,int,int,int);
void feedward(double **,double **,double *,double *,int,int,int);
double errSqu(double *,double *,int);
output=(double *)calloc(pp,sizeof(double));
err=0.0;
for (i=0;i<mm;i++)
{
feedward(w_ih,w_ho,patt,output,nn,hh,pp);
err +=errSqu(output,targ,pp);
}

/* err=sqrt(err/(pp*mm)); mean-square-root */
err=err/(pp*mm); /* mean-square */
if ((err<=crition)||(sa==1))
{
/* draw bp */
//drawbp(w_ih,w_ho,nn,hh,pp);
if (err<=crition)
*fin=1;
if ((fp=fopen(fileW,"wt"))==NULL)
{
fprintf(stderr,"cannot open %s file for writing\n",fileW);
exit(1);
}
sprintf(Msg,"weight from input to hidden");
fprintf(fp,"%s\n",Msg);
for (j=0;j<hh;j++)
{
for (s=0;s<(nn+1);s++)
fprintf(fp,"%f\t",w_ih[j][s]);
fprintf(fp,"\n");
}
sprintf(Msg,"weight from hidden to output");
fprintf(fp,"%s\n",Msg);
for (j=0;j<pp;j++)
{
for (s=0;s<(hh+1);s++)
fprintf(fp,"%f\t",w_ho[j][s]);
fprintf(fp,"\n");
}
fclose(fp);
if ((cor=fopen(resultF,"wt"))==NULL)
{
fprintf(stderr,"cannot open %s for writing\n",resultF);
exit(1);
}
sprintf(Msg,"Index");
sprintf(Msg1,"Class_Get");
sprintf(Msg2,"Class_Des");
sprintf(Msg3,"Out_1");
sprintf(Msg4,"Out_2");
sprintf(Msg5,"Out_3");
fprintf(cor,"%10s %10s %10s %10s %10s %10s\n",Msg,Msg1,Msg2,Msg3,Msg4,Msg5);
mis=0;
mis_tr=0;
mis_te=0;
for (i=0;i<total;i++)
{
feedward(w_ih,w_ho,patt,output,nn,hh,pp);
num_p=maximum(output,pp);
num_d=maximum(targ,pp);
if (num_p!=num_d)
{
if (i<mm)
mis_tr++;
else
mis_te++;
mis++;
}
fprintf(cor,"%10.3d %10.1d %10.1d",i,num_p,num_d);
for (j=0;j<pp;j++)
fprintf(cor,"%10.6f",output[j]);
fprintf(cor,"\n");
}
fprintf(cor,"Total number of misclassfication: %d\n",mis);
fprintf(cor," for traing: %d\t for test: %d\n",mis_tr,mis_te);
fclose(cor);
gotoxy(57,2);
printf("Error:%6.6f",err);
gotoxy(57,3);
printf("traing:%3.3d test:%3.3d",mis_tr,mis_te);
}
free(output);
return(err);
}
void
feedward(wih,who,in,out,n1,h1,p1)
double **wih;/* weight from input to hidden */
double **who;/* weight from hidden to output */
double *in;/* input vector */
double *out;/* output vector */
intn1;/* input dim */
int h1;/* hidden dim */
int p1;/* output dim */
{
int i,j;
double sum;
double *out_h;
double sigmoid(double);

out_h=(double *)calloc(h1,sizeof(double));
/* calculate the outputs of hidden neurons */
for (i=0;i<h1;i++)
{
sum=0.0;
for (j=0;j<n1;j++)
sum +=((wih[j])*(in[j]));
sum +=wih[n1];
out_h=sigmoid(sum);
}
/* calculate the network output */
for (i=0;i<p1;i++)
{
sum =0.0;
for (j=0;j<h1;j++)
sum +=who[j]*out_h[j];
sum +=who[h1];
out=sigmoid(sum);
}
free(out_h);
}
double
errSqu(ou,ta,le)
double *ou;/* practical output */
double *ta;/* desired output */
int le;/* output dim */
{
int i;
double dif,sum;
sum=0.0;
for (i=0;i<le;i++)
{
dif=ou-ta;
sum +=(dif*dif);
}
return(sum);
}
double
sigmoid(x)
double x;
{
x=(tanh(x*Beta)+1.0)/2;
return(x); }

2006-05-02 22:34
Vika
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2006-4-21
收藏
得分:0 

大家好!如果是C#的爱好者请加入C#群:16103518,方便大家学习交流!支持一下!


2006-05-03 11:00
快速回复:一个C++的程序怎么移植成C#
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016883 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved