'Programming'에 해당되는 글 4건

  1. 2007년 04월 10일 STL : 실시간에 정렬 기준 정의하기<string, string> (2) - 소류
  2. 2007년 04월 04일 String Conversions - 소류
  3. 2007년 04월 02일 Const - 소류
  4. 2006년 07월 20일 프로그래머들의 속담 (2) - 소류

class RuntimeStringCmp {
public:
 // 비교 기준을 위한 상수
 enum cmp_mode {normal, nocase};
 
private:
 // 실제 비교 모드
 const cmp_mode mode;
 
 // 대/소문자를 구별하지 않고 비교하기 위한 보조 함수
 static bool nocase_compare(char c1, char c2)
 {
  return toupper((c1) < toupper(c2));
 }

public:
 // 생성자: 비교 기준으로 초기화된다.
 RuntimeStringCmp(cmp_mode m=normal) : mode(m) {
 }

 // 비교
 bool operator() (const std::string& s1, const std::string& s2) const {
  if(mode == normal) {
   return s1<s2;
  }
  else {
   return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), nocase_compare);
  }
 }
};

크리에이티브 커먼즈 라이센스
Creative Commons License
2007년 04월 10일 15시 57분 2007년 04월 10일 15시 57분

String Conversions

String to Hex

sscanf(string, %04X, &your_word16); // where string = your string and
                                                     // 04 = length of your string and X = hex

Hex to CString

CString Str; unsigned char Write_Buff[1]; Write_Buff[0] = 0x01; Str.Format("0x0%x",Write_Buff[0]);

COleVariant to CString

CString strTemp; COleVariant Var; Var = "FirstName"; strTemp = Var.bstrVal; AfxMessageBox(strTemp);

CString to char pointer

CString MyString = "ABCDEF"; char * szMyString = (char *) (LPCTSTR) MyString;
char *pBuffer = new char[1024]; CString strBuf = "Test"; pBuffer = strBuf.GetBuffer(sizeof(pBuffer));

char pointer to CString

char * mystring = "12345"; CString string = mystring;

Double to CString including the fractional part

CString strValue,strInt, strDecimal; int decimal,sign; double dValue = 4.125; strValue = _fcvt(dValue,6,&decimal,&sign); // Now decimal contains 1 because there is  // only one digit before the . strInt = strValue.Left(decimal); // strInt contains 4 strDecimal = strValue.Mid(decimal); // strDecimal contains 125 CString strFinalVal; strFinalVal.Format("%s.%s",strInt,strDecimal); // strFinalVal contains 4.125

Double To CString

CString strValue; int decimal,sign; double dValue = 123456789101112; strValue = _ecvt(dValue,15,&decimal,&sign);

CString To Double

strValue = "121110987654321"; dValue = atof(strValue);

CString to LPCSTR

CString str1 = _T("My String"); int nLen = str1.GetLength(); LPCSTR lpszBuf = str1.GetBuffer(nLen); // here do something with lpszBuf........... str1.ReleaseBuffer();

CString to LPSTR

CString str = _T("My String"); int nLen = str.GetLength(); LPTSTR lpszBuf = str.GetBuffer(nLen); // here do something with lpszBuf........... str.ReleaseBuffer();

CString to WCHAR*

CString str = "A string here" ; LPWSTR lpszW = new WCHAR[255]; LPTSTR lpStr = str.GetBuffer( str.GetLength() ); int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL); MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen); AFunctionUsesWCHAR( lpszW ); delete[] lpszW;

LPTSTR to LPWSTR

int nLen = MultiByteToWideChar(CP_ACP, 0, lptStr, -1, NULL, NULL); MultiByteToWideChar(CP_ACP, 0, lptStr, -1, lpwStr, nLen);

string to BSTR

string ss = "Girish"; BSTR _bstr_home = A2BSTR(ss.c_str());

CString to BSTR

CString str = "whatever" ; BSTR resultsString = str.AllocSysString(); 

_bstr_t to CString

#include  #include  _bstr_t bsText("Hai Bayram"); CString strName; W2A(bsText, strName.GetBuffer(256), 256); strName.ReleaseBuffer(); AfxMessageBox(strName); char szFileName[256]; GetModuleFileName(NULL,szFileName,256); AfxMessageBox(szFileName);

