安装mono c#开发环境:
apt-get update
apt-get install mono-devel
.so文件生成:
g++ -fPIC -shared -o libmathlib52.so mathlib52.cpp
root@ubuntu:/home/cpptest/cstest# mcs mean.cs
root@ubuntu:/home/cpptest/cstest# mono mean.exe
avg=3,avg1=3,mode=2,stdDev=1.30930734141595,stdDev1=1.30930734141595
avg=3,avg1=3,mode=2,stdDev=1.30930734141595,stdDev1=1.30930734141595
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
public class RefComm
{
[DllImport("libmathlib52.so", EntryPoint="mean")]
public static extern double mean(double[] a,int n);// 平均数
// 计算由num个浮点数组成的数组的平均值
private static double Mean(IEnumerable<double> values)
{
int num=values.Count();
double avg=0;
foreach (double v in values)
{
avg+=v;
}
avg/=num;
return avg;
}
// 返回样本的众数值,如果众数超过一个,则返回最前面的一个
static T find_mode<T>(T[] a)
{
int num=a.Length;
T md=a[0],oldmode=a[0];
int count,oldcount=0;
for(int t=0;t<num;t++)
{
md=a[t];
count=1;
for(int w=t+1;w<num;w++)
if(md.Equals(a[w]))//此处不能用==
count++;//准众数值
if(count>oldcount)
{
oldmode=md;
oldcount=count;
}
}
return oldmode;
}
private static double StdDev(IEnumerable<double> values)
{
double ret = 0;
if (values.Count() > 0)
{
// 计算平均数
double avg =