环境 vs2010
c++类库项目,在 c#中 直接调用
还用的代码如下
c++类库
#pragma once
#include <iostream>
#include <cmath>
using namespace std;
//供C#代码使用
extern "C" {
__declspec(dllexport) void* complex_create(double real, double imag);
__declspec(dllexport) void complex_destroy(void* ptr);
__declspec(dllexport) void complex_prt(void* ptr);
__declspec(dllexport) double complex_cfabs(void* ptr);
__declspec(dllexport) double complex_angle(void* ptr);
__declspec(dllexport) void* complex_add(void* ptr, void* ptr2);
__declspec(dllexport) void* complex_sub(void* ptr, void* ptr2);
__declspec(dllexport) void* complex_mul(void* ptr, void* ptr2);
__declspec(dllexport) void* complex_div(void* ptr, void* ptr2);
__declspec(dllexport) void* complex_cpower(void* ptr, int n);
__declspec(dllexport) void complex_croot(void* ptr, int n, void** p);
__declspec(dllexport) void* complex_cexp(void* ptr);
__declspec(dllexport) void* complex_clog(void* ptr);
__declspec(dllexport) void* complex_csin(void* ptr);
__declspec(dllexport) void* complex_ccos(void* ptr);
}
class Complex {
private:
double R;
double I;
public:
//////////////////
//构造函数
//////////////////
Complex(double real = 0, double imag = 0) { R = real; I = imag; }
//////////////////
//复数输出
//////////////////
void prt() {
//输出形式为:(实部, 虚部)
cout << "(" << R << ", " << I << ")";
return;
}
//////////////////
//复数模
//////////////////
double cfabs() {
double y;
y = sqrt(R * R + I * I);
return y;
}
//////////////////
//复数幅角
//////////////////
double angle() {
double y;
y = atan2(I, R);
return (y);
}
//////////////////
//复数加法
//////////////////
Complex operator + (Complex& c2) {
Complex c;
c.R = R + c2.R; c.I = I + c2.I;
return c;
}
//////////////////
//复数减法
//////////////////
Complex operator - (Complex& c2) {
Complex c;
c.R = R - c2.R; c.I = I - c2.I;
return c;
}
//////////////////
//复数乘法
//////////////////
Complex operator * (Complex& c2) {
Complex c;
double p, q, s;
p = R * c2.R; q = I * c2.I;
s = (R + I) * (c2.R + c2.I);
c.R = p - q; c.I = s - p - q;
return c;
}
//////////////////
//复数除法
//////////////////
Complex operator / (Complex& c2) {
Complex c;
double p, q, s, w;
p = R * c2.R; q = -I * c2.I;
s = (R + I) * (c2.R - c2.I);
w = (c2.R) * (c2.R) + (c2.I) * (c2.I);
if (w + 1.0 != 1.0) {
c.R = (p - q) / w; c.I = (s - p - q) / w;
}
else {
c.R = 1e+300; c.I = 1e+300;
}
return c;
}
//////////////////
//复数乘幂
//////////////////
Complex cpower(int n) {
Complex c;
double r, q;
q = atan2(I, R);
r = sqrt(R * R + I * I);
if (r + 1.0 != 1.0) { r = n * log(r); r = exp(r); }
c.R = r * cos(n * q); c.I = r * sin(n * q);
return c;
}
//////////////////
//复数的n次方根
//////////////////
void croot(int n, Complex* p) {
Complex c;
int k;
double r, q, t;
if (n < 1) return;
q = atan2(I, R);
r = sqrt(R * R + I * I);
if (r + 1.0 != 1.0) { r = (1.0 / n) * log(r); r = exp(r); }
for (k = 0; k < n; k++) {
t = (2.0 * k * 3.1415926 + q) / n;
c.R = r * cos(t); c.I = r * sin(t);
p[k] = c;
}
}
//////////////////
//复数指数
//////////////////
Complex cexp() {
Complex c;
double p;
p = exp(R);
c.R = p * cos(I); c.I = p * sin(I);
return c;
}
//////////////////
//复数对数
//////////////////
Complex clog() {
Complex c;
double p;
p = R * R + I * I;
p = log(sqrt(p));
c.R = p; c.I = atan2(I, R);
return c;
}
//////////////////
//复数正弦
//////////////////
Complex csin() {
Complex c;
double p, q;
p = exp(I); q = exp(-I);
c.R = sin(R) * (p + q) / 2;
c.I = cos(R) * (p - q) / 2;
return c;
}
//////////////////
//复数余弦
//////////////////
Complex ccos() {
Complex c;
double p, q;
p = exp(I); q = exp(-I);
c.R = cos(R) * (p + q) / 2;
c.I = -sin(R) * (p - q) / 2;
return c;
}
};
////////////////////
//构造函数 供C#调用
////////////////////
void* complex_create(double real, double imag)
{
return new Complex(real, imag);
}
////////////////////
//销毁函数 供C#调用
//使用static_cast
//把ptr强制转换成complex类型
////////////////////
void complex_destroy(void* ptr)
{
delete static_cast<Complex*>(ptr);
}
////////////////////
//输出函数 供C#调用
////////////////////
void complex_prt(void* ptr)
{
static_cast<Complex*>(ptr)->prt();
}
////////////////////
//返回复数模 供C#调用
////////////////////
double complex_cfabs(void* ptr)
{
return static_cast<Complex*>(ptr)->cfabs();
}
////////////////////
//返回复数幅角 供C#调用
////////////////////
double complex_angle(void* ptr)
{
return static_cast<Complex*>(ptr)->angle();
}
////////////////////
//复数加法 供C#调用
////////////////////
void* complex_add(void* ptr, void* ptr2)
{
Complex* c1 = static_cast<Complex*>(ptr);
Complex* c2 = static_cast<Complex*>(ptr2);
return new Complex(*c1 + *c2);
}
////////////////////
//复数减法 供C#调用
////////////////////
void* complex_sub(void* ptr, void* ptr2)
{
Complex* c1 = static_cast<Complex*>(ptr);
Complex* c2 = static_cast<Complex*>(ptr2);
return new Complex(*c1 - *c2);
}
////////////////////
//复数乘法 供C#调用
////////////////////
void* complex_mul(void* ptr, void* ptr2)
{
Complex* c1 = static_cast<Complex*>(ptr);
Complex* c2 = static_cast<Complex*>(ptr2);
return new Complex(*c1 * *c2);
}
////////////////////
//复数除法 供C#调用
////////////////////
void* complex_div(void* ptr, void* ptr2)
{
Complex* c1 = static_cast<Complex*>(ptr);
Complex* c2 = static_cast<Complex*>(ptr2);
return new Complex(*c1 / *c2);
}
////////////////////
//复数乘幂 供C#调用
////////////////////
void* complex_cpower(void* ptr, int n)
{
return new Complex(static_cast<Complex*>(ptr)->cpower(n));
}
////////////////////
//复数的n次方根 供C#调用
////////////////////
void complex_croot(void* ptr, int n, void** p)
{
Complex* c = static_cast<Complex*>(ptr);
Complex* roots = new Complex[n];
c->croot(n, roots);
for (int i = 0; i < n; i++) {
p[i] = new Complex(roots[i]);
}
delete[] roots;
}
////////////////////
//复数指数 供C#调用
////////////////////
void* complex_cexp(void* ptr)
{
return new Complex(static_cast<Complex*>(ptr)->cexp());
}
////////////////////
//复数对数 供C#调用
////////////////////
void* complex_clog(void* ptr)
{
return new Complex(static_cast<Complex*>(ptr)->clog());
}
////////////////////
//复数正弦 供C#调用
////////////////////
void* complex_csin(void* ptr)
{
return new Complex(static_cast<Complex*>(ptr)->csin());
}
////////////////////
//复数余弦 供C#调用
////////////////////
void* complex_ccos(void* ptr)
{
return new Complex(static_cast<Complex*>(ptr)->ccos());
}
c#中调用的方法
//C#调用实例
IntPtr c1 = complex_create(1.0, 2.0);
IntPtr c2 = complex_create(3.0, 4.0);
complex_prt(c1);
complex_prt(c2);
double abs1 = complex_cfabs(c1);
double angle1 = complex_angle(c1);
IntPtr sum = complex_add(c1, c2);
complex_prt(sum);
Console.WriteLine();
IntPtr diff = complex_sub(c1, c2);
complex_prt(diff);
Console.WriteLine();
IntPtr prod = complex_mul(c1, c2);
complex_prt(prod);
Console.WriteLine();
IntPtr quot = complex_div(c1, c2);
complex_prt(quot);
Console.WriteLine();
IntPtr power = complex_cpower(c1, 2);
complex_prt(power);
Console.WriteLine();
const int n = 3;
IntPtr[] roots = new IntPtr[n];
complex_croot(c1, n, roots);
for (int i = 0; i < n; i++)
{
complex_prt(roots[i]);
Console.WriteLine();
}
IntPtr exp = complex_cexp(c1);
complex_prt(exp);
Console.WriteLine();
IntPtr log = complex_clog(c1);
complex_prt(log);
Console.WriteLine();
IntPtr sin = complex_csin(c1);
complex_prt(sin);
Console.WriteLine();
IntPtr cos = complex_ccos(c1);
complex_prt(cos);
Console.WriteLine();
complex_destroy(c1);
complex_destroy(c2);
complex_destroy(sum);
complex_destroy(diff);
complex_destroy(prod);
complex_destroy(quot);
complex_destroy(power);
for (int i = 0; i < n; i++)
{
complex_destroy(roots[i]);
}
complex_destroy(exp);
complex_destroy(log);
complex_destroy(sin);
complex_destroy(cos);
我想按照上面的例子 仿写一下 下面的c++ class
class NendToCSharp
{
private:
int N; //复系数多项式次数
Complex *c; //复系数多项式存储空间首地址
public:
NendToCSharp(void);
~NendToCSharp(void);
NendToCSharp(int nn=0, Complex *pp=NULL) //构造函数
{ N = nn; c = pp; }
//实数初始化
double init(double p)
{
p = 0.0;
return(p);
}
//复数初始化
Complex init(Complex p)
{
p = Complex(0.0, 0.0);
return(p);
}
//计算实数的绝对值
double ffabs(double p)
{
double q;
q = fabs(p);
return(q);
}
//计算复数的模
double ffabs(Complex p)
{
double q;
q = p.cfabs();
return(q);
}
template <class T>
int gauss(T *a, T *b, int n)
{
int *js,k,i,j,is,p,q;
double d,t;
T s;
js=new int[n];
for (k=0;k<=n-2;k++) //消元过程
{
d=0.0; //全选主元
for (i=k;i<=n-1;i++)
for (j=k;j<=n-1;j++)
{
t=ffabs(a[i*n+j]);
if (t>d) { d=t; js[k]=j; is=i;}
}
if (d+1.0==1.0) //系数矩阵奇异,求解失败!
{
for (i=0; i<n; i++) b[i] = init(s);
cout <<"系数矩阵奇异,求解失败!\n";
delete[]js;
return 0;
}
if (js[k]!=k) //列交换
{
for (i=0;i<=n-1;i++)
{
p=i*n+k; q=i*n+js[k];
s=a[p]; a[p]=a[q]; a[q]=s;
}
}
if (is!=k) //行交换
{
for (j=k;j<=n-1;j++)
{
p=k*n+j; q=is*n+j;
s=a[p]; a[p]=a[q]; a[q]=s;
}
s=b[k]; b[k]=b[is]; b[is]=s;
}
s=a[k*n+k];
for (j=k+1;j<=n-1;j++) //归一化
{
p=k*n+j; a[p]=a[p]/s;
}
b[k]=b[k]/s;
for (i=k+1;i<=n-1;i++) //消元
{
for (j=k+1;j<=n-1;j++)
{
p=i*n+j;
a[p]=a[p]-a[i*n+k]*a[k*n+j];
}
b[i]=b[i]-a[i*n+k]*b[k];
}
}
s=a[(n-1)*n+n-1];
if (ffabs(s)+1.0==1.0) //系数矩阵奇异,求解失败!
{
for (i=0; i<n; i++) b[i] = init(s);
cout <<"系数矩阵奇异,求解失败!\n";
delete[] js;
return 0;
}
b[n-1]=b[n-1]/s; //回代过程
for (i=n-2;i>=0;i--)
{
s=init(s);
for (j=i+1;j<=n-1;j++) s=s+a[i*n+j]*b[j];
b[i]=b[i]-s;
}
js[n-1]=n-1;
for (k=n-1;k>=0;k--) //恢复
if (js[k]!=k)
{
s=b[k]; b[k]=b[js[k]]; b[js[k]]=s;
}
delete[] js;
return 1;
}
template <class T> //模板声明T为类型参数
int gauss_jordan(T *a, T *b, int n)
{
int *js,k,i,j,is,p,q;
double d,t;
T s;
js=new int[n];
for (k=0;k<=n-1;k++) //消去过程
{
d=0.0; //全选主元
for (i=k;i<=n-1;i++)
for (j=k;j<=n-1;j++)
{
t=ffabs(a[i*n+j]);
if (t>d) { d=t; js[k]=j; is=i;}
}
if (d+1.0==1.0) //系数矩阵奇异,求解失败!
{
cout <<"系数矩阵奇异,求解失败!\n";
for (i=0; i<n; i++) b[i]=init(s);
delete[]js;
return 0;
}
if (js[k]!=k) //列交换
{
for (i=0;i<=n-1;i++)
{
p=i*n+k; q=i*n+js[k];
s=a[p]; a[p]=a[q]; a[q]=s;
}
}
if (is!=k) //行交换
{
for (j=k;j<=n-1;j++)
{
p=k*n+j; q=is*n+j;
s=a[p]; a[p]=a[q]; a[q]=s;
}
s=b[k]; b[k]=b[is]; b[is]=s;
}
s=a[k*n+k];
for (j=k+1;j<=n-1;j++) //归一化
{
p=k*n+j; a[p]=a[p]/s;
}
b[k]=b[k]/s;
for (i=0;i<=n-1;i++) //消元
{
if (i!=k)
{
for (j=k+1;j<=n-1;j++)
{
p=i*n+j;
a[p]=a[p]-a[i*n+k]*a[k*n+j];
}
b[i]=b[i]-a[i*n+k]*b[k];
}
}
}
for (k=n-1;k>=0;k--) //恢复
if (js[k]!=k)
{
s=b[k]; b[k]=b[js[k]]; b[js[k]]=s;
}
delete[] js;
return 1;
}
int trde(double b[], int n, int m, double d[])
{
int k,j;
double s;
if (m!=(3*n-2)) return -2;
for (k=0;k<=n-2;k++)
{
j=3*k; s=b[j];
if (fabs(s)+1.0==1.0) return 0;
b[j+1]=b[j+1]/s;
d[k]=d[k]/s;
b[j+3]=b[j+3]-b[j+2]*b[j+1];
d[k+1]=d[k+1]-b[j+2]*d[k];
}
s=b[3*n-3];
if (fabs(s)+1.0==1.0) return 0;
d[n-1]=d[n-1]/s;
for (k=n-2;k>=0;k--) d[k]=d[k]-b[3*k+1]*d[k+1];
return(2);
}
int band(double b[], double d[], int n, int l, int il, int m)
{
int ls,k,i,j,is,u,v;
double p,t;
if (il!=(2*l+1)) return(-2);
ls=l;
for (k=0;k<=n-2;k++)
{
p=0.0;
for (i=k;i<=ls;i++)
{
t=fabs(b[i*il]);
if (t>p) {p=t; is=i;}
}
if (p+1.0==1.0) return(0);
for (j=0;j<=m-1;j++)
{
u=k*m+j; v=is*m+j;
t=d[u]; d[u]=d[v]; d[v]=t;
}
for (j=0;j<=il-1;j++)
{
u=k*il+j; v=is*il+j;
t=b[u]; b[u]=b[v]; b[v]=t;
}
for (j=0;j<=m-1;j++)
{
u=k*m+j; d[u]=d[u]/b[k*il];
}
for (j=1;j<=il-1;j++)
{
u=k*il+j; b[u]=b[u]/b[k*il];
}
for (i=k+1;i<=ls;i++)
{
t=b[i*il];
for (j=0;j<=m-1;j++)
{
u=i*m+j; v=k*m+j; d[u]=d[u]-t*d[v];
}
for (j=1;j<=il-1;j++)
{
u=i*il+j; v=k*il+j; b[u-1]=b[u]-t*b[v];
}
u=i*il+il-1; b[u]=0.0;
}
if (ls!=(n-1)) ls=ls+1;
}
p=b[(n-1)*il];
if (fabs(p)+1.0==1.0) return(0);
for (j=0;j<=m-1;j++)
{
u=(n-1)*m+j; d[u]=d[u]/p;
}
ls=1;
for (i=n-2;i>=0;i--)
{
for (k=0;k<=m-1;k++)
{
u=i*m+k;
for (j=1;j<=ls;j++)
{
v=i*il+j; is=(i+j)*m+k;
d[u]=d[u]-b[v]*d[is];
}
}
if (ls!=(il-1)) ls=ls+1;
}
return(2);
}
int ldle(double a[], int n, int m, double c[])
{
int i,j,l,k,u,v,w,k1,k2,k3;
double p;
//省略
return(2);
}
int chlk(double a[], int n, int m, double d[])
{
int i,j,k,u,v;
if ((a[0]+1.0==1.0)||(a[0]<0.0)) return(0);
a[0]=sqrt(a[0]);
//省略
return(2);
}
int tlvs(double t[], int n, double b[], double x[])
{
int i,j,k;
double a,beta,q,c,h,*y,*s;
s=new double[n];
y=new double[n];
a=t[0];
//省略
return(1);
}
int seidel(double *a, double *b, int n, double *x, double eps)
{
int i,j,u,v;
double p,t,s,q;
//省略
return 1;
}
void grad(double a[],int n,double b[],double eps,double x[])
{
Matrix mx;
int i,k;
double *p,*r,*s,*q,alpha,beta,d,e;
p=new double[n];
r=new double[n];
s=new double[n];
q=new double[n];
for (i=0; i<=n-1; i++)
{ x[i]=0.0; p[i]=b[i]; r[i]=b[i]; }
i=0;
//省略
delete[] p; delete[] r; delete[] s; delete[] q;
return;
}
int maqr(double a[],int m,int n,double q[])
{
int i,j,k,l,nn,p,jj;
double u,alpha,w,t;
//省略
return 1;
}
int gmqr(double a[], int m, int n, double b[], double q[])
{
int i,j;
double d,*c;
c=new double[n];
i=maqr(a,m,n,q);
if (i==0)
{
delete[] c; return(0);
}
for (i=0; i<=n-1; i++)
{
d=0.0;
for (j=0; j<=m-1; j++) d=d+q[j*m+i]*b[j];
c[i]=d;
}
b[n-1]=c[n-1]/a[n*n-1];
for (i=n-2; i>=0; i--)
{
d=0.0;
for (j=i+1; j<=n-1; j++) d=d+a[i*n+j]*b[j];
b[i]=(c[i]-d)/a[i*n+i];
}
delete[] c; return(1);
}
int gmiv(double a[], int m, int n, double b[], double x[],
double aa[], double eps, double u[], double v[], int ka)
{
//省略
return(1);
}
int bingt(double a[], int n, double b[], double eps, double x[])
{
int i,j,k;
double q, qq;
double *p,*r,*e;
p=new double[n*n];
r=new double[n];
e=new double[n];
k=0;
//省略
delete[] p; delete[] r; delete[] e; return 1;
}
};
需要完整的c++代码 和c# 调用例子