C++写功能代码C#写界面的英汉词典 (九)

2014-11-24 02:49:06 · 作者: · 浏览: 15
'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 {
if(line==counterNum-1)
outfile< else
outfile< }
outfile.close();//关闭文件
char* st = new char[50];
char *a = &biword[0];
strcpy(st,a);
*s=st;
}

这是C#写的界面:

代码如下:


[csharp]
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