kowala's home

kowala's home
這裡是我的學習筆記,陸續增加中。
http://kowala21.blogspot.com

2011-07-26

Turbo C 結構體的使用

本範例示範結構體的使用
如何傳遞結構體給函數,
有如物件般,把資料封裝起來.

執行如圖



由於結構太大,所以無法一一列出,故只能跳著列出,每20層一印,每層又有20筆,總執行次數為 n = 11000次,結構示意如下

<01期>
    |--<01層>
    |        |--<10筆>
    |        |--<10筆>
    |--<02層>
            |--<10筆>
            |--<10筆>
 ...
    |--<100層>
            |--<10筆>
            |--<10筆>
 ...
<10期>
    |--<01層>
    |        |--<10筆>
    |        |--<10筆>
    |--<02層>
            |--<10筆>
            |--<10筆>

 ...
    |--<100層>
            |--<10筆>
            |--<10筆>

程式碼如下

#include <conio.h>
#include <stdio.h>
#include <string.h>
    char msg[128]="TC struct data test.\nby kowala... \t2011-07-26\n";
    struct data{
        int position[10];
        int mapping[10];
    };
    struct node{
        int period;
        struct data numbers[100];
    };
    struct node srcNode[10];        /* 示範結構體 */

/* 畫線 */
void print_line(){printf("\n---------------------------------\n");}
/* 清結構體 */
void clrNode(struct node *map,int len){
    int i,j,k;   
    for(i=0;i<=len;i++){
        map[i].period=0;
        for(j=0;j<100;j++){
            for(k=0;k<10;k++){
                map[i].numbers[j].position[k]=0;
                map[i].numbers[j].mapping[k]=0;
            }       
        }
    }   
}
/*******************************
本範例示範結構體的使用
*******************************/
void main() {
    int i,j,k,n,nodeCount=10;   
    clrscr();
    printf("%s\n",msg);

    clrNode(&srcNode,nodeCount);    /* 清結構體 */
    n=0;
    for(i=0;i<=nodeCount;i++){       
        printf("1-%d(%d) ",i,srcNode[i].period);
        for(j=0;j<100;j++){
            for(k=0;k<10;k++){
            n++;
            if(j%20==0 && k==0)
            printf("2-%d(%d,%d) ",j,srcNode[i].numbers[j].position[k],srcNode[i].numbers[j].mapping[k]);
            }
        }
        printf("\n");
    }   
    printf("After %d calculation...\n",n);
    print_line();
}

2011-07-19

Win7 批次檔 for 寫法

我發現有些指令都沒動作了,像是以前常用的 inkey.com [1] ,現在都不行用了!
剛測試還能跑 for 迴圈,簡單寫個 Memo 起來備查。

測試 kb.exe  <-- 這是我要跑的目標程式
輸入檔 input01, input02, input03 .... input20
輸出結果 01.out,  02.out,  03.out,  ... 20.out

執行畫面轉向到 dbg.txt

使用了兩個批次檔,一個跑迴圈,一個帶參數執行,FOR 迴圈指令,我是寫成一行,沒換行。

go.bat
-------------------檔案開始-----------------------
@echo off
echo Debug for KB.EXE v.1.0.1 > dbg.txt
FOR %%A IN (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20) DO call gokb input%%A %%A >> dbg.txt
del *.out
type dbg.txt
echo.
echo You can reading dbg.txt for test detail. @_@
echo.
pause
---------------------檔案結束---------------------


gokb.bat
----------------------檔案開始--------------------
@echo off
echo.
echo  ========================= start %1 =========================
kb %1 %2.out
-------------------檔案結束-----------------------

在 win7 下可以跑
ASCII 表
這也是很常用的ASCII對照表: 


註:
[1].inkey.com 是讀鍵值小程式,通常都是搭配 if errorlevel==27  goto xxx 來使用
製作方法如下( 27 = 0x1B = ESC )

製作 inkey.com ,由 Windows 內建的工具 debug 來製作即可,按下開始按鈕後,上面搜尋欄位輸入 cmd 再按 enter ,就會跑出命令列視窗,


 輸入debug 就可以叫出如下畫面,然後依照下圖輸入組合語言指令,輸入完成後,按 Ctrl+C 中止輸入,指定長度 rcx 為 24,指定名稱 n,再寫出 w,離開 q ... 就完成了。

接受合法鍵值 ESC, 0-9, a-z, A-Z
其他鍵值會重新輸入,直到正確鍵值為止,傳回鍵值可以用 errorlevel 去接

