2014年2月27日 星期四

C - call by value, call by value of pointer, call by reference

Call by value

int swap(int a, int b) 

{
    int tmp = a;
    a = b;
    b = tmp;
    printf("%d, %d", &a, &b); // 2686752, 2686756
    printf("%d, %d", a, b); // 20, 10
}

int main() 
{
    int a, b;
    a = 10;
    b = 20;

    printf("%d, %d", &a, &b); // 2686788, 2686784
    swap(a, b);
    printf("%d, %d", a, b); // 10, 20
}

Note
    Just copy the value into swap function

Call by value of pointer
int swap(int *a, int *b) 

{
    int tmp = *a;
    *a = *b;
    *b = tmp;
    printf("%d, %d", &a, &b); // 2686752, 2686756
    printf("%d, %d", a, b); // 2686788, 2686784
    printf("%d, %d", *a, *b); // 20, 10
}

int main() 
{
    int a, b;
    a = 10;
    b = 20;

    printf("%d, %d", &a, &b); // 2686788, 2686784
    swap(&a, &b);
    printf("%d, %d", a, b); // 20, 10
}

Note:
    Send the address of the value into swap function

Call by refrence
int swap(int &a, int &b) 

{
    int tmp = a;
    a = b;
    b = tmp;
    printf("%d, %d", &a, &b); // 2686788, 2686784
    printf("%d, %d", *a, *b); // 20, 10
}

int main() 
{
    int a, b;
    a = 10;
    b = 20;

    printf("%d, %d", &a, &b); // 2686788, 2686784
    swap(a, b);
    printf("%d, %d", a, b); // 20, 10
}

Note
    alias

Reference
    什麼是傳值call by value、傳址call by address、傳參考call by reference
    [C++] Call by value、Call by pointer、Call by reference

C - Data type

sizeof
    printf("sizeof(char) = %d\n", sizeof(char));        // 1
    printf("sizeof(short) = %d\n", sizeof(short));      // 2
    printf("sizeof(int) = %d\n", sizeof(int));          // 4
    printf("sizeof(long) = %d\n", sizeof(long));        // 4
    printf("sizeof(float) = %d\n", sizeof(float));      // 4
    printf("sizeof(double) = %d\n", sizeof(double));    // 8

    printf("sizeof(char) = %d\n", sizeof(char*));       // 4
    printf("sizeof(short) = %d\n", sizeof(short*));     // 4
    printf("sizeof(int) = %d\n", sizeof(int*));         // 4
    printf("sizeof(long) = %d\n", sizeof(long*));       // 4
    printf("sizeof(float) = %d\n", sizeof(float*));     // 4
    printf("sizeof(double) = %d\n", sizeof(double*));   // 4
Reason:
    char
        -128 ~ 127 -> 8 bits needed = 1 byte
    unsigned char
        0 ~255
    short
        -32768 ~ 32767 -> 16 bits needed = 2 bytes
    unsigned short
        0 ~ 65535
    int 
        -2147483648 ~ 2147483647
    float 
        32 bit (s + exp + fraction = 1 + 8 + 23)

    double
        64 bit (1 + 11 +52)

    pointer
        4 bytes = 1 word

Precision Problem
    1. estimate the precision you want.
    2. use big number.



2014年2月25日 星期二

C# - Console in Windows Form

Open Console in Windows Form

In Microsoft Wndows Form Application, console is closed, but you can open it by following snippet.

public partial class Form1 : Form
{

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]

    static extern bool AllocConsole();

    public Form1(string[] args)
    {
        AllocConsole();

        InitializeComponent();
    }
}


Parameter usage
  In "main"
[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1(args)); // send args to Form1.cs
}
In Form1.cs
public partial class Form1 : Form
{
    public Form1(string[] args)
    {
        foreach (string parameter in args)
            System.Console.WriteLine(parameter);
    }
}

Android - NetworkOnMainThreadException


Situation:

private void connect() {

    String device_URL = "http://tw.yahoo.com/";

    HttpURLConnection urlc;

    try {

        urlc = (HttpURLConnection) new URL(device_URL).openConnection();

        urlc.connect();

        Log.e("POST", urlc.getResponseMessage());

    } catch (Exception e) {
        e.printStackTrace();
    }

}


It will cause "NetworkOnMainThreadException" when you try to connect the Internet.


Reason of this situation:

In case of the connection is too long, the screen is gonna block in the main thread.
Solution 1:

Ignore the policy, just add few code in below.
(Not recommend and it's only provide android SDK version 9 or highter)

private void ignoreNetworkPolicy() {

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);
    }
}


Solution 2:

Use thread.

public void createConnectThread() {

    Thread thread = new Thread(new Runnable() {

        @Override

	public void run() {
		request();
        }

    });

    thread.start();
}


Note:
If you want to update UI in thread, it will not permit because Android only allow main thread can update UI. The solution of this is "Handler".

private void updateUI() {

    Handler handler = new Handler();

    handler.post(new Runnable() {

        @Override

	public void run() {
		mTextView.setText("123");
	}
    });
}

Reference:
解决android.os.NetworkOnMainThreadException

2014年2月24日 星期一

C# - StreamWriter and StreamReader

0. Show text in Console

System.Console.Write("Showing\n"); 

System.Console.WriteLine("Showing");


1. Create a directory

System.IO.Directory.CreateDirectory("Test");


2. Create a file in "Test" folder.

string fileName = "Test\\data.txt";

if(File.Exists(fileName))

    File.Create(fileName).Close();


3. Write Text into "data.txt"

string fileName = "Test\\data.txt";

StreamWriter sw= new StreamWriter(fileName);



sw.Write("First");

sw.WriteLine("Second");



sw.Close();

4. Read Text from "data.txt"

string fileName = "Test\\data.txt";

StreamReader sr = new StreamReader(fileName);



System.Console.WriteLine("" + sr.Read());