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

Source: Internet
Author: User

CodeFor download, see:Http://download.csdn.net/source/2954182

In the previous week, we made some tentative work on standard C ++ and MFC integrated programming, and changed some pre-processing code from C # To C ++. This time I want to abandon C # completely, and I can use C ++ to complete the experiment.

First, go to the previous one to show some of the preprocessing work.

Conclusion: string type (string, wstring, char cstring) in vsplatform is prone to confusion. Note This when writing code.

My solution is: try to make your ownAlgorithmDo not be kidnapped by vsplatform, that is, do not use cstring unless the control is used to display the character string.

In this way, a class is needed to process the conversion between various strings.

This is defined as the stringprocess class.

The Code is as follows:

 

Conversion between char, String, wstring, and cstring

# Include " Stdafx. h "
# Include " Stringprocess. h "
Using   Namespace STD;
Stringprocess: stringprocess ( Void )
{
}

Stringprocess :: ~ Stringprocess ( Void )
{
}
/* **************************************** ****************************** */
/* Convert a narrow string to a wide string */
/* **************************************** ****************************** */
Wstring stringprocess: mymultibytetowidechar ( String Sresult)

{

Int Iwlen = Multibytetowidechar (cp_acp, 0 , Sresult. c_str (), sresult. Size (), 0 , 0 ); // Calculate the length of the converted string. (Does not contain string Terminator)

Wchar_t * Lpwsz =   New Wchar_t [iwlen + 1 ];

Multibytetowidechar (cp_acp,0, Sresult. c_str (), sresult. Size (), lpwsz, iwlen );//Formal conversion.

Lpwsz [iwlen]=L'\ 0';

Wstring wsresult (lpwsz );

Delete [] lpwsz;

ReturnWsresult;

}

StringStringprocess: mywidechartomultibyte (wstring wsresult)

{
String Sresult;
Int Ilen = Widechartomultibyte (cp_acp, null, wsresult. c_str (), - 1 , Null, 0 , Null, false ); // Calculates the length of the converted string. (Including string Terminator)
Char   * Lpsz =   New   Char [Ilen];
Widechartomultibyte (cp_oemcp, null, wsresult. c_str (), - 1 , Lpsz, ilen, null, false ); // Formal conversion.
Sresult. Assign (lpsz, ilen - 1 ); // Assign values to the string object.
Delete [] lpsz;
Return Sresult;

}
Cstring stringprocess: stringtocstring (StringSresult)
{
Cstring cstrresult (sresult. c_str ());
ReturnCstrresult;
}

StringStringprocess: cstringtostring (cstring CSTR)
{
ReturnStatic_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 ); // Override default precision
Out < Value;
String Str =   Out . STR (); // Extract string from stream
Str. Swap ( String (Str. c_str ())); // Remove extra characters After NUL
Return STR;
}

 

 

 

In addition, we need to maintain the portability of Core algorithms (although it is impossible to have any original Core algorithms in my master's thesis. The core algorithm should be defined as a class for encapsulation. Remember that MFC is only used for result display. For example, in the "Search" button, click the function to query the database and display the result. We also regard database query as part of the core algorithm, so the database query Code does not appear directly in the click function of the button.

As follows:

 

Code

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
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.