顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2019/05/11

C 使用strtok切割字串


#include <iostream>
#pragma warning( disable : 4996 )

int main()
{
 char str[1024] = "121454.00,A,2237.79779,N,12035.89901,E,0.489,,100519,,,A*71";
 char* c;
 c = strtok(str, ",");
 while (c != NULL) {
  printf("%s\n", c);
  c = strtok(NULL, ",");
 }
}


參考資料:
https://dotblogs.com.tw/thkaw/2013/12/11/133462
https://blog.wu-boy.com/2010/04/cc-%E5%88%87%E5%89%B2%E5%AD%97%E4%B8%B2%E5%87%BD%E6%95%B8%EF%BC%9Astrtok-network-mac-address-%E5%88%86%E5%89%B2/

C empty a char array


char c[30] = "Hello";
memset(c, '\0', sizeof(c));

C double to char array / char array to double


char c[10] = "0.68";
double d = 0.0f;
//char array to double
d = atof(c);
//clean char array
memset(c, '\0', sizeof(c));
//double to char array
sprintf(c, "%f", degrees);
//output to display
printf("%s\n", c);

2014/03/17

C/C++選擇排序(Selection sort)

選擇排序:找到最小數值排到新的序列裡頭


輸入:未排序數序列
輸出:排序過後序列

最差時間複雜度 О(n²)
最優時間複雜度 О(n²)
平均時間複雜度 О(n²)



#include <stdio.h>


void soft(int a[], int length){
 for(int i = 0; i < length-2; i++){
  
  int min = i;

  for(int j = i + 1; j < length; j++){
   if(a[j]< a[min])
   {
    int temp = a[j];
    a[j] = a[min];
    a[min] = temp;
   }
  }
 }

 for(int i=0; i<length;i++){
  printf("%d\n", a[i]);
 }
}



int main(){
 int A[] = {98, 45, 68, 90, 29, 43, 17};
 int length = sizeof(A)/sizeof(int);
 soft(A,length);

 return 1;
}




2014/03/16

C/C++ 插入排序(Insertion Sort)

插入排序:將已知序列分別依照數值大小去排序
例子:玩撲克牌會將手上排依照號碼大小分別插入相對的位址


輸入:n個數字的序列
輸出:排列好得序列

最差時間複雜度 O(n^2)
最優時間複雜度 O(n)
平均時間複雜度 O(n^2)
最差空間複雜度  O(n) ,需要輔助空間O(1)



虛擬碼:
INSERTION-SOFT(A)

for i ← 2 to length[A]
 do key ← A[i]

 j = i -1

 while j >0 and A[j] > key
  do A[j+1] ← A[j]
  j ← j-1

 A[j+1] ← key


2014/01/22

C++ Assert

Assert:只允許正確的條件式,其他條件式會直接噴錯


#include <iostream>
#include <cassert>
using namespace std;

int main()
{

 int number = 0;
 cin >> number;
 assert(number == 0);
 cin >> number;
 assert(number == 0);

 system("pause");
 return 0;
}

2013/08/28

C/C++ 指標和雙重指標以及三重指標

指標在C語言內是一個很重要的元素,指標可以指到任何的位置,不過要小心使用,能指到記憶體位置,就代表可以透過指標去取得資料以及處理資料

不過有些方法也可以透過指標處理得很乾淨,例如Swap
在這邊我大概把之前學得,指標和雙重指標以及三重指標大概寫上來,以免哪天忘記


指標顧名思義就是指標



指標的宣告方式就是在變數前頭加上*,例如:
int *ptr;
int * ptr;


2013/07/27

資料結構 刪除單向鍊結串列



#include <stdio.h>
#include <stdlib.h>
 
#define MAX 10
 
struct List
{
    int num;
    int total;
    struct List *Next;
};
typedef struct List Node;
typedef Node *Link;

int Data[2][MAX]={1,3,5,7,9,2,4,6,8,10,
                    141,77,150,200,19,12,1414,176,188,20};
 
 
Link Delete_List(Link Head, int Key)
{
    Link Pointer = Head;
    Link Back;

    while(1){
        if(Pointer->Next == NULL){
            printf("Not found\n");
            break;
        }

        if(Head->num == Key){
            Head = Pointer->Next;
            free(Pointer);
            break;
        }

        Back = Pointer;
        Pointer = Pointer->Next;

        if(Pointer->num == Key){
            Back ->Next = Pointer->Next;
            free(Pointer);
            break;
        }
    }

    return Head;
}


void Print_List(Link Head)
{
    Link Pointer = Head;

    while (Pointer != NULL){
        printf("[%d, %d] ", Pointer->num, Pointer->total);
        Pointer = Pointer->Next;
    }
    printf("\n");
}
 
void Free_List(Link Head)
{
    Link Pointer;
    while(Head != NULL){
        Pointer = Head;
        Head = Head->Next;
        free(Pointer);
    }
}
 
