现在的位置: 首页 > 综合 > 正文

一些常用算法实现

2013年06月24日 ⁄ 综合 ⁄ 共 7018字 ⁄ 字号 评论关闭

CString newGUID()
{
 GUID guid;
 ::CoCreateGuid(&guid);  //生成guid
 CString str;
 str.Format(_T("%08X%04X%04x%02X%02X%02X%02X%02X%02X%02X%02X"),guid.Data1,guid.Data2,guid.Data3,
  guid.Data4[0],guid.Data4[1],guid.Data4[2],guid.Data4[3],guid.Data4[4],guid.Data4[5],guid.Data4[6],guid.Data4[7]);
 return str;
}

CString GetDateStr2(CTime t)
{
 CString re;
 re.Format(_T("%.4d-%.2d-%.2d"),t.GetYear(),t.GetMonth(),t.GetDay());
 return re;
}
//获取当前日期的字符串
CString GetDateStr()
{
 CTime t = CTime::GetCurrentTime();
 return GetDateStr2(t);
}

//判断是否数字,这里指不包含负号的数字
int IsNum(const CString& str)
{
 if (str.IsEmpty())
  return - 1;
 int nDot = 0;
 //数值只能是0到9及小数点组成
 for (int i = 0; i < str.GetLength(); i++)
 {
  char ch = str.GetAt(i);
  if ('.' == ch)//小数点
  {
   nDot++;
   continue;
  }
  if (ch >= '0' && ch <= '9')//数字
   continue;
  return - 2; //非法字符
 }
 if (nDot > 1)
  return - 3;//小数点多于两个
 else if (0 == nDot)
  return 1;//整数
 else if (1 == nDot)
  return 0;//浮点数
 return - 1000; //未知错误
}
//包含符号的数字
int IsSignNum(const CString& str)
{
 if (str.IsEmpty())
  return - 1;
 int nDot = 0;
 //数值只能是0到9及小数点组成
 for (int i = 0; i < str.GetLength(); i++)
 {
  char ch = str.GetAt(i);
  if ('.' == ch)//小数点
  {
   nDot++;
   continue;
  }
  if (ch >= '0' && ch <= '9')//数字
   continue;
  else if (ch == '-'&&i==0)
   continue;
  return - 2; //非法字符
 }
 if (nDot > 1)
  return - 3;//小数点多于两个
 else if (0 == nDot)
  return 1;//整数
 else if (1 == nDot)
  return 0;//浮点数
 return - 1000; //未知错误
}

//判断是否数字
bool IsNumber( LPCTSTR pszText )
{

 for( int i = 0; i < lstrlen( pszText ); i++ )
  if( !_istdigit( pszText[ i ] ) )
   return false;

 return true;
}

//判断字符串是否英文
bool IsEnglish(const CString str)
{
 int   nIndex;  
 int   nLen   =   str.GetLength();  
 bool re = true;
 for(nIndex=0;nIndex<nLen;nIndex++)
 {
  if(str.GetAt(nIndex)>127)
  {
   re = false;
   break;
  }
 }
 return   re;  

}

//VC中判断是日期的方法
bool IsDate( LPCTSTR pszText )
{
 // format should be 99/99/9999.

 if( lstrlen( pszText ) != 10 )
  return false;

 return _istdigit( pszText[ 0 ] )
  && _istdigit( pszText[ 1 ] )
  && pszText[ 2 ] == _T('/')
  && _istdigit( pszText[ 3 ] )
  && _istdigit( pszText[ 4 ] )
  && pszText[ 5 ] == _T('/')
  && _istdigit( pszText[ 6 ] )
  && _istdigit( pszText[ 7 ] )
  && _istdigit( pszText[ 8 ] )
  && _istdigit( pszText[ 9 ] );
}
//计算正整数的位数
int CalIntBit(unsigned Input)
{
 int count = 1;
 while(Input/10 != 0)
 {
  count++;
  Input /= 10;
 }
 return count;
}

