一些常用演算法實現

來源:互聯網
上載者:User

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

//退出上面的全屏,下面的IME按鈕不顯示
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);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.