Show toolbar

2014年7月14日 星期一

Regular Expression to match IP Address

標題:使用正規表示法驗證IP
VC++ (regex.cpp):
#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):
//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):
//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):
//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):
#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):
//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波範例