2013年10月21日 星期一

Cool Buttons 超連結變漂亮的按鈕

Wow, 好厲害,透過 <A> 超連結 tag 就做出各式各樣漂亮的 button,真的是太神奇了,立馬把 css 摳回家。

這裡有扁平設計(Flat Design)、漸變陰影 (Gradient)、3D 按鈕等一應俱全喔 (^_^)


URL: http://fribly.com/2013/10/18/cool-buttons/

2013年7月24日 星期三

Android 多國語系 strings.xml 檔案轉 iOS InfoPlist.strings

同時間開發 Android 和 iOS 時, 要共用其設定好的多國語言設定檔的話, 可以透過 Notepad++ 的 RegExp 來幫忙轉換.

Android to iOS:
Find: <string name="
([A-Z0-9_\-.%@ ':]+)">(.*)</string> 
Replace as: "\1"="\2";

iOS to Android:
Find: "([A-Z0-9_\-.%@ ':]+)"="(.*)";
Replace as: <string name="\1">\2</string>

Notepad++官方說明文章如下:

Notepad++ RegExp List
Note: In case you have the plugins installed, try CONTROL+R or in the Menu Plugins � TextFX Quick - Find/Replace to get a sophisticated dialogue including a drop down for regular expressions and multi line search/replace.
In a regular expression, special characters interpreted are:
.Matches any character
(This marks the start of a region for tagging a match; so what's inside ( ) you can use in "replace with" using \1, \2 etc.
)This marks the end of a tagged region.
\nWhere n is 1 through 9 refers to the first through ninth tagged region when replacing. For example, if the search string was Fred([1-9])XXX and the replace string was Sam\1YYY , when applied to Fred2XXX this would generate Sam2YYY .
\<This matches the start of a word using Scintilla's definitions of words.
\>This matches the end of a word using Scintilla's definition of words.
\xThis allows you to use a character x that would otherwise have a special meaning. For example, \[ would be interpreted as [ and not as the start of a character set.
[...]This indicates a set of characters, for example, [abc] means any of the characters a, b or c. You can also use ranges, for example [a-z] for any lower case character.
[^...]The complement of the characters in the set. For example, [^A-Za-z] means any character except an alphabetic character.
^This matches the start of a line (unless used inside a set, see above).
$This matches the end of a line.
*This matches 0 or more times. For example, Sa*m matches Sm , Sam , Saam , Saaam and so on.
+This matches 1 or more times. For example, Sa+m matches Sam , Saam , Saaam and so on.

Source of this information is the Scintilla edit component help, but it was adapted to Notepad++ behaviour.
Notepad++ RegExp Examples
Important
  • You have to check the box "regular expression" in search & replace dialog
  • When copying the strings out of here, pay close attention not to have additional spaces in front of them! Then the RegExp will not work!
You use a MediaWiki (e.g. WikipediaWikitravel) and want to make all headings one "level higher", so a H2 becomes a H1 etc.
  1. Search ^=(=)
    Replace with \1
    Click "Replace all" to find all headings2...9 (two equal sign characters are required) which begin at line beginning (^) and to replace the two equal sign characters by only the last of the two, so eleminating one and having one remaining.
  2. Search =(=)$
    Replace with \1
    Click "Replace all" to find all headings2...9 (two equal sign characters are required) which end at line ending ($) and to replace the two equal sign characters by only the last of the two, so eleminating one and having one remaining.
  3. == title == became = title =, you're done :-)
You have a document with a lot of dates, which are in German date format (dd.mm.yy) and you'd like to transform them to sortable format (yy-mm-dd). Don't be afraid by the length of the search term – it's long, but consiting of pretty easy and short parts.
  1. Search ([^0-9])([0123][0-9])\.([01][0-9])\.([0-9][0-9])([^0-9])
    Replace with \1\4-\3-\2\5
    Click "Replace all" to fetch
    • the day, whose first number can only be 0, 1, 2 or 3
    • the month, whose first number can only be 0 or 1
    • but only if the spearator is . and not any charcter ( . versus \. )
    • but only if no numbers are sourrounding the date, as then it might be an IP address instead of a date
    and to write all of this in the opposite order, except for the surroundings. Pay attention: Whatever SEARCH matches will be deleted and only replaced by the stuff in the REPLACE field, thus it is mandtory to have the surroundings in the REPLACE field as well!
  2. 31.12.97 became 97-12-31 and 14.08.05 became 05-08-14 and the IP address 14.13.14.14 did not change, you're done :-)
You have printed in windows a file list using dir /b/s >filelist.txt to the file filelist.txt and want to make local URLs out of them.
  1. Open filelist.txt with Notepad++
  2. Search \\
    Replace with /
    Click "Replace all" to change windows path separator char \  into URL path separator char / 
  3. Search ^(.*)$
    Replace with file:///\1
    Click "Replace all" to add file:/// in the beginning of all lines
  4. Depended on your requirements, preceed to escape some characters like space to %20 etc.
  5. C:\!\aktuell.csv became file:///C:/!/aktuell.csv, you're done :-)
Another Search Replace Example
[Data]
EU AX ALA 248 �land Islands
EU AL ALB 008 Albania, People's Socialist Republic of
AF DZ DZA 012 Algeria, People's Democratic Republic of
OC AS ASM 016 American Samoa
EU AD AND 020 Andorra, Principality of
AF AO AGO 024 Angola, Republic of
NA AI AIA 660 Anguilla
AN AQ ATA 010 Antarctica (the territory South of 60 deg S)
NA AG ATG 028 Antigua and Barbuda
SA AR ARG 032 Argentina, Argentine Republic
AS AM ARM 051 Armenia
NA AW ABW 533 Aruba
OC AU AUS 036 Australia, Commonwealth of
[SearchPattern]
([A-Z]+) ([A-Z]+) ([A-Z]+) ([0-9]+) (.*)
[ReplacePattern]
\1,\2,\3,\4,\5
[FinalData]
AS,AF,AFG,004,Afghanistan
EU,AX,ALA,248,�land Islands
EU,AL,ALB,008,Albania, People's Socialist Republic of
AF,DZ,DZA,012,Algeria, People's Democratic Republic of
OC,AS,ASM,016,American Samoa
EU,AD,AND,020,Andorra, Principality of
AF,AO,AGO,024,Angola, Republic of
NA,AI,AIA,660,Anguilla
AN,AQ,ATA,010,Antarctica (the territory South of 60 deg S)
NA,AG,ATG,028,Antigua and Barbuda
SA,AR,ARG,032,Argentina, Argentine Republic
AS,AM,ARM,051,Armenia
NA,AW,ABW,533,Aruba
OC,AU,AUS,036,Australia, Commonwealth of

2013年5月22日 星期三

[C] Sudoku

上次寫C語言, 可能是在2專的時候, 在 1997年或 1998年, 這次挑戰用C 語言寫數獨(Sudoku) 的程式, 大約 0.009秒就可以計算出簡單版本數獨的結果. 試了一下, 好像要用亂數來幫忙做決定猜測的流程.

input:
194800203500012096203495008935700682418029500726500900600058324840936015000070060

output:
1948XX2X35XXX12096...
(Where X means any one of 1..9)


以下的程式只能解決簡單的情況, 無法解決 "需要去猜測" 的情況.

#include <stdio.h>

int option_ary[81][10];

//int main(int argc, char *argv[])
int main()
{
    /*
    if(argc < 2 || argc > 3)
    {
        printf("Pleae input one paramater.\n");
        return 0;
    }
    */
 
    //printf("input string is %s.\n",argv[1]);
    //printf("input strlen is %d.\n",strlen(argv[1]));
   
    char input_string[256];
    scanf("%s",input_string);
    //printf("your string: %s",input_string);
    //return 1;
   
    int i,j;
   
    // initial options table.
    // first [i][0] is the final answer.
    for(i=0;i<81;i++){
        option_ary[i][0]=0;
        for(j=1;j<=9;j++){
            option_ary[i][j]=j;
        }
    }
   
    // get input data.
    for(i=0;i<81;i++){
        //int input_value = argv[1][i]-48;
        int input_value = input_string[i]-48;
        option_ary[i][0]=input_value;
        if(option_ary[i][0]>0){
            remove_optins_in_table(i,input_value);
        }
    }

    int check_count=0;
    int check_result=0;
    for(i=0;i<100;i++){
        check_count++;
        check_result=check_answer(0);
        if(check_result==1)
        {
            break;
        }
    }
   
    // 無解(或多種解), 開始猜測可能的解.
    // (沒寫)

    if(check_result==1){
        printf("finish, check count:%d",check_count);
    }
    else
    {
        printf("check fail..., no answer.");
    }


   
    // test convert index
    //int index=80;
    //printf("row/col/block: %d/%d/%d", get_row(index), get_col(index), get_block(index));

    // output final answer
    //printf("\n");
    for(i=0;i<81;i++){
        printf("%d",option_ary[i][0]);
        /*
        if(i % 9==8){
            printf("\n");
        }
        */
    }
    return 1;
}


// purpose: check answer
// return:
//      1: finish.
//      0: not finish.
int check_answer(int try_to_guess)
{
    int ans_i=0,ans_j=0;
    int unknow_value_count=0;
    for(ans_i=0; ans_i<81; ans_i++)
    {
        int zero_count=0;
        int last_value=0;
        if(option_ary[ans_i][0]==0){
            unknow_value_count++;
            // value un-know.
            for(ans_j=1; ans_j <=9; ans_j++)
            {
                if(option_ary[ans_i][ans_j]==0)
                {
                    zero_count++;
                }
                else
                {
                    if(last_value==0){
                        // looking for first value.
                        last_value=option_ary[ans_i][ans_j];
                    }
                    // looking for last value.
                    //last_value=option_ary[ans_i][ans_j];
                }
            }
            if(zero_count==8)
            {
                // bingo.
                //printf("\n bingo %d,%d\n",ans_i,last_value);
                option_ary[ans_i][0]=last_value;
                remove_optins_in_table(ans_i,last_value);
            }
            else {
                if(zero_count==(8-try_to_guess)){
                    // two element, guess last one.
                    if(try_to_guess>=1){
                        option_ary[ans_i][0]=last_value;
                        remove_optins_in_table(ans_i,last_value);
                    }
                }
            }
        }
    }
   
    int return_value=0;
    if(unknow_value_count==0)
    {
        return_value=1;
    }
   
    return return_value;
}

int get_row(int index)
{
    return (index/9)+1;
}

int get_col(int index)
{
    return index%9;
}

int get_block(int index)
{
    int base_row=(index/27)*3+(index%9/3)+1;
}

int remove_optins_in_table(int index, int input_value)
{
    // remove options in row.
    remove_options_in_row(index, input_value);
   
    // remove options in col.
    remove_options_in_col(index, input_value);
   
    // remove options in block.
    remove_options_in_block(index, input_value);
}

// remove options in row.
int remove_options_in_row(int index, int input_value)
{
    int i=0;
    for(i=0; i<9; i++)
    {
        int target_index=((index/9)*9)+i;
        option_ary[target_index][input_value]=0;
    }
   
    return 1;
}

// remove options in col.
int remove_options_in_col(int index, int input_value)
{
    int i=0;
    for(i=0; i<9; i++)
    {
        int target_index=(index%9)+(i*9);
        option_ary[target_index][input_value]=0;
    }
   
    return 1;
}

// remove options in block.
int remove_options_in_block(int index, int input_value)
{
    int i=0;
    int target_block=get_block(index);
    for(i=0; i<81; i++)
    {
        if(get_block(i)==target_block)
        {
            option_ary[i][input_value]=0;
        }
    }
   
    return 1;
}

Facebook 留言板