2010年10月26日 星期二

算質數

感覺像是以前修課在寫作業


1 #include stdio.h
2 #include stdlib.h
3
4 void calculate(int max){
5 int *result = (int*) malloc(sizeof(int) * max);
6
7 result[0] = result[1] = 0;
8 result[2] = 1;
9
10 for(int i=3; i 11 if(i%2 == 0){
12 result[i] = 0;
13 }
14 else{
15 result[i] = 1;
16 }
17 }
18
19 for(int i=3; i*i<=max; i++){
20 if(result[i]==1){
21 for(int j=i+i; j 22 result[j] = 0;
23 }
24 }
25 }
26
27 for(int i=0; i 28 if(result[i] == 1){
29 printf("%3d ", i);
30 }
31 }
32
33 }
34
35
36 int main(){
37
38 calculate(100);
39
40 return 0;
41 }

calculate PI by rand()

使用機率計算半徑為1的四分之一圓
乘以四之後為一個圓
由於圓的面積為PI*r*r,此時r又是1
所以圓面積為PI

===============================
1 #include stdio.h
2 #include stdlib.h
3 #include math.h
4
5 void get_value(int sum){
6 double x,y;
7 double tmp;
8 int count = 0;
9 int i = 0;
10
11 srand(0);
12
13 do{
14 x = (double) rand()/RAND_MAX;
15 y = (double) rand()/RAND_MAX;
16
17 tmp = x*x + y*y;
18
19 if(sqrt(tmp)<=1){
20 count++;
21 }
22
23 }while(++i < sum);
24
25 printf("result: %lf \n", 4 *((double)count/sum));
26 return;
27 }
28
29 int main(){
30
31 get_value(10000000);
32
33 return 0;
34 }

c++ inheritance about constructor/destructor/override/virtual function

1 #include
2 using namespace std;
3
4 class A{
5 public:
6 A(){
7 cout << "1" << endl;
8 }
9
10 void func1(){
11 cout << "2" << endl;
12 }
13
14 virtual void func2(){
15 cout << "3" << endl;
16 }
17
18 ~A(){
19 cout << "4" << endl;
20 }
21
22
23 };
24
25 class B:A{
26 public:
27 B(){
28 cout << "5" << endl;
29 }
30
31 void func1(){
32 cout << "6" << endl;
33 }
34
35 virtual void func2(){
36 cout << "7" << endl;
37 }
38
39 ~B(){
40 cout << "8" << endl;
41 }
42
43 };

1 #include iostream
2 #include "my-header.h"
3
4 int main(void){
5
6 A *ptr;
7
8 B *my_B = new B();//1 5
9
10 ptr = (A*)my_B;
11
12 ptr->func1();//2
13 ptr->func2();//7
14
15 my_B->func1();//6
16 my_B->func2();//7
17
18 delete my_B;//8 4
19
20 return 0;
21 }

2010年10月20日 星期三

c++ operator overload

myClass.h
1 #include "myClass.h"
2
3 myClass operator+(myClass c, int i){
4 myClass tmp(c.value() + i);
5 return tmp;
6 }
7
8 myClass operator+(int i, myClass c){
9 myClass tmp(c.value() + i);
10 return tmp;
11 }
12
13 myClass operator-(myClass c, int i){
14 myClass tmp(c.value() - i);
15 return tmp;
16 }
17
18 myClass operator-(int i, myClass c){
19 myClass tmp(c.value() - i);
20 return tmp;
21 }
22
23 myClass operator++(myClass &c){
24 c.n++;
25 return c;
26 }
27
28 myClass operator++(myClass &c, int i){
29 myClass tmp(c.value());
30 c.n++;
31 return tmp;
32 }
33
34 myClass operator--(myClass &c){
35 c.n--;
36 return c;
37 }
38
39 myClass operator--(myClass &c, int i){
40 myClass tmp(c.value());
41 c.n--;
42 return tmp;
43 }




myClass.c
1 class myClass{
2 public:
3 myClass(){
4 n=0;
5 }
6
7 myClass(int in){
8 n = in;
9 }
10
11 myClass operator+(myClass c){//only for "myClass + myClass"
12 myClass tmp(n+c.value());
13 return tmp;
14 }
15
16 myClass operator-(myClass c){
17 myClass tmp(n-c.value());
18 return tmp;
19 }
20 /*
21 myClass& operator++(){//for ++myClass //& for reduce copy object?
22 n++;
23 return *this;//"this" is a pointer
24 }
25
26 myClass operator++(int i){//for myClass++
27 myClass tmp(n);
28 n++;
29 return tmp;
30 }
31
32 myClass& operator--(){
33 n--;
34 return *this;
35 }
36
37 myClass operator--(int){
38 myClass tmp(n);
39 n--;
40 return tmp;
41 }
42 */
43
44 friend myClass operator+(myClass c, int i); //for myClass + int
45
46 friend myClass operator+(int i, myClass c); //for int + myClass
47
48 friend myClass operator-(myClass c, int i);
49
50 friend myClass operator-(int i, myClass c);
51
52
53 friend myClass operator++(myClass &c);//for ++myClass
54
55 friend myClass operator++(myClass &c, int);//for myClass++
56
57 friend myClass operator--(myClass &c);//for --myClass
58
59 friend myClass operator--(myClass &c, int);//for myClass--
60
61 int value(){
62 return n;
63 }
64 private:
65 int n;
66 };

2010年10月19日 星期二

C++ called by reference

參考http://caterpillar.onlyfun.net/Gossip/CppGossip/PassBy.html


int main(){
int x = 10;
printf("%d\n", inc(x));
printf("%d\n", x);

return 0;
}

int inc(int n){
n = n+1;
return n;
}


若程式這樣寫,則印出來為:
11
10

因為n與x是不同的變數(called by value)
==============================================
int main(){
int x = 10;
printf("%d\n", inc(x));
printf("%d\n", x);

return 0;
}

int inc(int *n){
*n = *n+1;
return *n;
}

使用指標的方式輸出為:
11
11

========================================
int main(){
int x = 10;
printf("%d\n", inc(x));
printf("%d\n", x);

return 0;
}

int inc(int &n){
n = n+1;
return n;
}
若在function的參數中加上&,則代表int &n = x
(n為x的一個別名,改變n,x會一起變)

這樣輸出也是:
11
11


=========================================
若不准傳進function的參數遭到修改的話,可以使用const
int my_function(const int &in){
...
}


2010年10月18日 星期一

flash memory

裡面圖很多都是網路上抓的














linux kernel compile & install


2010年10月13日 星期三

compile cyanogenmod android by your self for Nexus One

There is a detail steps in cyanogenmod's wiki(Passion equals Nexus One?)
http://wiki.cyanogenmod.com/index.php?title=Compile_CyanogenMod_for_Passion

I follow instructions on that wiki, finally build a bootable ROM,
then I can modify every where interested!

currently have some problems(no IME, error in quick setting...)