CString ZuoBiaoZhuanHuan(const double& Input)
{
 CString str;
 int du,fen,miao;
 du = (int)Input;
 double tmp = (Input - du)*60;
 fen = (int)tmp;
 tmp = (tmp - fen)*60;
 miao = (int)tmp;
 if (tmp - miao>0.5)
 {
  miao+=1;             //四舍五入
 }
 CString duStr(_T("°")),fenStr(_T("′")),miaoStr(_T("″"));
 str.Format(_T("%d%s%d%s%d%s"),du,duStr,fen,fenStr,miao,miaoStr);
 return str;
}

//输入范围
bool Scope(const CString& Input)
{
 int fenge=0;
 for (int i = 0;i<Input.GetLength();i++)
 {
  if (Input.GetAt(i)=='-')
  {
   fenge++;               //'-'号的数目
  }
 }
 if (fenge>1||fenge == 0)           //输入的字符串含有超过1个‘-’,或者没有这样的分隔符返回false
 {
  return false;
 }
 else if (fenge == 1)
 {
  int index = Input.Find('-');
  CString Min,Max;
  Min = Input.Mid(0,index);
  Max = Input.Mid(index+1,Input.GetLength()-index-1);
  if (IsNum(Min)!=1&&IsNum(Min)!=0)
  {
   return false;                       //'-'前的字符串不是数值
  }
  if (IsNum(Max)!=0&&IsNum(Max)!=1)
  {
   return false;                  //'-'后的字符串不是数值
  }
  if (cstod(Min)>cstod(Max))
  {
   return false;
  }
 }

 return true;
}

double GetMax(const CString& Input)  
{
 CString max;
 int index = Input.Find('-');
 max = Input.Mid(index+1,Input.GetLength()-index-1);
 return cstod(max);
}

double GetMin(const CString& Input)
{
 CString min;
 int index = Input.Find('-');
 min = Input.Mid(0,index);
 return cstod(min);
}

void SetSipBnStatus(INT show);
//全屏
void FullScreen(CWnd* pWnd,BOOL _bMove)
{
 HWND hBar = ::SHFindMenuBar(pWnd->m_hWnd);
 ::CommandBar_Show( hBar, FALSE );

 pWnd->SetForegroundWindow();
 ::SHFullScreen( pWnd->m_hWnd, SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON | SHFS_HIDESTARTICON );
 RECT cliRect;
 pWnd->GetClientRect(&cliRect);

 if ( _bMove )
 {
  //cliRect.top -= 26;
  cliRect.bottom += 26*heightScale;
  pWnd->MoveWindow(&cliRect);
 }

 //去掉软键盘按钮
 SetSipBnStatus(SW_HIDE);
}//end of FullScreen
//退出全屏
void ExitFullScreen(CWnd* pWnd,BOOL _bMove)
{
 HWND hBar = ::SHFindMenuBar(pWnd->m_hWnd);
 ::CommandBar_Show( hBar, TRUE );

 SetSipBnStatus(SW_SHOW);

 SHFullScreen(pWnd->m_hWnd,SHFS_SHOWTASKBAR|SHFS_SHOWSIPBUTTON|SHFS_SHOWSTARTICON);
 HWND hTask = ::FindWindow(TEXT("HHTaskBar"),NULL);
 ::ShowWindow(hTask,SW_SHOW);

 RECT cliRect;
 pWnd->GetClientRect(&cliRect);

 if ( _bMove )
 {
  //cliRect.top -= 26;
  cliRect.bottom -= 26*heightScale;
  pWnd->MoveWindow(&cliRect);
 }
}

//退出上面的全屏,下面的输入法按钮不显示
void ExitFullScreenHideSip(CWnd* pWnd,BOOL _bMove)
{
 HWND hBar = ::SHFindMenuBar(pWnd->m_hWnd);
 ::CommandBar_Show( hBar, FALSE );

 SHFullScreen(pWnd->m_hWnd,SHFS_SHOWTASKBAR|SHFS_SHOWSTARTICON);
 HWND hTask = ::FindWindow(TEXT("HHTaskBar"),NULL);
 ::ShowWindow(hTask,SW_SHOW);

 RECT cliRect;
 pWnd->GetClientRect(&cliRect);

 if ( _bMove )
 {
  //cliRect.top -= 26;
  cliRect.bottom -= 26*heightScale;
  pWnd->MoveWindow(&cliRect);
 }
 
}