Link Create_List(Link Head)
{
    Link New;
    Link Pointer;
    int i;
 
    Head = (Link)malloc(sizeof(Node));
     
    if(Head == NULL)
        printf("Memory allocate Failure!!\n");
    else{
        Head->num = Data[0][0];
        Head->total = Data[1][0];
        Head->Next = NULL;
 
        Pointer = Head;
         
        for(i=1; i<MAX; i++){
            New = (Link)malloc(sizeof(Node));
            New->num = Data[0][i];
            New->total = Data[1][i];
            New->Next = NULL;
 
            Pointer->Next = New;
            Pointer = New;
        }
    }
     
    return Head;
}
 
void main()
{
    Link Head = Create_List(Head);
    int key;
 
    if(Head != NULL){

        Print_List(Head);

        while(1){
            printf("Input 0 to Exit\n");    
            
            printf("Input data number for Delete:");
            scanf("%d", &key);
            
            if(key == 0)
                break;

            Head = Delete_List(Head, key);
            Print_List(Head);
        }

        Free_List(Head);
    }
}


參考資料:
資料結構 黃國瑜/葉乃菁

資料結構 單向鍊結串列插入


#include <stdio.h>
#include <stdlib.h>
 
#define MAX 10
 
struct List
{
    int num;
    int total;
    struct List *Next;
};
typedef struct List Node;
typedef Node *Link;
 
int Data[2][MAX]={1,3,5,7,9,2,4,6,8,10,
                    141,77,150,200,19,12,1414,176,188,20};
 
 
Link Insert_List(Link Head, Link New, int key)
{
    Link Pointer = Head;

    while(1){

        if(Pointer == NULL){
            New->Next = Head;
            Head = New;
            break;
        }

        if(Pointer->num == key){
            New->Next = Pointer->Next;
            Pointer->Next = New;
            break;
        }

        Pointer = Pointer->Next;
    }

    return Head;
}


void Print_List(Link Head)
{
    Link Pointer = Head;

    while (Pointer != NULL){
        printf("[%d, %d] ", Pointer->num, Pointer->total);
        Pointer = Pointer->Next;
    }
    printf("\n");
}
 
void Free_List(Link Head)
{
    Link Pointer;
    while(Head != NULL){
        Pointer = Head;
        Head = Head->Next;
        free(Pointer);
    }
}
 
Link Create_List(Link Head)
{
    Link New;
    Link Pointer;
    int i;
 
    Head = (Link)malloc(sizeof(Node));
     
    if(Head == NULL)
        printf("Memory allocate Failure!!\n");
    else{
        Head->num = Data[0][0];
        Head->total = Data[1][0];
        Head->Next = NULL;
 
        Pointer = Head;
         
        for(i=1; i<MAX; i++){
            New = (Link)malloc(sizeof(Node));
            New->num = Data[0][i];
            New->total = Data[1][i];
            New->Next = NULL;
 
            Pointer->Next = New;
            Pointer = New;
        }
    }
     
    return Head;
}
 
void main()
{
    Link Head = Create_List(Head);
    Link New;
    int key;
 
    if(Head != NULL){
        Print_List(Head);

        while(1){
            printf("Input 0 to Exit\n");
            
            New = (Link) malloc(sizeof(Node));
            
            printf("Input data number:");
            scanf("%d", &New->num);
            
            if(New->num == 0)
                break;

            printf("Input data total:");
            scanf("%d", &New->total);

            printf("Input the data number for Insert:");
            scanf("%d", &key);

            Head = Insert_List(Head, New, key);

            Print_List(Head);
        }

        Free_List(Head);
    }
}


參考資料:
資料結構 黃國瑜/葉乃菁

資料結構 單向鍊結串列搜尋




#include <stdio.h>
#include <stdlib.h>

#define MAX 10

struct List
{
    int num;
    int total;
    struct List *Next;
};
typedef struct List Node;
typedef Node *Link;

int SearchTime = 0;
int Data[2][MAX]={91,35,75,75,99,2,400,6,85,100,
                    141,77,150,200,19,12,1414,176,188,20};


int Search_List(int key, Link Head)
{
    Link Pointer;
    Pointer = Head;

    while( Pointer != NULL){
        SearchTime++;
        if( Pointer->num == key ){
            printf("Data number: %d\n", Pointer->num);
            printf("Data total: %d\n", Pointer->total);
            return 1;
        }
        Pointer = Pointer->Next;
    }

    return 0;
}


void Free_List(Link Head)
{
    Link Pointer;
    while(Head != NULL){
        Pointer = Head;
        Head = Head->Next;
        free(Pointer);
    }
}

