这是C++写的功能代码
这是file.h头文件,代码如下:
extern "C" __declspec(dllexport) void __stdcall Add(char **s,char* c);
extern "C" __declspec(dllexport) void __stdcall txtReader(char **s,char* word);
extern "C" __declspec(dllexport) void __stdcall deleteword(char **s,char* word);
这是fun.h的文件及代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ofstream outfile("D:\\text.txt");//定义一个文件输出流对象
void fun(string s)
{
outfile<<s<<endl;
}
这是CPPDome.cpp源文件,代码如下:
// CSharpInvokeCPP.CPPDemo.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "fun.h"
#include "file.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#include <string>
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
string Binary_delete(string a,string b[1000][2],int begin,int end)//折半查找功能函数
{
int k = end;
while(true)
{
if(a==b[k][0])
return "删除成功!";
else if((begin==end-1)&&a!=b[(begin+end)/2][0])
return "单词不存在";
else if(a>b[(begin+end)/2][0])
begin=(begin+end)/2;
else if(a<b[(begin+end)/2][0])
end=(begin+end)/2;
else{
begin = (begin+end)/2;
for(;begin<=k;begin++){
b[begin][0]=b[begin+1][0];b[begin][1]=b[begin+1][1];
}
return "删除成功";
}
}
}
string insertWord(string a,string c,string b[1000][2],int begin,int end,int judge)
{
int i;
if(judge==1){b[end+1][0]=a;b[end+1][1]=c;}
else if(judge==0){
for(int e=end;e>=0;e--){
b[e+1][0]=b[e][0];b[e+1][1]=b[e][1];
}b[0][0]=a;b[0][1]=c;
}else{
for(i=end;i>begin;i--)
{
b[i+1][0] = b[i][0];
b[i+1][1] = b[i][1];
}
b[i+1][0] = a;
b[i+1][1] = c;
}
return "插入成功!";
}
string Binary_search(string a,string b[1000][2],int begin,int end)//折半查找功能函数
{
while(true)
{
if((begin==end-1)&&a!=b[(begin+end)/2][0])
return "no result";
else if(a>b[(begin+end)/2][0])
begin=(begin+end)/2;
else if(a<b[(begin+end)/2][0])
end=(begin+end)/2;
else
return b[(begin+end)/2][1];
}
}
string Binary_add(string a,string c,string b[1000][2],int begin,int end)//折半查找功能函数
{
int k = end;
while(true)
{
if((begin==end-1)&&a!=b[(begin+end)/2][0])
{
if(a<b[0][0])
return insertWord(a,c,b,begin,k,0);
else if(a>b[k][0])
return insertWord(a,c,b,begin,k,1);
else
return insertWord(a,c,b,begin,k,2);
}
else if(a>b[(begin+end)/2][0])
begin=(begin+end)/2;
else if(a<b[(begin+end)/2][0])
end=(begin+end)/2;
else
return "单词已存在,插入失败";
}
}
void __stdcall Add(char **s,char* word)
{
char firstletter = word[0];
int m = 0;
int p;
for(p=0;p<50;p++)
{
if(word[p]=='\0') break;
if(word[p]==' ') m=p;
}
string insertword(word);
string insertwords = insertword.substr(0,m);
string insertchinese = insertword.substr(m+1,p);
string path;
switch(firstletter)//确定要打开的文件的路径
{
case'a':path = "lib/a.txt";break;
case'b':path = "lib/b.txt";break;
case'c':path = "lib/c.txt";break;
case'd':path = "lib/d.txt";break;
case'e':path = "lib/e.txt";break;
case'f':path = "lib/f.txt";break;
case'g':path = "lib/g.txt";break;
case'h':path = "lib/h.txt";break;
case'i':path = "lib/i.txt";break;
case'j':path = "lib/j.txt";break;
case'k':path = "lib/k.txt";break;
case'l':path = "lib/l.txt";break;
case'm':path = "lib/m.txt";break;
case'n':path = "lib/n.txt";break;
case'o':path = "lib/o.txt";break;
case'p':path = "lib/p.txt";break;
case'q':path = "lib/q.txt";break;
case'r':path = "lib/r.txt";break;
case's':path = "lib/s.txt";break;
case't':path = "lib/t.txt";break;
case'u':path = "lib/u.txt";break;
case'v':path = "lib/v.txt";break;
case'w':path = "lib/w.txt";break;
case'x':path = "lib/x.txt";break;
case'y':path = "lib/y.txt";break;
case'z':path = "lib/z.txt";break;
case'A':path = "lib/a.txt";break;
case'B':path = "lib/b.txt";break;
case'C':path = "lib/c.txt";break;
case'D':path = "lib/d.txt";break;
case'E':path = "lib/e.txt";break;
case'F':path = "lib/f.txt";break;
case'G':path = "lib/g.txt";break;
case'H':path = "lib/h.txt";break;
case'I':path = "lib/i.txt";break;
case'J':path = "lib/j.txt";break;
case'K':path = "lib/k.txt";break;
case'L':path = "lib/l.txt";break;
case'M':path = "lib/m.txt";break;
case'N':path = "lib/n.txt";break;
case'O':path = "lib/o.txt";break;
case'P':path = "lib/p.txt";break;
case'Q':path = "lib/q.txt";break;
case'R':path = "lib/r.txt";break;
case'S':path = "lib/s.txt";break;
case'T':path = "lib/t.txt";break;
case'U':path = "lib/u.txt";break;
case'V':path = "lib/v.txt";break;
case'W':path = "lib/w.txt";break;
case'X':path = "lib/x.txt";break;
case'Y':path = "lib/y.txt";break;
case'Z':path = "lib/z.txt";break;
}
ifstream infile(path);//以输入方式打开文件
string words[1000][2];//定义一个二维数组存储信息
char ch[50];//定义一个字符数组用来存储从txt文件中读取的信息
int counterNum;//定义一个计数器
for(counterNum=0;!infile.eof();counterNum++)//从文件内读取数据存到一个string数组中
{
int k=0;
infile.getline(ch,50);
int i;
for(i=0;i<50;i++)
{
if(ch[i]=='\0')
break;
if(ch[i]==' ')
k=i;
}
string judgeword(ch);
if(judgeword.length()!=0){
words[counterNum][0] = judgeword.substr(0,k);
words[counterNum][1] = judgeword.substr(k+1,i);}
}
infile.close();//关闭文件
/*
*此处为二分插入的功能代码
**/
string biword = Binary_add(insertwords,insertchinese,words,0,counterNum-1);
ofstream outfile(path);//以输出方式打开文件
if(biword=="插入成功!") counterNum++;
for(int line=0;line<counterNum;line++)
{
if(line==counterNum-1)
outfile<<words[line][0]<<" "<<words[line][1];//想文件内输入信息
else
outfile<<words[line][0]<<" "<<words[line][1]<<endl;
}
outfile.close();//关闭文件
char* st = new char[50];
char *a = &biword[0];
strcpy(st,a);
*s=st;
}
void __stdcall txtReader(char **s,char* word)
{
char firstletter = word[0];
string insertword(word);
string path;
switch(firstletter)//确定要打开的文件的路径
{
case'a':path = "lib/a.txt";break;
case'b':path = "lib/b.txt";break;
case'c':path = "lib/c.txt";break;
case'd':path = "lib/d.txt";break;
case'e':path = "lib/e.txt";break;
case'f':path = "lib/f.txt";break;
case'g':path = "lib/g.txt";break;
case'h':path = "lib/h.txt";break;
case'i':path = "lib/i.txt";break;
case'j':path = "lib/j.txt";break;
case'k':path = "lib/k.txt";break;
case'l':path = "lib/l.txt";break;
case'm':path = "lib/m.txt";break;
case'n':path = "lib/n.txt";break;
case'o':path = "lib/o.txt";break;
case'p':path = "lib/p.txt";break;
case'q':path = "lib/q.txt";break;
case'r':path = "lib/r.txt";break;
case's':path = "lib/s.txt";break;
case't':path = "lib/t.txt";break;
case'u':path = "lib/u.txt";break;
case'v':path = "lib/v.txt";break;
case'w':path = "lib/w.txt";break;
case'x':path = "lib/x.txt";break;
case'y':path = "lib/y.txt";break;
case'z':path = "lib/z.txt";break;
case'A':path = "lib/a.txt";break;
case'B':path = "lib/b.txt";break;
case'C':path = "lib/c.txt";break;
case'D':path = "lib/d.txt";break;
case'E':path = "lib/e.txt";break;
case'F':path = "lib/f.txt";break;
case'G':path = "lib/g.txt";break;
case'H':path = "lib/h.txt";break;
case'I':path = "lib/i.txt";break;
case'J':path = "lib/j.txt";break;
case'K':path = "lib/k.txt";break;
case'L':path = "lib/l.txt";break;
case'M':path = "lib/m.txt";break;
case'N':path = "lib/n.txt";break;
case'O':path = "lib/o.txt";break;
case'P':path = "lib/p.txt";break;
case'Q':path = "lib/q.txt";break;
case'R':path = "lib/r.txt";break;
case'S':path = "lib/s.txt";break;
case'T':path = "lib/t.txt";break;
case'U':path = "lib/u.txt";break;
case'V':path = "lib/v.txt";break;
case'W':path = "lib/w.txt";break;
case'X':path = "lib/x.txt";break;
case'Y':path = "lib/y.txt";break;
case'Z':path = "lib/z.txt";break;
}
ifstream infile(path);//以输入方式打开文件
string words[1000][2];//定义一个二维数组存储信息
char ch[50];//定义一个字符数组用来存储从txt文件中读取的信息
int counterNum=0;//定义一个计数器
for(counterNum=0;!infile.eof();counterNum++)//从文件内读取数据存到一个string数组中
{
int k=0;
infile.getline(ch,50);
int i;
for(i=0;i<50;i++)
{
if(ch[i]=='\0')
break;
if(ch[i]==' ')
k=i;
}
string judgeword(ch);
if(judgeword.length()!=0){
words[counterNum][0] = judgeword.substr(0,k);
words[counterNum][1] = judgeword.substr(k+1,i);}
}
infile.close();//关闭文件
/*
*此处为二分查找的功能代码
**/
string biword = Binary_search(insertword,words,0,counterNum);
char* st = new char[50];
char *a = &biword[0];
strcpy(st,a);
*s=st;
}
void __stdcall deleteword(char **s,char *word)
{
char firstletter = word[0];
int m = 0;
int p;
for(p=0;p<50;p++)
{
if(word[p]=='\0') break;
if(word[p]==' ') m=p;
}
string insertword(word);
string path;
switch(firstletter)//确定要打开的文件的路径
{
case'a':path = "lib/a.txt";break;
case'b':path = "lib/b.txt";break;
case'c':path = "lib/c.txt";break;
case'd':path = "lib/d.txt";break;
case'e':path = "lib/e.txt";break;
case'f':path = "lib/f.txt";break;
case'g':path = "lib/g.txt";break;
case'h':path = "lib/h.txt";break;
case'i':path = "lib/i.txt";break;
case'j':path = "lib/j.txt";break;
case'k':path = "lib/k.txt";break;
case'l':path = "lib/l.txt";break;
case'm':path = "lib/m.txt";break;
case'n':path = "lib/n.txt";break;
case'o':path = "lib/o.txt";break;
case'p':path = "lib/p.txt";break;
case'q':path = "lib/q.txt";break;
case'r':path = "lib/r.txt";break;
case's':path = "lib/s.txt";break;
case't':path = "lib/t.txt";break;
case'u':path = "lib/u.txt";break;
case'v':path = "lib/v.txt";break;
case'w':path = "lib/w.txt";break;
case'x':path = "lib/x.txt";break;
case'y':path = "lib/y.txt";break;
case'z':path = "lib/z.txt";break;
case'A':path = "lib/a.txt";break;
case'B':path = "lib/b.txt";break;
case'C':path = "lib/c.txt";break;
case'D':path = "lib/d.txt";break;
case'E':path = "lib/e.txt";break;
case'F':path = "lib/f.txt";break;
case'G':path = "lib/g.txt";break;
case'H':path = "lib/h.txt";break;
case'I':path = "lib/i.txt";break;
case'J':path = "lib/j.txt";break;
case'K':path = "lib/k.txt";break;
case'L':path = "lib/l.txt";break;
case'M':path = "lib/m.txt";break;
case'N':path = "lib/n.txt";break;
case'O':path = "lib/o.txt";break;
case'P':path = "lib/p.txt";break;
case'Q':path = "lib/q.txt";break;
case'R':path = "lib/r.txt";break;
case'S':path = "lib/s.txt";break;
case'T':path = "lib/t.txt";break;
case'U':path = "lib/u.txt";break;
case'V':path = "lib/v.txt";break;
case'W':path = "lib/w.txt";break;
case'X':path = "lib/x.txt";break;
case'Y':path = "lib/y.txt";break;
case'Z':path = "lib/z.txt";break;
}
ifstream infile(path);//以输入方式打开文件
string words[1000][2];//定义一个二维数组存储信息
char ch[50];//定义一个字符数组用来存储从txt文件中读取的信息
int counterNum;//定义一个计数器
for(counterNum=0;!infile.eof();counterNum++)//从文件内读取数据存到一个string数组中
{
int k=0;
infile.getline(ch,50);
int i;
for(i=0;i<50;i++)
{
if(ch[i]=='\0')
break;
if(ch[i]==' ')
k=i;
}
string judgeword(ch);
if(judgeword.length()!=0){
words[counterNum][0] = judgeword.substr(0,k);
words[counterNum][1] = judgeword.substr(k+1,i);}
}
infile.close();//关闭文件
/*
*此处为二分查找并删除的功能代码
**/
string biword = Binary_delete(insertword,words,0,counterNum-1);
ofstream outfile(path);//以输出方式打开文件
if(biword=="删除成功") counterNum--;
if(biword=="删除成功!") counterNum=counterNum-1;
for(int line=0;line<counterNum;line++)
{
if(line==counterNum-1)
outfile<<words[line][0]<<" "<<words[line][1];//向文件内输入信息
else
outfile<<words[line][0]<<" "<<words[line][1]<<endl;
}
outfile.close();//关闭文件
char* st = new char[50];
char *a = &biword[0];
strcpy(st,a);
*s=st;
}
这是C#写的界面:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace InterFace
{
public class CPPDLL
{
[System.Runtime.InteropServices.DllImport("CSharpInvokeCPP.CPPDemo.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Ansi, EntryPoint = "Add")]
public static extern void Add(ref IntPtr s, string y);
[System.Runtime.InteropServices.DllImport("CSharpInvokeCPP.CPPDemo.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Ansi, EntryPoint = "txtReader")]
public static extern void txtReader(ref IntPtr s, string word);
[System.Runtime.InteropServices.DllImport("CSharpInvokeCPP.CPPDemo.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = System.Runtime.InteropServices.CharSet.Ansi, EntryPoint = "deleteword")]
public static extern void deleteword(ref IntPtr s, string word);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//添加
{
try
{
string word = textBox1.Text;
string wordCh = textBox3.Text;
if (word.Length == 0 || wordCh.Length == 0) MessageBox.Show("输入的中文和英文都不能是空!");
else
{
word = word + " " + wordCh;
IntPtr s = Marshal.StringToHGlobalAnsi("");
CPPDLL.Add(ref s, word);
string cstr = Marshal.PtrToStringAnsi(s);//得到值
textBox2.Text = cstr;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)//删除
{
try
{
string word = textBox1.Text;
if (word.Length == 0) MessageBox.Show("输入的英文不能是空!");
else
{
IntPtr s = Marshal.StringToHGlobalAnsi("");
CPPDLL.deleteword(ref s, word);
string cstr = Marshal.PtrToStringAnsi(s);//得到值
textBox2.Text = cstr;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)//查找
{
try
{
string word = textBox1.Text;
if (textBox1.Text.Length == 0) MessageBox.Show("输入内容不能为空!");
else
{
IntPtr s = Marshal.StringToHGlobalAnsi("");
CPPDLL.txtReader(ref s, word);
string cstr = Marshal.PtrToStringAnsi(s);//得到值
textBox2.Text = cstr;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}