Show toolbar

2015年5月6日 星期三

Using the div tag to create table colspan

標題:使用div建立表格合併欄位
HTML (index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-TW" xml:lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name=viewport content="width=device-width, initial-scale=1">
<title>TableBoxy</title>
<style>
.css_fieldset { /*框架*/
    padding: 10px;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
}
.css_legend { /*框架標題*/
    float: left;
    background: #fff;
    margin-top: -20px;
    padding: 0 10px;
}
.css_tablebox { /*表格框架*/
    clear: both;
    overflow-x: auto;
}
.css_table { /*表格*/
    border-collapse: collapse;
    display: table;
}
.css_tr { /*橫列*/
    padding: 3px;
    display: table-row;
}
.css_th { /*標題欄位*/
    padding: 5px;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
    display: table-cell;
    font-weight: bold;
    white-space: nowrap;
}
.css_td { /*一般欄位*/
    padding: 5px;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
    display: table-cell;
    white-space: nowrap;
}
.css_colspan { /*表格合併欄位*/
    border-left-style: solid;
    border-top-style: solid;
    border-bottom-style: solid;
    border-left-width: 1px;
    border-top-width: 1px;
    border-bottom-width: 1px;
    border-color: #000000;
    display: table-cell;
    white-space: nowrap;
}
.css_colspan_text { /*文字欄位*/
    width: 0px;
    padding: 5px;
    position:relative;
}
.css_colspan_blank { /*中間空欄位*/
    border-top-style: solid;
    border-bottom-style: solid;
    border-top-width: 1px;
    border-bottom-width: 1px;
    display: table-cell;
}
.css_colspan_last { /*最後欄位*/
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: solid;
    border-top-width: 1px;
    border-right-width: 1px;
    border-bottom-width: 1px;
    border-color: #000000;
    display: table-cell;
}
</style>
</head>
<body>
<div class="css_fieldset">
    <div class="css_legend">標題</div>
    <div class="css_tablebox">
        <div class="css_table">
            <div class="css_tr">
                <div class="css_th">標題</div>
                <div class="css_th">日期</div>
                <div class="css_th">回應</div>
            </div>
            <div class="css_tr">
                <div class="css_td">今天晚餐吃少一點吧</div>
                <div class="css_td">2015-05-05</div>
                <div class="css_td">5</div>
            </div>
            <div class="css_tr">
                <div class="css_colspan">
                    <div class="css_colspan_text">宵夜不吃了,早點回家睡覺比較實際。</div>
                </div>
                <div class="css_colspan_blank"></div>
                <div class="css_colspan_last"></div>
            </div>
            <div class="css_tr">
                <div class="css_td">今天早餐到底要吃什麼好呢?</div>
                <div class="css_td">2015-05-06</div>
                <div class="css_td">3</div>
            </div>
            <div class="css_tr">
                <div class="css_td">作者</div>
                <div class="css_colspan">
                    <div class="css_colspan_text">QQBoxy</div>
                </div>
                <div class="css_colspan_last"></div>
            </div>
        </div>
    </div>
</div>
</body>
</html>


範例:

標題
標題
日期
回應
今天晚餐吃少一點吧
2015-05-05
5
宵夜不吃了,早點回家睡覺比較實際。
今天早餐到底要吃什麼好呢?
2015-05-06
3
作者
QQBoxy


於新視窗開啟檢視效果:http://output.jsbin.com/kagobamopi

說明:
使用div建構具有合併欄位(colspan)的表格效果,
並且建構div分組fieldset、legend能在行動裝置自適應顯示過長內容。

2014年7月14日 星期一

Regular Expression Iterator

標題:使用迭代器匹配字串
VC++ (regex.cpp):
#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):
#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;
}
範例結果:


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