Link Create_List(Link Head)
{
    Link New;
    Link Pointer;
    int i;

    Head = (Link)malloc(sizeof(Node));
    
    if(Head == NULL)
        printf("Memory allocate Failure!!\n");
    else{
        Head->num = Data[0][0];
        Head->total = Data[1][0];
        Head->Next = NULL;

        Pointer = Head;
        
        for(i=1; i<MAX; i++){
            New = (Link)malloc(sizeof(Node));
            New->num = Data[0][i];
            New->total = Data[1][i];
            New->Next = NULL;

            Pointer->Next = New;
            Pointer = New;
        }
    }
    
    return Head;
}

void main()
{
    Link Head = Create_List(Head);
    int number;

    if(Head != NULL){
        printf("Input Data number:");
        scanf("%d", &number);

        if( Search_List(number,Head)){
            printf("Search time = %d\n", SearchTime);
        }else{
            printf("NO found!!\n");
        }

        Free_List(Head);
    }
}



參考資料:
資料結構 黃國瑜/葉乃菁

資料結構 Greatest common divisor(最大公因數)

GCD是耳熟能詳的一種遞迴方法,中文意思是『求最大公因數
求最大公因數再小學的時候就有交過,有一種方法就是透過輾轉相除法
透過輾轉相除法不斷的對除,最後解即為最大公因數

程式碼如下:

#include <stdio.h>

int count = 0;

int GCD(int a, int b){
        count++;

        if(a % b == 0)  return b;
        else return GCD(b, a % b);
}

int main()
{
        printf("Number:%d\n", GCD(120, 45));
        printf("Count:%d\n", count);

        return 0;
}


資料結構 Ackermain's function(阿克曼函數)

感覺是很複雜的函數,有興趣可以探討『阿克曼函数--一个计算方法


#include <stdio.h>

int count = 0;

int Ackerman(int m, int n){
        count++;

        if(m == 0) return n + 1;
        else if(n == 0) return Ackerman(m - 1, 1);
        else return Ackerman(m - 1, Ackerman(m, n - 1));
}


int main()
{
        printf("Number:%d\n", Ackerman(2,2));
        printf("Count:%d\n", count);

        return 0;
}



參考資料:
http://zh.wikipedia.org/wiki/%E9%98%BF%E5%85%8B%E6%9B%BC%E5%87%BD%E6%95%B8
http://blog.sina.com.cn/s/blog_4765a1560100bpqn.html

資料結構 Binomial coefficient(二項式係數)

有興趣可以參考一下Binomial coefficient




#include <stdio.h>
int count = 0;

int Binomial(int n, int m){
         count++;

         if( n == m || m == 0 )
                return 1;
         else
                return (Binomial( n - 1, m) + Binomial( n - 1 , m - 1 ));
}

int main()
{
         printf("Number:%d\n",Binomial(5,3));
         printf("Count%d\n",count);

        return 0;
}



參考資料:
http://en.wikipedia.org/wiki/Binomial_coefficient

2013/07/02

.Net 產生函式庫說明檔

最近在寫關於HRV的DLL,平常寫程式都只有寫上註解
但封裝成DLL的時候,就沒有註解可以看了,但可以使用Visual Studio的物件瀏覽器去瀏覽DLL有包裝什麼方法在裡面,效果又是如何?



不知道有什麼Tag可以使用的人可以參考『建議的標記,文件註解 (Visual C++)』,這篇寫得還蠻詳細
雖然是For C++,仍是值得參考呀!


2013/05/24

Visual Studio2010 用VC開發OpenGL

最近要開始寫OpenNI,開始來找一下如何用Visual Studio 2010設定VC環境
VC以前是獨立的產品,自從Visual Studio 2005就被整合在裡面

對於開發VC的程式經驗實在不多,所以來補足自己Know-how不足之處

2013/02/28

C++ Plain Old Data (POD)

Plain old data structures are appropriate when there is a part of a system where it should be clearly indicated that the detailed logic for data manipulation and integrity are elsewhere. PODs are often found at the boundaries of a system, where information is being moved to and from other systems or persistent storage and the problem domain logic that is found in other parts of the system is not relevant. For example, PODs would be convenient for representing the field values of objects that are being constructed from external data, in a part of the system where the semantic checks and interpretations needed for valid objects have not yet been applied.


2013/01/31

Dependency Walker 2.2

Dependency Walker是一套可以拿來看dll輔助工具,像我常常必須寫不一樣語言的程式,有可能要調用dll檔;這工具就還蠻值得使用的

如果有安裝Microsoft Visual Studio 2010企業版應該可以搜尋電腦底下的『depends.exe』,如果沒有的話可以去官網下載,不過可能要注意系統位元問題

初始介面如下圖,如果要開啟dll則選取File/Open即可

2012/12/04

分析VC Thread

Source Code來自於微軟Demo,分析是寫給學弟看得,C語言有很多種實現執行緒的方式。

那我找到是這個,就參考底下的範例去分析吧!
依依瞭解C在執行緒的方式以及解決方法,如果你有讀完OS聖經,想必更好懂原因。

哈哈,等你會了老闆一定會愛死你的 哈哈

對了,如果不懂的話下面有我找到的參考資料,慢慢看吧