Character arrays

Char array to integer

char MyArray[20]; int nValue; nValue = atoi(MyArray);

Char array to float

char MyArray[20]; float fValue; fValue = atof(MyArray);

Char Pointer to double

char *str = " -343.23 "; double dVal; dVal = atof( str );

Char Pointer to integer

char *str = " -343.23 "; int iVal; iVal = atoi( str );

Char Pointer to long

char *str = "99999"; long lVal; lVal = atol( str );

Char* to BSTR

char * p = "whatever"; _bstr_t bstr = p;


Float to WORD and Vice Versa

float fVar; WORD wVar; fVar = 247.346; wVar = (WORD)fVar; //Converting from float to WORD.  //The value in wVar would be 247 wVar = 247; fVar = (float)fVar; //Converting from WORD to float.  //The value in fVar would be 247.00 


LPSTR ->BSTR

BSTR bstr;
CString temp;
LPSTR lpstr;
temp = lpstr;
bstr = temp.AllocSysString();


BSTR -> int

CString str = CString(width);
this->m_width = atoi(str);


CString -> BSTR

BSTR bsFile = CString( pszFile ).AllocSysString();


CString -> char*

char * pszValue = NULL;

CString strValue = "AAA";

pszValue = (LPSTR)(LPCTSTR)strValue;


BSTR -> char*

char* BSTRtoChar(BSTR in)
{
 char* pChar = (char*)malloc(_bstr_t(in).length());
 strcpy(pChar,(char*)_bstr_t(in));
 SysFreeString(in);
 return pChar;
}


LPCTSTR -> int

int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);


int ->LPCTSTR

CString s;
s.Format(_T("%0.*f"),(int)nPos);


CString -> WCHAR

WCHAR       wstring[1024];
ZeroMemory(wstring, sizeof(wstring));
mbstowcs(wstring, (LPCTSTR)fullPath, fullPath.GetLength());

크리에이티브 커먼즈 라이센스
Creative Commons License
2007년 04월 04일 10시 21분 2007년 04월 04일 10시 21분

Const

1. 변수
예)
    const i = 100;
i 값 변경불가

2. 포인터형 : 기본적으로 2가지 형태가 있을 수 있음. 그외 여려형태가 가능
예1) 값은 변경 불가능하지만 주소는 변경가능한 형태
    int temp = 100, temp2 = 200;
    const int *ipConst = &temp;  // *ipConst 값 변경 불가, ipConst(주소)값은 변경가능
    // int const *ipConst = &temp;  // 이런형태로 써도 위와 같은 의미

    // *ipConst = 300;   // 불가능한 형태
    ipConst = &temp2;  // 가능한 형태

예2) 주소는 변경 불가능하지만 값은 변경가능한 형태
    int temp = 100, temp2 = 200;
    int * const iConstp = &temp;  // *iConstp 값 변경 가능, iConstp(주소)값은 변경불가

    *iConstp = 300;   // 가능한 형태
    //iConstp = &temp2;  // 불가능한 형태

주의 : const가 결합되는 위치가 값인지 주소인지에 유의


3. 참조형
예1) 직접적으로 값과 주소 모두 변경 불가능하지만 참조 원본을 통한 값변경은 가능한 경우
   int temp3 = 100, temp5 = 200;
   int const &ircVal = temp3;  
  
   //ircVal = 2000; // 컴파일 에러 발생 (const 참조는 값 변경불가)
   //ircVal = temp5;  // 주소도  변경불가
   temp3 = 9000;  // 참조 원본은 변경가능, 결과적으로 ircVal의 값도 변하게됨
   

예2) 직접적으로 값과 주소 모두 변경 가능하지만 참조가 가르키는 값은 변화가 없는경우
    int temp4 = 300, temp5 = 500;
    int & const icrVal = temp4;

    icrVal = 6000;  // 값변경 가능, 하지만 값에 변경이 안됨  
    cout << " icrVal " << icrVal << endl;  // 여전히 300이 찍힘
    icrVal = temp5; // 주소도 변경가능 역시 값에 변경이 안됨
    cout << " icrVal " << icrVal << endl;  // 여전히 300이 찍힘

