易截截图软件、单文件、免安装、纯绿色、仅160KB

c编写dll供c#调用

1.新建dll工程,在函数前面增加 extern "C" __declspec(dllexport) double __stdcall
double 为函数返回值类型
// log.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define PI 3.14159
double AverageRandom(double min,double max)//产生(min,max)之间均匀分布的随机数
{
int MINnteger = (int)(min*10000);
int MAXnteger = (int)(max*10000);
int aa=rand();
int bb=rand();
int randInteger = aa*bb;
int diffInteger = MAXnteger - MINnteger;
int resultInteger = randInteger % diffInteger + MINnteger;
return resultInteger/10000.0;
}
double LogNormal(double x,double miu,double sigma) //对数正态分布概率密度函数
{
return 1.0/(x*sqrt(2*PI)*sigma) * exp(-1*(log(x)-miu)*(log(x)-miu)/(2*sigma*sigma));
}
extern "C" __declspec(dllexport) double __stdcall Random_LogNormal(double miu,double sigma,double min,double max)//产生对数正态分布随机数
{

double x;
double dScope;
double y;
do
{
x = AverageRandom(min,max);
y = LogNormal(x, miu, sigma);
dScope = AverageRandom(0, LogNormal(miu,miu,sigma));
}while( dScope > y);
return x;
}
2.C#中代码
  //通过DllImport引用log.dll类。Random_LogNormal来自于log.dll类
    [DllImport("log.dll", EntryPoint = "Random_LogNormal")]
    public static extern double Random_LogNormal(double miu, double sigma, double min, double max);
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
class Program
{
///****************************************************
/// 产生N=100个在(0,50)区间内满足对数正态分布的随机数
///*****************************************************/
const int N = 100;
const int MAX = 30;
const double MIN = 0.5;


相关文档:

extern "C" 阅读笔记 zz

发信人: RoachCock (反动学术权威), 信区: CPlusPlus
标  题: extern "C" 阅读笔记
发信站: 水木社区 (Fri Mar  7 00:22:47 2008), 站内
本以为很简单,仔细阅读了一下 C++ 标准,发现内容还不少。总结了一下。
要点:
函数类型,函数名,变量名具有语言链接性,language linkage。
语言链接性可能会影响到 ......

C 标准库 函数 源代码的实现 和 分析

//库函数实现
char *strcat (char *dst, const char *src)
{
  char *p = dst;
  while (*p)
    p++;
  while ((*p++ = *src++))
    ;
  return dst;
}
char *strncat (char *s1, const char *s2, long unsigned int n)
{
  char *dest = s1;
  ......

我的C实践(7):位计数

  位计数就是对一个数中具有某些特征的位进行计数。看下面实现:
/* bitscount.c:位计数 */
/* 计算x中1位的数目:方案1,采用分治策略 */
inline int pop(unsigned x){
/* 对每个2位字段,先析出其右端的1位,再析出其左端的1位,然后让这两个位相加 */
x=(x & 0x55555555)+((x>>1) & 0x555555 ......

我的C实践(8):字搜索

  字搜索就搜索一个数中具有某些特征的位。实现如下:
/* wsearch.c:字搜索 */
/* 从左边寻找第一个0字节:第0(1,2,3)个字节是0时,返回0(1,2,3),否则返回4 */
int zbytel(unsigned x){
if((x>>24)==0) return 0;
else if((x & 0x00ff0000)==0) return 1;
else if((x & 0x0000ff00)==0) r ......

C字符串反转算法

一个很简洁的算法:
void Reverse(char s[])
{
    for(int i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
        char c = s[i];
        s[i] = s[j];
        s[j] = c;
 & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号