Finally, the working environment has been moved to C++

代码下载见:http://download.csdn.net/source/2954182 

前一周里主要是针对标准C++和MFC融合编程做了一些试探性的工作,并将一些前期预处理代码由C#改成了C++。这回我要彻底地摒弃C#了,也可以安心用C++做毕设实验了。

先上一张截图,展示下预处理的部分工作。

总结:VS平台下,字符串类型(string,wstring,char CString)容易引起混乱。这一点在写代码的时候一定要注意。

我采取的方案是:尽量让自己的算法不被VS平台所绑架,即除非用控件显示字字符串,否则不用CString。

这样就需要一个类专门用来处理各种字符串之间的转换。

在此定义为stringprocess类。

该类代码如下:

char ,string ,wstring, Cstring之间的互相转换
#include "StdAfx.h"
#include 
"stringprocess.h"
using namespace std;
stringprocess::stringprocess(
void)
{
}

stringprocess::
~stringprocess(void)
{
}
/************************************************************************/
/* 窄字符串转换成宽字符串                                                                     */
/************************************************************************/
wstring stringprocess:: myMultibyteToWideChar(
string sResult)  

 {  

    
int iWLen=MultiByteToWideChar( CP_ACP, 0, sResult.c_str(), sResult.size(), 00 );// 计算转换后宽字符串的长度。(不包含字符串结束符)  

    wchar_t 
*lpwsz= new wchar_t [iWLen+1];  
    

    MultiByteToWideChar( CP_ACP, 
0, sResult.c_str(), sResult.size(), lpwsz, iWLen ); // 正式转换。  

    lpwsz[iWLen] 
= L'\0';   

   wstring wsResult(lpwsz);  

    delete []lpwsz;  

    
return wsResult;  

 }  

string stringprocess:: myWideCharToMultibyte(wstring wsResult)  

 {  
     
string sResult;  
    
int iLen= WideCharToMultiByte( CP_ACP, NULL, wsResult.c_str(), -1, NULL, 0, NULL, FALSE ); // 计算转换后字符串的长度。(包含字符串结束符)  
     char *lpsz= new char[iLen];  
     WideCharToMultiByte( CP_OEMCP, NULL, wsResult.c_str(), 
-1, lpsz, iLen, NULL, FALSE); // 正式转换。  
     sResult.assign( lpsz, iLen-1 ); // 对string对象进行赋值。  
     delete []lpsz;  
     
return sResult;  
    

   

 } 
CString stringprocess::stringToCString(
string sResult)
{
    CString cstrResult(sResult.c_str());
    
return cstrResult;
}

string stringprocess::CStringTostring(CString cstr)
{
    
return static_cast<LPCSTR>(CStringA(cstr));
}

void stringprocess:: CStringTochar(char* out,CString cstr)
{
    
int nLength = cstr.GetLength();
    
int nBytes = WideCharToMultiByte(CP_ACP,0,cstr,nLength,NULL,0,NULL,NULL);
    
char*  P_char= new char[ nBytes+1];
    memset(P_char,
0,nBytes + 1);
    WideCharToMultiByte(CP_OEMCP, 
0, cstr, nLength, P_char, nBytes, NULL, NULL);
    memcpy(
out,P_char,nBytes+1);

}

void stringprocess::TrimString(string &str,const string val)
{
    str.erase(
0,str.find_first_not_of(val));
    str.erase(str.find_last_not_of(val)
+val.size());
}
string stringprocess:: do_fraction(int value)
{
    ostringstream 
out;
    
//int prec=numeric_limits<int>::digits10; // 18
    
//out.precision(prec);//覆盖默认精度
    out<<value;
    
string str= out.str(); //从流中取出字符串
    str.swap(string(str.c_str()));//删除nul之后的多余字符
    return str;
}



 另外,要保持核心算法(尽管在硕士论文中不可能有什么破天荒的独创性核心算法,姑且这么叫吧)的可移植性。核心算法要自己定义成一个类进行封装,万万不可和MFC框架类混在一起。记住MFC仅用于结果显示。比如,在"搜索"按钮的单击函数中,我们查询数据库,显示结果。我们把查询数据库也看做是核心算法的一部分,因此数据库查询代码不直接出现在按钮的单击函数中。

如下:

代码
void CTabPage1Dlg::OnBnClickedBtnsearch()
{
   stringprocess strp;
   RoughAnalyzer analyzer;
   vector
<string> retword;
   vector
<pair<string,string> >retconcept;
    UpdateData();
   
//MessageBox(m_editstring);
   CString selectcstr;
   CString cstrselectconcept;
   
if (m_editstring.GetLength()==1)
   {
       selectcstr.Format(_T(
"select num, define from liuyuwordlist where characters='%s' and class='ZI'"),m_editstring);
   }
   
else
   {
       selectcstr.Format(_T(
"select num, define from liuyuwordlist where words='%s'"),m_editstring);

   }
   
string  selectstr=strp.CStringTostring(selectcstr);
 
   cstrselectconcept.Format(_T(
"select name,defdesc from concept where name like'%s[_]%%'"),m_editstring);
   
string strselectconcept=strp.CStringTostring(cstrselectconcept);
   analyzer.GetFromDataBaseByWord(retword,selectstr);
   analyzer.GetFromConceptByWord(retconcept,strselectconcept);
    m_ListCtrl.DeleteAllItems();
    m_listCtrl2.DeleteAllItems();
    
int i=0;
    
int j=0;
    
for (vector<string>::iterator it=retword.begin();it!=retword.end();it++)
    {    
        CString cstrtmp
=strp.stringToCString(*it);
        m_ListCtrl.InsertItem(i,m_editstring);
        m_ListCtrl.SetItemText(i,
1,cstrtmp);
        i
++;
    }
    
for (vector<pair<string,string>>::iterator it=retconcept.begin();it!=retconcept.end();it++)
    {    
        CString cstrpart1
=strp.stringToCString(it->first);
        CString cstrpart2
=strp.stringToCString(it->second);
        m_listCtrl2.InsertItem(j,cstrpart1);
        m_listCtrl2.SetItemText(j,
1,cstrpart2);
        
        j
++;
    }




   
   



    
// TODO: Add your control notification handler code here
}
原文地址:https://www.cnblogs.com/finallyliuyu/p/1925730.html