Microsoft Windows [???? 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\aaa>debug

-a 100
1392:0100 mov ah,00
1392:0102 int 16
1392:0104 cmp al,1b
1392:0106 je 120
1392:0108 cmp al,30
1392:010A jl 0100
1392:010C cmp al,7a
1392:010E jg 100
1392:0110 cmp al,39
1392:0112 jle 120
1392:0114 cmp al,61
1392:0116 jge 120
1392:0118 cmp al,41
1392:011A jl 100
1392:011C cmp al,5a
1392:011E jg 100
1392:0120 mov ah,4c
1392:0122 int 21
1392:0124 ^C
-rcx
CX 0000
:24
-n inkey
-w
Writing 00024 bytes
-q

C:\Users\aaa>

再把它改名為 inkey.com 就能用了,但在 windows 環境中不能跑 = =,只能用在 dos 環境。

2011-07-14

Turbo C - 字串陣列參數處理

能夠看到這裡,就是有緣,可以為您省下大半天的時間,說實在的,TC的字串功能不太友善,相信有經驗的人就知道,字串陣列又比較麻煩,常常都耗掉許多時間測試。

字串陣列的問題是出在 "不定長度",它不像數值陣列,每個元素長度固定,字串都是以 '\0' 做結尾的,舉例來說

char a[3];
a[0] <= "不定"
a[1] <= "不定長"
a[2] <= "不定長度"

看了上例知道,字串就是不定長度。能正確使用字串,大約有兩種方法,一是固定長度,二是用指標,其實指標也是固定長度,傳遞位址的長度就是int。下面範例兩種方法都用,先把數字轉成字串再存入陣列之中,因為是要實驗字串,不是數字。

在下面範例中,固定長度的關鍵在於宣告字串陣列的長度 [1]

 void setArr2(char nodeArr[][3] )

這樣才能正確取到陣列元素


void setArr1();
void setArr2();

void main() {
    char msg[50];
    char data[50][3];
    int i;

    clrscr();
    printf("\n\tArray test...\n\n");

    setArr1(&msg);
    printf("%s\n",msg);
    setArr2(&data);
    for(i=0;i<50;i++)printf("data[%d] = %s\t",i,data[i]);
}
/* 用指標 */
void setArr1(char *msgArr){
    strcpy(msgArr,"Hello, here show you how to deal with char[].\n");
}
/* 固定長度 */
void setArr2(char nodeArr[][3]){
    int i;
    char tmp[3];
    for(i=0;i<50;i++){
        itoa(i,tmp,10);
        strcpy(nodeArr[i],tmp);      
    }
}

結果如下


參考資料
[1]  Turbo C 簡介

2011-07-13

Turbo C -參數 main(int argc, char *argv[])

這幾天幫人重整老東西,用 Turbo C 2.0 寫的,就順手把它整理上來放,現在好像很多學校還是喜歡用這個工具來教學,特別是資料結構之類的課。Turbo C 簡稱 TC,是 Borland 公司的產品,現在好像有開放下載,所以 Google 一查就可以找到載點。

main(int argc, char *argv[])

這是甚麼呢?這是很古早時代在用的東西,當程式執行時,在程式尾端掛幾個參數,這是很常用的手法,那在開發環境中要怎麼寫這種程式及測試呢,以下就是個小範例。


先來看看程式碼

#include <conio.h>
#include <stdio.h>
#include <string.h>

char str1[32];
char str2[32];
char str3[32];

int main(int argc, char *argv[]) {

    int i;
    clrscr();

    printf("\nargc = %d\n",argc);
    for(i=0;i<=3;i++)printf("argv[%d] = %s\n",i,argv[i]);

    strcpy(str1,argv[1]);
    strcpy(str2,argv[2]);
    strcpy(str3,argv[3]);

    printf("\n");
    printf("str1 = %s\n",str1);
    printf("str2 = %s\n",str2);
    printf("str3 = %s\n",str3);
  
    return(0);
}

執行結果


傳入參數在 Turbo C 中的設定


說明

上圖紅色所框的部分就是宣告引入參數
int argc 這個是傳入的參數個數,是整數值
char *argv[]  這個是參數值,是C字串格式,如果要用數字的話,可以使用 atoi() 來轉數字。

例如
count=atoi(argv[0]);    /* 字串轉數字 */

參數部分說明,本例是測試 3 個參數,

d2340.354
opennum
e2340.354

但是 argc 卻會顯示 4 個,因為 argv[] 第一個都固定放程式的執行路徑,所以變成4個了,如圖一可知,參數有四個。

要如何取出參數呢,我們知道參數是C字串,不能直接指定給變數 str1、str2、str2,而是要透過函數 strcpy() 取出。

char str1[32];
char str2[32];
char str3[32];
....

    strcpy(str1,argv[1]);
    strcpy(str2,argv[2]);
    strcpy(str3,argv[3]);

這樣就行了。 ^^

[註]C字串:就是字元陣列,在最尾端擺入 '\0' 當作字串結束標記
char mystr = {'a','b','c','d','e','f','g','\0'}
mystr = "abcdefg";
古早時候是沒有 String 的。

2011-07-11

離奇!埃特納火山噴發 時鐘快轉15分

原文出處








義大利西西里島上的埃特納火山,今年第5度大噴發,不但導致當地機場一度關閉,詭異的是,火山爆發後,西西里島上的數位電子鐘、鬧鐘,全都快跑了15到20分鐘,當地民眾議論紛紛,實在搞不懂,火山爆發為什麼會讓時鐘變快?

埃特納火山驚天爆,岩漿、碎石噴個不停,熾熱岩漿順著火山斜坡滾滾而下,濃密的火山灰衝上高空。

位於西西里島東南岸的埃特納火山,今年以來第5次噴發,它三不五時就來蠢動一下,還好持續時間不長,觀光客還會搭車,近距離觀賞火山噴發奇景,一度被迫關閉的西西里島卡塔尼亞機場,隔天就重新開放,火山噴發稀鬆平常,怪就怪在當地鬧鐘,居然變快了。

網友:「島上所有的數位時鐘,全都快跑了15分鐘,真的非常詭異,非常詭異。」

島上居民一大早發現,大家都提早到班,這才曉得,舉凡電子錶、鬧鐘,甚至電腦上的時間,都快了15分鐘,到底為什麼會這樣?沒有人搞得清楚,各種揣測紛紛出籠。網友:「島上民眾有人怪罪幽浮,也有人說是太陽火焰惹禍,大家都摸不清頭緒。」

其實早在5月間,西西里島就有人發現數位時鐘突然變快的現象,至今沒有定論,謎團還沒解開之際,埃特納火山再度噴發,島上的數位時鐘又快轉了15分鐘。

2011-07-08

Trigonometric 三角函數

說明

這個是我第一隻 apk 程式,可以查看自選的三角函數作圖,可以顯示單一函數,或是一起顯示,每隔90度(PI/2)劃一條垂直線,也繪製了上下限的線 (Sin(x)=0),功能很陽春,請勿見怪,操作如下方影片。

應用程式螢幕擷取畫面

影片

 

2011-07-05

YouTube 三二事,轉檔器介紹...

寫 Blog 文章,有時用影片來介紹是省時省力的作法,尤其是軟體操作這種動態流程,用影片是最簡單的說明,直接給 USER 看您怎麼用它。
那影片要放哪裡呢?免費的 YouTube 是不錯啦,但首先要了解它支援那些格式,這樣才能使用愉快。
下面是 YouTube 官網對接受的影片檔案格式說明。

我能夠上傳哪些影片檔案格式?
http://www.google.com/support/youtube/bin/answer.py?answer=55744&hl=zh_TW&msrcid=fupldhq

YouTube 接受的影片檔案格式

    Windows Media Video(.avi)
    .3GP (手機適用)
    .AVI (Windows 適用)
    .MOV (Mac 適用)
    .MP4 (ipod/psp 適用)
    .MPEG
    .FLV (Adobe Flash 適用)
    .MKV (h.264 適用)

好了,既然已經知道它支援的格式,那就萬事俱備,只欠東風了,我們還需要一個優良的轉檔器,當然,所謂優良的條件,最好也是 free,這裡我將為大家介紹一款不錯的軟體,叫做 Any Video Converter,AVC
這是一款功能強大、操作簡單、支援上述所有影片格式及多國語言的轉檔工具,而且是免費,快去下載來用吧!

官網 Any Video Converter
Any Video Converter Free version is an All-in-One video converting tool
http://www.any-video-converter.com/download-avc-free.php

下面是安裝時的選項

這個應該不用,我都用 KMP


應該不用去參觀官網,好用比較實在。


這是安裝完成執行的畫面,各位可以看到 1. > 2. > 3.
轉檔就是那麼簡單!

2011-07-03

Android Tween Animation test

kowala's home
http://kowala21.blogspot.com

look at my video first.


my test code under list


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

    private ImageView imageView;
    private Button alphaBtn,rotateBtn,scaleBtn,translateBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        imageView = (ImageView) findViewById(R.id.imageView1);     
        alphaBtn = (Button) findViewById(R.id.button1);
        alphaBtn.setOnClickListener(btnClickListen);
        rotateBtn = (Button) findViewById(R.id.button2);
        rotateBtn.setOnClickListener(btnClickListen);
        scaleBtn = (Button) findViewById(R.id.button3);
        scaleBtn.setOnClickListener(btnClickListen);
        translateBtn = (Button) findViewById(R.id.button4);
        translateBtn.setOnClickListener(btnClickListen);
    }
    private OnClickListener btnClickListen = new OnClickListener(){
        @Override
        public void onClick(View v) {
            switch(v.getId()){
              case R.id.button1://alpha
                  doAlpha(1,0);
                  break;
              case R.id.button2://rotate
                  doRotate(0,720);             
                  break;
              case R.id.button3://scale
                 doScale(0f,0f,18f,18f);                
                  break;
              case R.id.button4://translate                 
                  doGoto(-2.4f,-2.6f,2.7f,3.8f);
                  break;
            }
        }
    };
    //淡入淡出
    private void doAlpha(float alpha1,float alpha2){      
        Animation alphaAnimation = new AlphaAnimation(alpha1,alpha2);
        alphaAnimation.setDuration(1500);      
        imageView.startAnimation(alphaAnimation);
    }
    //逆時針轉,順時針轉
    private void doRotate(float deg1,float deg2){          
        Animation rotateAnimation = new RotateAnimation(deg1,deg2,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(2000);      
        imageView.startAnimation(rotateAnimation);      
    }
    //放大縮小
    private void doScale(float x1,float y1,float x2,float y2){      
        Animation scaleAnimation = new ScaleAnimation(x1,x2,y1,y2,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);          
        scaleAnimation.setFillAfter(false);
        scaleAnimation.setDuration(4000);
        imageView.startAnimation(scaleAnimation);
    }
    //移動
    private void doGoto(float x1,float y1,float x2,float y2){      
        Animation tAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, x1, Animation.RELATIVE_TO_SELF, x2,
        Animation.RELATIVE_TO_SELF, y1, Animation.RELATIVE_TO_SELF, y2);          
        tAnimation.setFillAfter(false);
        tAnimation.setDuration(4000);      
        imageView.startAnimation(tAnimation);
    }
}


