首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

C++实用技巧十四

2008-12-19 
解决VC++程序国际化的类,解决乱码问题

    #include "stdafx.h"

  #include "global.hpp"

  // Description: generate an error message

  int Error(long err, char *fmt, ...)

  {

  static char buf[256];

  static char buf2[256];

  va_list args;

  va_start(args, fmt);

  vsprintf(buf, fmt, args);

  sprintf(buf2, "Error: %s (HRESULT= 0x%x)", buf, err);

  va_end(args);

  AfxMessageBox(buf2);

  TRACE2("Error: HRESULT: 0x%x, %s\n", err, buf);

  return err;

  }

  // Description: returns the next complete word found starting at string

  // index sIdx

  int getWordFrom( CString &instr, int &sIdx, CString &word )

  {

  int len = instr.GetLength();

  word = "";

  if ( sIdx == len ) return FALSE;

  for ( ; sIdx < len; sIdx++) { // find first nonspace

  if (instr[sIdx] != ’ ’) break;

  }

  for (int e=sIdx; e < len; e++) { // find the first space

  if (instr[e] == ’ ’) break;

  }

  if (sIdx == len) return FALSE;

  word = instr.Mid(sIdx, esIdx);

  sIdx = e;

  return TRUE;

  }

  // Description: returns the nth word in instr. The word is returned in

  // word.

  int words( CString &instr )

  {

  int count = 0;

  int idx = 0;

  CString word("");

  while (1) {

  if ( ! getWordFrom( instr, idx, word ) ) break;

  count++;

  }

  return count;

  }

  // Description: returns the nth word in instr. The word is returned in

  // word.

  int word( CString &instr, int n, CString &word )

  {

  int idx = 0;

  word = "";

  for (int i = 1; i <= n; i++) {

  if ( ! getWordFrom( instr, idx, word ) ) break;

  }

  return (word != "");

  }

  // Description: returns a series of words in instr. The words are

  // returned in word.

  // Parameters: sn = starting word index (starts at 1)

  // nw = # of words to retrieve (1 means remaining words)

  int subword( CString &instr, int sn, int nw, CString &substr )

  {

  substr = "";

  if (0 == nw) return TRUE; // OK, you asked for 0 words, you got 0 words

  if (sn < 1) return FALSE; // bad parameter, word index starts at 1

  int idx = 0;

  CString word;

  // first get to the first wanted word

  for (int i = 1; i < sn; i++) {

  if ( ! getWordFrom( instr, idx, word ) ) break;

  }

  // get all the words needed

  i = 0;

  while (1) {

  if ( ! getWordFrom( instr, idx, word ) ) break;

  if (i > 0) substr += " ";

  substr += word;

  if (++i == nw) break;

  }

  return (substr != "");

  }

  // Description: returns the index or the word that matches the character */

  // position in the string */

  int wordIdxFromCharIdx( CString &instr, int cIdx)

  {

  int idx = 0;

  int wIdx = 0;

  CString word;

  while (1) {

  if ( ! getWordFrom( instr, idx, word ) ) break;

  wIdx++;

  if ( cIdx < idx ) return wIdx;

  }

  return 0;

  }

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.net/exam/

热点排行