2012年8月25日 星期六

[C++] Sample for Using stdlib to implement Extend Quick Sort function

As last example, I use standard library to implement quick sort again.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>

void quickSortEx(int[], int, bool, int, int);
int cmp(const void* a, const void* b);

int _tmain(int argc, _TCHAR* argv[])
{
    srand(time(NULL));
    const int count = 10;
    int number[count] = {0};

    printf("Before: ");
    int i;
    for(i = 0; i < count; i++) {
        number[i] = rand() % 100;
        printf("%d ", number[i]);
    }
    printf("\n");

    quickSortEx(number, count, true, 0, count-1);

    printf("Afer: ");
    for(i = 0; i < count; i++)
        printf("%d ", number[i]);

    printf("\n");
    return 0;
}

void quickSortEx(int number[], int count, bool desc, int left, int right)
{
    qsort(number, count, sizeof(number[0]), cmp);

    if (desc)
        std::reverse(number, number+count);
}

int cmp(const void* a, const void* b)
{
    const int x = *static_cast<const int*>(a);
    const int y = *static_cast<const int*>(b);

    if (x == y)
        return 0;

    return x > y ? 1 : -1;
}


Reference:
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
http://www.cplusplus.com/reference/algorithm/reverse/

2012年8月24日 星期五

[C++] A sample for Extended Quick Sort

I need to implement a function to sort items with specified ascending order.
I modify other's sample as below.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void quickSortEx(int[], int, bool, int, int);
void swap(int &x, int &y);

int _tmain(int argc, _TCHAR* argv[])
{
    srand(time(NULL));
    const int count = 10;
    int number[count] = {0};
 
    printf("Before: ");
    int i;
    for(i = 0; i < count; i++) {
        number[i] = rand() % 100;
        printf("%d ", number[i]);
    }
    printf("\n");

    quickSortEx(number, count, false, 0, count-1);

    printf("Afer: ");
    for(i = 0; i < count; i++)
        printf("%d ", number[i]);
 
    printf("\n");
    return 0;
}

void quickSortEx(int number[], int count, bool desc, int left, int right) {
    if(left < right) {
        int i = left;
        int j = right + 1;

        while(1) {
            // To find the item should sort after 'left'
            while(i + 1 < count && (desc ? number[left] < number[++i] : number[++i] < number[left]));
            // To find the item should sort before 'left'
            while(j -1 > -1 && (desc ? number[left] > number[--j] : number[--j] > number[left])) ;
            if(i >= j)
                break;
            swap(number[i], number[j]);
        }

        swap(number[left], number[j]);

        quickSortEx(number, count, desc, left, j-1);
        quickSortEx(number, count, desc, j+1, right);
    }
}

void swap(int &x, int &y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
}

Reference:
http://caterpillar.onlyfun.net/Gossip/AlgorithmGossip/QuickSort1.htm
http://emn178.pixnet.net/blog/post/88613503-%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E6%B3%95(quick-sort)

2012年8月23日 星期四

[C++] Sample for String Compare

It is a simple sample for understanding how to implement a function to compare two string.

#include "stdafx.h"
#include <string.h>
#include <iostream>

using namespace std;

int codePointCompare(const char* c1, const char* c2)
{
 int l1 = strlen(c1);
 int l2 = strlen(c2);
        const unsigned lmin = l1 < l2 ? l1 : l2;
        unsigned pos = 0;
        while (pos < lmin && *c1 == *c2)
 {
             c1++;
             c2++;
             pos++;
        }

        if (pos < lmin)
             return (c1[0] > c2[0]) ? 1 : -1;

        if (l1 == l2)
             return 0;

 return (l1 > l2) ? 1 : -1;
}

int _tmain(int argc, _TCHAR* argv[])
{
 char* c1 = new char[1024];
 char* c2 = new char[1024];
 cout << "please input first string:";
 cin >> c1;
 cout << "please input second string:";
 cin >> c2;

 cout << codePointCompare(c1, c2);
 return 0;
}


Reference:
http://trac.webkit.org/changeset/110822/trunk/Source/JavaScriptCore/wtf/text/StringImpl.cpp

2012年8月20日 星期一

[C++] Threading Sample

This is a simple sample for understanding of thread create in C++.

#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <stdio.h>

void myfunc(int n);

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE thd[2];
    DWORD tid;

    printf("Thead Start.\n");

    for (int i=0; i<2; i++)
    {
        thd[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)myfunc, (void*)i, 0, &tid);
        printf("Thread %d is start \n",tid);
    }


    Sleep(1000);

    printf("main is end \n");

    system("pause");
}

void myfunc(int n)
{
    int i;
    for(int i=0;i<5;i++){
        printf("Thread %d, index=%d\n", n, i);
        Sleep(1000);
    }
    printf("Thread %d is over \n", getpid());
}

Reference:
http://wuminfajoy.blogspot.tw/2010/10/c.html

2012年8月16日 星期四

[C++] Create GUID

You can create GUID by using CoCreateGuid API.
I copy example source code from my reference article for memo as below.


#include "stdafx.h"
#include <objbase.h>
#include <string>

using namespace std;

wstring GetGUID()
{
    _TUCHAR *guidStr = NULL;

    GUID *pguid = new GUID;

    CoCreateGuid(pguid);

    // Convert the GUID to a string
    UuidToString(pguid, (RPC_WSTR*)&guidStr);
    delete pguid;
    return wstring(guidStr);
}

int _tmain(int argc, _TCHAR* argv[])
{
wstring guid = GetGUID();
    wprintf(guid.c_str());
    return 0;
}



Reference:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms688568(v=vs.85).aspx
http://www.dotblogs.com.tw/alonstar/archive/2011/09/05/mfc_32bit_64bit.aspx

2012年8月12日 星期日

[MS SQL] Performance counter registry hive consistency check

To avoid meet the problem again, I memo the solution here.

While installing SQLServer 2008 on Windows 7 64bit - Traditional Chinese Version.
I got an error message told me "Performance counter registry hive consistency check".
It would not happen on English version OS.
Here is an article will help.
http://www.dotblogs.com.tw/lastsecret/archive/2010/06/14/15865.aspx

In addition, you should execute window update to download Visual Studio 2008 sp1 if you like to install SQL Server 2008 Management Studio. Otherwise you'll got error message as "Another version of Microsoft Visual Studio 2008 has been detected on this system that must be updated to SP1.  Please update all Visual Studio 2008 installations to SP1 level, by visiting Microsoft Update."