Show toolbar

2014年7月14日 星期一

Regular Expression Iterator

標題:使用迭代器匹配字串
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <regex> //string lib here
using namespace std;
int main(int argc, char* argv[])
{
string str = "facet normal -8.944272e-001 4.472136e-001 0.000000e+000";
regex pattern("[+-]?[0-9]\\.[0-9]+[eE][+-]?[0-9]+");
auto words_begin = sregex_iterator(str.begin(), str.end(), pattern);
auto words_end = sregex_iterator();
cout << "Found " << std::distance(words_begin, words_end) << " numbers:\n";
for (sregex_iterator i = words_begin; i != words_end; ++i) {
cout << atof( ((smatch) *i).str().c_str() ) << '\n';
}
system("pause");
return 0;
}


說明:
使用迭代器匹配字串。

Regular Expression to match IP Address

標題:使用正規表示法驗證IP
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <regex> //string lib here
using namespace std;
int main(void) {
string ip = "192.168.1.100";
smatch results;
regex pattern("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)");
if(regex_match(ip, results, pattern)) {
cout << atof( results[1].str().c_str() ) << endl;
cout << atof( results[2].str().c_str() ) << endl;
cout << atof( results[3].str().c_str() ) << endl;
cout << atof( results[4].str().c_str() ) << endl;
} else {
cout << "Not Match." << endl;
}
system("pause");
return 0;
}

說明:
簡單使用正規表示法驗證IP,並印出每個區段數值。

Bisection method using Recursion

標題:使用遞迴函式計算二分法

使用遞迴
VC++ (recursion.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Chapter 3 - Recursion
#include <iostream>
using namespace std;
const int No = 1000;
const double tolerance = 0.00001;
const double minimal = 0;
const double maximal = 1;
double equation(double);
double determine(double, double, double=equation(minimal), int=0);
int main(void)
{
cout << "Best: " << scientific << determine(minimal, maximal) << endl;
system("pause");
return 0;
}
double determine(double a, double b, double fa, int n) {
double p = 0, fp = 0;
p = a+(b-a)/2;
fp = equation(p);
//cout << dec << n << '\t' << scientific << p << endl;
if(( fp==0 || (b-a)/2 < tolerance ) || n >= No)
return p;
return (fa*fp>0) ? determine(p, b, fp, n+1) : determine(a, p, fa, n+1);
}
double equation(double x) {
return x - 1/pow(2.0, x);
}
/*double equation(double x) {
return pow(x, 4)-2*pow(x, 3)-4*pow(x, 2)-4*x+4;
}*/

不使用遞迴
VC++ (bisectionMethod.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Chapter 3 - Bisection Method
#include <iostream>
using namespace std;
const int No = 1000;
const double tolerance = 0.00001;
const double minimal = 0;
const double maximal = 1;
double equation(double);
int main(void)
{
int i=0;
double x=0, y=0, fa=0, fp=0, p=0, a=0, b=0;
a = minimal;
b = maximal;
fa = equation(a);
while(i<No) {
p = a+(b-a)/2;
fp = equation(p);
if(fp==0 || (b-a)/2 < tolerance) {
break;
}
if(fa*fp>0) {
a = p;
fa = fp;
} else {
b = p;
}
i++;
}
cout << scientific << p << endl;
system("pause");
return 0;
}
/*
double equation(double x) {
return pow(x, 4)-2*pow(x, 3)-4*pow(x, 2)-4*x+4;
}
*/
double equation(double x) {
return x - 1/pow(2.0, x);
}

說明:
比較遞迴與不遞迴方式計算二分法。

Default Argument

標題:函式預設引數
VC++ (defaultArgument.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 3 - Default Argument
#include <iostream>
using namespace std;
double circumference(double, double = 3.14);
int main(void)
{
//scientific使用科學記號輸出
cout << scientific << circumference(1) << endl;
cout << scientific << circumference(1, acos(-1.0)) << endl;
system("pause");
return 0;
}
double circumference(double d, double pi) {
return d * pi;
}
範例結果:


說明:
基礎的函式預設引數範例。

2014年7月2日 星期三

Simple Regular expression

標題:基本正規表示法
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
//#include <string>
#include <regex> //string lib here
using namespace std;
int main(void)
{
int r = 0;
string number = "";
//匹配模型
regex number_format("[0-9]{1,5}");
//隨機數
srand(time(NULL));
r = rand()%10;
cout << "Input number:" << endl;
cin >> number;
//資料比對
if(regex_match(number, number_format)) {
cout << number << " * " << r << " = " << stoi(number) * r << endl;
} else {
cout << "Not match." << endl;
}
system("pause");
return 0;
}

說明:
基本的正規表示法進行數字的匹配,並乘上一隨機數。

For statement

標題:for迴圈語句
VC++ (forstatement.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Chapter 1 - for statement
#include <iostream>
using namespace std;
int main(void)
{
int i = 0, j = 0, len = 0;
const double PI = acos(-1.0), DEGREE = 180.0/PI;
for(i=0;i<=180;i+=10) {
len = (int)( sin( i/DEGREE )*30 );
for(j=0;j<len;j++) {
cout << '*';
}
cout << " " << len << endl;
}
system("pause");
return 0;
}
範例結果:

說明:
基本使用for迴圈印出sin波範例

If...else...statements

標題:if else 判斷語句
VC++ (ifstatement.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Chapter 1 - if statement
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int len = 0;
string footballer = "";
cout << "Input Name:";
cin >> footballer;
len = footballer.length();
if(len != 0 && len <= 10) {
cout << footballer << endl;
} else {
cout << "Word length of less than 10" << endl;
}
system("pause");
return 0;
}

說明:
基本if else判斷語句的使用

Cin and cout command

標題:輸入與輸出命令
VC++ (variable.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 1 - cin and cout
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int age = 19, year = 2011;
string footballer = "";
cout << "Input Name:";
cin >> footballer;
cout << footballer << " is a Brazilian footballer.\n";
cout << "At the age of " << age << ", " << footballer <<" won the " << year << " South American Footballer\n";
system("pause");
return 0;
}

說明:
基本輸入與輸出命令

Variable type and size

標題:變數型態與大小
VC++ (datatype.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Chapter 1 - datatype
#include <iostream>
using namespace std;
int main(void)
{
cout << "type\tbyte" << endl;
cout << "int\t" << sizeof(int) << endl; //4
cout << "long\t" << sizeof(long) << endl; //4
cout << "float\t" << sizeof(float) << endl; //4
cout << "double\t" << sizeof(double) << endl; //8
cout << "char\t" << sizeof(char) << endl; //1
cout << "bool\t" << sizeof(bool) << endl; //1
system("pause");
return 0;
}

說明:
基本的變數型態與大小

Cout example

標題:cout範例
VC++ (cout.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Chapter 1 - Part1
//引用函式庫,使用標準函式庫
#include <iostream>
//標準函式庫的命名空間
using namespace std;
//主函式寫法
int main(void)
{
//印出整數及文字
cout << 10 << endl;
cout << "Neymar" << endl;
//送出Command命令pause
system("pause");
//回傳終止
return 0;
}

說明:
基礎cout範例程式