4. 함수 : class의 멤버함수인 경우만 const 함수 사용가능. 해당 class의 멤버변수를 변경할수 없음.
예)
class ConstTest
{
public:
        int m_iA;

        ConstTest()
        {        m_iA = 1;           }
        int const_func1( int &a_iA,  int &a_iB) const
        {
                int a = 1;
                int b = 2;
                int c = 0;

                c = a + b;
                a_iA += 100;  
                // m_iA += 100; // 에러발생. 멤버변수는 변경 불가  
                return m_iA;
        }
};
               

5. 클레스
예)
  const CMyConstClass CC;  
 
  // 내부 멤버변수 전체를 변경불가능한 클레스,(생성자 함수만은 예외)
  // 모든 내부 멤버 함수는 기본적으로 const 함수가 되야만함.
  // 내부 함수의 지역 변수및 인자로 받은 변수는 변경가능.
크리에이티브 커먼즈 라이센스
Creative Commons License
2007년 04월 02일 01시 19분 2007년 04월 02일 01시 19분

프로그래머들의 속담

[KLDP.org에서 퍼온글~]

  • 가는 소스가 고와야 오는 파일에 바이러스 없다.
  • 잦은 Warning 에 Error 날줄 모른다.
  • 영업은 상사 편이다.
  • 디자이너는 프로그래머 편이다.
  • 프린터 밑에 누워 소스 떨어지기만을 기다린다.
  • printf 도 디버깅에 쓸려면 에러난다.
  • 에러 무서워서 코딩 못 할까
  • 소스가 한 박스라도 코딩을 해야 프로그램이다.
  • 코더도 타이핑하는 재주는 있다!!
  • 길고 짧은 것은 strlen을 써봐야 안다.
  • 소스도 먼저 코딩하는 놈이 낫다.
  • 믿는 팀장에 발등 찍힌다.
  • 개발실 청소 아줌마 삼 년에 디버깅 한다.
  • 보기 좋은 코드가 디버깅 하기 좋다.
  • 소스 잃고 백업장치 구입한다.
  • 아니 코딩한 소스에 버그 날까?
  • 안 되는 코더는 엔터를 쳐도 PC가 다운된다.
  • 잘되면 프로그래머 탓, 못되면 시스템 탓.
  • 야한 화일도 위아래가 있다.
  • 하룻 프로그래머 정품단속반 무서울줄 모른다.
  • 백업을 안하면 삼대가 내리 흉하다.
  • 안에서 새는 메모리 밖에서도 샌다.
  • 잘 키운 개발자 하나 열 코더 안부럽다.
  • 프로그램은 개발자가 짜고, 보너스는 영업이 받는다.
  • 늦게 배운 코딩 날새는줄 모른다.
  • 경영다툼에 개발자등 터진다.
  • 제 코드가 석자.
  • 개발일정 시계는 거꾸로 매달아도 간다.
  • 다 된 코드에 기획 바꾸기.
  • 버그보고 놀란가슴 오타보고 놀란다.
  • 코딩 전 마음 다르고, 코딩 후 마음 다르다.
  • 소스 놓고 main도 모른다.
  • 천줄코드도 #include부터.
  • 소스 한줄로 천냥빚도 생긴다.
  • 기능 많은 프로그램 버그잘날 없다.

그리고... 명언 모음집!

  • 내일 정전이 된다해도 나는 오늘 한 줄의 코드를 쓰겠다. - 스피노자
  • 스스로 돌아봐서 에러가 없다면 천만인이 가로막아도 나는 컴파일하리라. - 맹자
  • 나는 하루라도 코드를 쓰지 않으면 입안에 가시가 돋는다. - 안중근 의사
  • 가장 커다란 에러는 컴파일의 순간에 도사린다. - 나폴레옹
  • 나는 코딩한다, 고로 나는 존재한다. - 데카르트
  • 대박 프로그램은 1%의 영감과 99%의 노가다로 이루어진다. - 에디슨
  • 네 코드를 알라. - 소크라테스

크리에이티브 커먼즈 라이센스
Creative Commons License
2006년 07월 20일 17시 44분 2006년 07월 20일 17시 44분