layout file main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal" android:layout_height="wrap_content" android:paddingTop="20sp">
   
<TextView 
    android:layout_height="wrap_content" android:textColor="#ff8888ff"
    android:text="@string/hello" android:layout_width="fill_parent" android:gravity="center_horizontal" android:textSize="20sp" android:typeface="sans" android:textStyle="bold"/>
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:src="@drawable/dedo"></ImageView>
    <LinearLayout
    android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:paddingBottom="20sp">
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="透明"></Button>
    <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉"></Button>
    <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="比例"></Button>
    <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移動"></Button>
</LinearLayout>
</RelativeLayout>

Reference:
Android 动画框架详解
http://www.ibm.com/developerworks/cn/opensource/os-cn-android-anmt1/index.html

Android画图学习总结(四)
http://www.moandroid.com/?p=790
http://www.moandroid.com/?p=808

Android 如何手動安裝APK

有很多免費的APK檔案都不錯玩,來源除了Market之外,到處都可以下載的到,那要如何安裝到手機中呢?以下將一步一步帶您完成。

1. 首先是打開手機權限,允許安裝非 Market 應用程式

MENU > 設定 > 應用程式 > 未知的來源  [打勾]





2. 安裝 ASTRO File Manager,可以從官網或Market或手動安裝,手動安裝其實不用裝,只要把它 copy 到手機中,直接執行就行了。[1]

按這裡可以下載 apk 檔 ASTRO File Manager 訊6

另一個 http://www.appbrain.com/app/astro-file-manager/com.metago.astro

3. 先來下載個五子棋來測試看看,到網站去

http://apk.gfan.com/Product/App150906.html


4. 選直接下載後,存檔到桌面備用,我是先把那隨機檔名改一下,改成 Backgammon.apk,中文檔名會錯喔。然後再把它 copy 到手機的 SD卡裏頭。



5. 開啟 ASTRO ,有如檔案總管般的畫面,預設路徑就是 SD卡了,如果不是的話,就按左邊的主頁,紅色房子那個啦~,就會到 SD卡了,把 Backgammon.apk 按下去。


 6. 跳出對話視窗,按下 "開啟程式管理員"


 7. 看到 "安裝" ,就給他按下去
 再按一次 "安裝"


8. 等他跑完,等一會兒吧


9. 好了,開啟試玩一下吧
 10.還不錯玩,贏了

 哈哈
11. 把它放在桌面


這樣就行了。

註 [1].這裡有一些關於 ASTRO File Manager 教學說明。

http://www.android-hk.com/applications/astro-file-manager-2

http://gfans.bryan.tw/2010/10/01/621