程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++寫功能代碼C#寫界面的英漢詞典

C++寫功能代碼C#寫界面的英漢詞典

編輯:C++入門知識

這是C++寫的功能代碼

這是file.h頭文件,代碼如下:


[cpp] 
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); 

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);
這是CPPDome.cpp源文件,代碼如下:


[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; 

// 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#寫的界面:

代碼如下:


[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 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); 
            } 
        } 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved