Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

2008-09-26

Field Service Notebook v1.1

Download

Changelog:
v1.1 (2008-09-25)
Update displayed data after item(s) deleted.
v1.0 (2008-09-24)
Glitch fixed, summary of month implemented.
beta-0919 (2008-09-19)
Basic functions implemented.

2008-09-14

Field Service Notebook BETA Released

Helping JWs managing field service records and data of householders.

Project page: http://wiki.jienew.twbbs.org/project:field-service-notebook

Now beta version released(09/14). Basic features are implemented. Try it~ ^^"

Using wxWidgets and SQLite, it's cross-platform. Windows build is available on project page; for Linux build, contact me.

2008-08-20

Wiimote 動作辨識 Source Code

由於沒時間修改以及補完... 直接公佈原始碼。(July 9)

Document:
http://wiki.jienew.twbbs.org/project:wiimote-gesture-recognition
Source&Demo:
http://wiki.jienew.twbbs.org/local--files/project:wiimote-gesture-recognition/WiimoteGR.zip

使用的語言是C++。整體架構大概就是用 HMM 辨識動作量化後的 symbol sequence,還有用 SQLite3 (3.5x) 存資料。兩者都包成成物件方便使用,另外也定義了一個量化方式的 interface。主程式即是用文字介面來使用這些物件。本來要改成 GUI,但時間不足。有興趣的同學請自行取用。

關於 wiimote 如何透過藍芽與電腦相連可參閱這篇: http://wiibrew.org/wiki/Wiimote_Tutorial。基本上就是偵測藍芽裝置時,同時按 wiimote 上的 1, 2 鍵,就可以建立藍芽連線。

由於使用 WiiYourself!(1.0) 驅動 wiimote,所以只能跑在 windows 上,若改用 wiiuse 就可以跨平台了,但同樣因為沒時間所以... Linux user 請自己改XD。只有主程式用到 WiiYourself!,演算法的部份跟SQLite3都可以跨平台。

2008-06-25

Head First Design Pattern in C++

最近要開始唸 Head First Design Pattern... 裡面都是JAVA程式碼:P

雖然JAVA是我最早學的語言,不過最近偏向用C++ @@"

JAVA寫起來是很好看沒錯...不過user要裝VM才能用實在是有點麻煩,而且速度也稍慢,GUI介面也與OS原生的有點不搭...

離題了...

http://sourceforge.net/projects/hfdp-cpp/ 這裡有人把 Head First Design Pattern中的 code 改成 C++,可以參考看看。不過不看這個直接看書應該就能寫出 C++ code,畢竟設計模式可是不分語言的。

2008-06-19

Wiimote Gesture Recognition

為了期末報告寫的 wiimote 動作辨識終於在今天有了初步成果。

放個 demo 上來...

http://wiki.jienew.twbbs.org/project:wiimote-gesture-recognition

在 Demo 標題下有連結。

接下來要打報告...來不及做實驗了orz

2008-05-07

Convolution Code Library

根據林舒教授的 Error Control Coding 一書定義的 Convolution Code 所撰寫的 C++ Library,可以根據任意的(n,k,m) 以及 g vectors 產生 encoder / decoder 來模擬編碼解碼。使用 Viterbi Algorithm。

原始碼/DEMO下載

p.s. 演算法不確定是否正確,尤其 decoder 仍無法判斷 code 在編碼時何時開始補 0,可能誤判。

是我誤判= =" ... 演算法是對的

2008-04-26

5分鐘計時開始!

星期四寫的... 五分鐘碼表

給有特殊需求的人:P

下載

2008-04-20

C++ Vector Iterator

vector 新增/刪除元素會導致 iterator 失效,此時不該再對其 iterator 做 dereference ,否則產生 runtime error。

使用 list 可保證 iterator 不失效,但 iterator 前後關係仍會改變。此外 list 不支援像 aList[i] 這樣直接用 index 存取其中的元素。

2008-04-18

C++ Dynamic 3D Array

WoW!

#include <iostream> int main(void){ int i=6,j=5,k=4; int ****threeDimensionArr;// 4 stars! for 3D threeDimensionArr = new int ***[i]; for(int i_i=0; i_i<i; ++i_i){ threeDimensionArr[i_i] = new int **[j]; for(int j_i=0; j_i<j; ++j_i){ threeDimensionArr[i_i][j_i] = new int *[k]; for(int k_i=0; k_i<k; ++k_i){ threeDimensionArr[i_i][j_i][k_i] = new int(i_i*j_i*k_i); std::cout << *threeDimensionArr[i_i][j_i][k_i]<<"\t"; } std::cout << std::endl; } std::cout << std::endl; } //free memory for(int i_i=0; i_i<i; ++i_i){ for(int j_i=0; j_i<j; ++j_i){ for(int k_i=0; k_i<k; ++k_i){ delete threeDimensionArr[i_i][j_i][k_i]; } delete [] threeDimensionArr[i_i][j_i]; } delete [] threeDimensionArr[i_i]; } }

C++ Array of References?

It's legal to get reference of dynamical allocated object:

std::vector<double> *vec = new std::vector<double>(5); std::vector<double> &vec0Ref = *vec;

Use it:

#include <iostream> #include <vector> int main(){ size_t *n = new size_t; std::cin >> *n; std::vector<double> *vec = new std::vector<double>[*n]; // No error when vec is Pointer to Nothing! (*n == 0/1) // It is dangerous using dynamic array... std::vector<double> &vec0Ref = vec[0]; std::vector<double> &vec1Ref = vec[1]; double *a = new double; std::cin >> *a; vec0Ref.push_back(*a); //runtime-error when *n <= 0 std::cout << vec0Ref[0] << std::endl; vec1Ref.push_back(*a); //runtime-error when *n <= 1 std::cout << vec1Ref[0] << std::endl; }

Note:

會想到要拿到動態物件的 reference,是因為想要在物件建立後使用 array of references 取代 array of pointers ,用來放 sorting 的結果或做 mapping。但發現 It's illegel!

reference 並不能用來作為 array 或 container 的元素。基本上,reference 並不是物件,因此沒有辦法複製它,或改變其內容(指reference本身);其意義只是物件的別名。在JAVA/C#中的意義又不太一樣了;reference 感覺像是 const pointer。

當我們要對已存在的數個物件做 mapping,sorting 之類的動作,只能使用 array of pointers 來避免複製物件。之前會想要使用 reference 是因為書上說使用 reference 比 pointer 自然,也錯把 reference 視作 const pointer。(以前學JAVA= =)然而在這種情況下,使用 array of pointers 才是合理且正確的用法。

Another Test:

#include <iostream> int main(){ //existing objects int a=1, b=2; int arr[2] = {a,b}; // correct //error: declaration of `refs' as array of references int &refs[2] = {a,b}; //error! //array of pointers to integer int *ptrs[2] = {&a,&b}; // correct //reference of arr[2] int (&ref)[2] = arr; // correct //pointer to arr[2] int (*ptr1)[2] = &arr; // correct //assign "array" to "pointer to array" int (*ptr2)[2] = arr; // error! //pointer to first element of array int *ptr3 = arr; // correct std::cout <<"arr="<<arr<<" arr[1]="<<arr[1]<< std::endl <<"ref="<<ref<<" ref[1]="<<ref[1]<<std::endl <<"ptr1="<<ptr1<<" *ptr1="<<*ptr1 <<" (*ptr1)[1]="<<(*ptr1)[1]<<std::endl <<"ptr3="<<ptr3<<" ptr3[1]="<<ptr3[1]<<std::endl; std::cout <<"sizeof(arr)="<<sizeof(arr)<<std::endl <<"sizeof(ref)="<<sizeof(ref)<<std::endl <<"sizeof(ptr1)="<<sizeof(ptr1)<<std::endl <<"sizeof(*ptr1)="<<sizeof(*ptr1)<<std::endl <<"sizeof(ptr3)="<<sizeof(ptr3)<<std::endl; }
將錯誤去掉後的執行結果:
arr=0012FF5C arr[1]=2 ref=0012FF5C ref[1]=2 ptr1=0012FF5C *ptr1=0012FF5C (*ptr1)[1]=2 ptr3=0012FF5C ptr3[1]=2 sizeof(arr)=8 sizeof(ref)=8 sizeof(ptr1)=4 sizeof(*ptr1)=8 sizeof(ptr3)=4

可見 array of reference 不合法。

此外,雖然 ptr3 跟 arr 幾乎等效,ptr3 其實還是 pointer to int(arr[0]) 而非 array name。而 ptr1 是 pointer to arr,因此*ptr1 就是 arr。

2008-04-13

C++ Dynamic Array Test

Note:

The elements of a dynamically allocated array can be initialized only to the default value of the element type. The elements cannot be initialized to separate values as can be done for elements of an array variable.

To initialize elements,

method 1:

std::cin >> n; std::vector<int> **vec = new std::vector<int> *[n]; for (int i=0;i<n;i++) //initialize each vector vec[i]=new std::vector<int>(i); // use first vector vec[0]->push_back(something);

method 2:

std::cin >> n; // vectors are initialized by default constructor std::vector<int> *vec = new std::vector<int>[n]; for (int i=0;i<n;i++) // change size of each vector (vec+i)->resize(i); // equivalent to vec[i].resize(i) // use first vector vec[0].push_back(something);

2008-04-12

C++ Array and Reference Test

這幾天在學C++,對arrayreference的用法做個測試。

#include <iostream> #include <string> void test(std::string (&str)[2]){ std::cout << sizeof(str) << " " << sizeof(std::string); } int main(){ size_t n; std::cin >> n; size_t m = 2; const size_t o = 3; std::string *testStrArr1 = new std::string[n];// correct std::string testStrArr2[n]; // wrong in VC++, correct in gcc, wrong in ISO C++ std::string testStrArr3[2] = {"hello","hi"}; // correct std::string testStrArr4[m]; // wrong in VC++, correct in gcc, wrong in ISO C++ std::string testStrArr5[o]; // correct test(testStrArr1); // wrong test(testStrArr2); // wrong test(testStrArr3); // correct test(testStrArr4); // wrong test(testStrArr5); // wrong [3]->[2] }

正確的那行會印出: 8 4

2006-12-17

How to Compile wxWidgets in Windows Using MinGW

Download

wxWidgets
wxWdigets 2.8.0 is used in this post. Enter the "Download" page of this site, then download wxMSW source archieve.
MinGW
Enter the "Download" page, then download MinGW-5.0.2.exe or newer version in the "Proposed" section.

Inatall

Execute files you just downloaded. Inatall step by step. Be sure that make tool and C/C++ compilers selected when installing MinGW.

Add path [MinGW Directory]\bin to Path variable.

Change directory to [wxWidgets Directory]\build\msw

Enter mingw32-make -f makefile.gcc UNICODE=1 SHARED=0 BUILD=release MONOLITHIC=0

Now we can compile sample programs using the same command. The output binary file will be in the gcc_mswu subdirectory.

Reference

This post is just a note for myself

2005-10-07

Computer Organization HW1

MIPS assembly code: (copied from textbook)
( n corresponds to the argument register $a0 )
 
fact: addi $sp,$sp,-8 # adjust stack for 2 items
sw $ra,4($sp) # save the return address
sw $a0,0($sp) # save the argument n
slti $t0,$a0,1 # test for n < 1
beq $t0,$zero,L1 # if n >= 1, go to L1.
 
addi $v0,$zero,1 # return 1
addi $sp,$sp,8 # delete 0 and address
jr $ra # return to after jal of L1
 
L1: addi $a0,$a0,-1 # n >= 1: argument gets (n - 1)
jal fact # call fact with (n - 1)
 
lw $a0,0($sp) # return from jal:restore argument n
lw $ra,4($sp) # restore the return address
addi $sp,$sp,8 # adjust stack pointer to pop 2 items
mul $v0,$a0,$v0 # return n * fact(n-1)
jr $ra # return to the caller
 
trace: n = 3
 
fact: addi $sp,$sp,-8 # adjust stack for 2 items
sw $ra,4($sp) # save the return address in stack
sw $a0,0($sp) # save the argument n = 3 in stack
slti $t0,$a0,1 # test if n < 1
beq $t0,$zero,L1 # if n >= 1, go to L1. Now n = 3, so go to L1
 
L1: addi $a0,$a0,-1 # n = 3 - 1 = 2
jal fact # call fact with n = 2
 
fact: addi $sp,$sp,-8 # adjust stack for 2 items
sw $ra,4($sp) # save the return address in stack
sw $a0,0($sp) # save the argument n = 2 in stack
slti $t0,$a0,1 # test if n < 1
beq $t0,$zero,L1 # if n >= 1, go to L1. Now n = 2, so go to L1
 
L1: addi $a0,$a0,-1 # n = 2 - 1 = 1
jal fact # call fact with n = 1
 
fact: addi $sp,$sp,-8 # adjust stack for 2 items
sw $ra,4($sp) # save the return address in stack
sw $a0,0($sp) # save the argument n = 1 in stack
slti $t0,$a0,1 # test if n < 1
beq $t0,$zero,L1 # if n >= 1, go to L1. Now n = 1, so go to L1
 
L1: addi $a0,$a0,-1 # n = 1 - 1 = 0
jal fact # call fact with n = 0
 
fact: addi $sp,$sp,-8 # adjust stack for 2 items
sw $ra,4($sp) # save the return address in stack
sw $a0,0($sp) # save the argument n = 0 in stack
slti $t0,$a0,1 # test if n < 1
beq $t0,$zero,L1 # if n >= 1, goto L1. n = 0,so goto next line
 
addi $v0,$zero,1 # return 1
addi $sp,$sp,8 # pop 2 items(0 and return address) of stack
jr $ra # return to the instruction after jal
 
lw $a0,0($sp) # returned from jal. restore argument 1
lw $ra,4($sp) # restore the return address
addi $sp,$sp,8 # pop 2 items(1 and return address) of stack
mul $v0,$a0,$v0 # return n*fact(n-1) = 1*fact(0) = 1 * 1 = 1
jr $ra # return to the instruction after jal
     
lw $a0,0($sp) # returned from jal. restore argument 2
lw $ra,4($sp) # restore the return address
addi $sp,$sp,8 # pop 2 items(2 and return address) of stack
mul $v0,$a0,$v0 # return n*fact(n-1) = 2*fact(1) = 2 * 1 = 2
jr $ra # return to the instruction after jal
     
lw $a0,0($sp) # returned from jal. restore argument 3
lw $ra,4($sp) # restore the return address
addi $sp,$sp,8 # pop 2 items(3 and return address) of stack
mul $v0,$a0,$v0 # return n*fact(n-1) = 3*fact(2) = 3 * 2 = 6
jr $ra # return to the caller

2005-03-03

My First C Program!!

#include <stdio.h> int main(){ char* myname = "jieNew"; printf("Hello! World! My name is %s.", myname); return (0); }

output:

Helo! World! My name is jieNew.