//查找某一个字符在字符串中的最后索引
int LastIndexOf(const CString &str,const char c)
{                                    
 int i = str.GetLength()-1;
 while (i >= 0)    //查找字符串中的'_'字符
 {
  if (str.GetAt(i) == c)
   break;
  i--;
 }
 return i;
}
//判断后数是否整除前数
#define   NUM_ZERO   1.0e-10  
bool Divisibility(const double dividend,const double divisor)
{
 bool re = false;
 if(dividend < divisor)   //被除数小于除数
  return re;
 double result = dividend/divisor;
 result = result - (int)result;           //获取小数部分

 if(fabs(result)<=NUM_ZERO)      //为零
 {
  re = true;
 }
 return re;
}
//获取当前系统的时间,返回yyyy-MM-dd HH:MM:SS字符串
CString GetCurTime()
{
 CTime time = CTime::GetCurrentTime();
 CString curTime;
 curTime.Format(_T("%.4d-%.2d-%.2d %.2d:%.2d:%.2d"),time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
 return curTime;
}
//设置软键盘的显示状态
void SetSipBnStatus(INT show)
{
 HWND hWndSipButton = ::FindWindow(TEXT("MS_SIPBUTTON"), NULL);
 if (NULL != hWndSipButton)
 {
  ::ShowWindow(hWndSipButton, show);                           
 }
}

bool ExistFile(const CString &fileName)
{
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind;
 hFind = FindFirstFile(fileName,&FindFileData);
 if (hFind != INVALID_HANDLE_VALUE)
 {
  FindClose(hFind);
  return true;
 }
 else
 {
  FindClose(hFind);
  return false;
 }

}

//分离字符串
//strContent:准备分离的字符串,例如:a;b;c;d
//arString:分离后的数组
//nCount:数组大小
//cToken:分隔符
void Splite(CString strContent, CArray<CString,CString> &arrString, int &nCount, const char &cToken)
{
 if (strContent.Trim() == _T(""))
  return;

 int nBegin;
 int nEnd;
 int conLength = strContent.GetLength();
 nBegin = 0;
 if (strContent.GetAt(conLength-1)!=cToken)
  strContent += cToken;
 nEnd   = strContent.Find(cToken, 0);
 nCount = 0;

 while (nEnd >= 0)
 {
  arrString.Add(strContent.Mid(nBegin, nEnd - nBegin));
  nCount++;
  nBegin = nEnd + 1;
  if (nBegin >= conLength)
   break;
  nEnd = strContent.Find(cToken, nBegin);
 }
}

bool StrInArray(const CString &str,const CArray<CString,CString> &arrStr)
{
 int arrCount = arrStr.GetCount();

 for (int i=0;i<=arrCount-1;i++)
 {
  if (arrStr.GetAt(i) == str)
   return true;
 }
 return false;
}
//判断一个字符串是否在另一个字符串中
bool StrInString(const CString &beContain,const CString &strContent)
{
 if (strContent == _T("")||beContain == _T(""))
  return false;
 if (beContain.GetLength()>strContent.GetLength())
  return false;
 int index = strContent.Find(beContain);
 if (index >=0)
  return true;
 else
  return false;
}
//根据屏幕规格加载位图
void LoadBmp(CBitmap* pbitmap,UINT nID16,UINT nID32)
{
 ASSERT(pbitmap);

 if (heightScale>1.5||widthScale>1.5)
 {
  pbitmap->LoadBitmapW(nID32);
 }
 else
 {
  pbitmap->LoadBitmapW(nID16);
 }
}

【上篇】
【下篇】

抱歉!评论已关闭.