2013年11月26日 星期二

Step by step to compile a C++ program on Ubuntu.

Reference:
http://www.wikihow.com/Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux

1. Install compiling tool
sudo apt-get install build-essential

2.Create a directory to hold your programs
mkdir -p CCPP/HelloWorld

3. Go into the directory you created.
cd CCPP/HelloWorld

4. Create CPP file
gedit main.cpp

5. Fill code in main.cpp and save it.
#include<iostream>
using namespace std;
int main()
{
    cout << "\nHello World !\n" << endl;
    return(0);
}

6. Compile it.
g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
7. Execute it.
./HelloWorldCPP



2013年10月28日 星期一

2013年10月22日 星期二

Install LAMP on Ubuntu

Copy from http://www.howtoforge.com/ubuntu_lamp_for_newbies for note.

Install Apache

1. Open up the Terminal (Applications > Accessories > Terminal).
2. Type the following command in Terminal
sudo apt-get install apache2
3. The Terminal will then ask you for you're password, type it and then press enter.

Testing Apache

1. Open up any web browser and then enter the following into the web address:
http://localhost/












Install PHP

Step 1. Again open up the Terminal (Applications > Accessories > Terminal).
Step 2. Copy/Paste the following line into Terminal and press enter:
sudo apt-get install php5 libapache2-mod-php5
Step 3. In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:
sudo /etc/init.d/apache2 restart

Test PHP

Step 1. In the terminal copy/paste the following line:
sudo gedit /var/www/testphp.php
This will open up a file called phptest.php.
Step 2. Copy/Paste this line into the phptest file:
<?php phpinfo(); ?>
Step 3. Save and close the file.
Step 4. Now open you're web browser and type the following into the web address:
http://localhost/testphp.php
The page should look like this:
















Install MySQL

To finish this guide up we will install MySQL. (Note - Out of Apache and PHP, MySQL is the most difficult to set up. I will provide some great resources for anyone having trouble at the end of this guide.)
Step 1. Once again open up the amazing Terminal and then copy/paste this line:
sudo apt-get install mysql-server

It would tip you to set password for 'root' and continue the installing.














Step 2 (optional). In order for other computers on your network to view the server you have created, you must first edit the "Bind Address". Begin by opening up Terminal to edit the my.cnf file.
gksudo gedit /etc/mysql/my.cnf
Change the line
bind-address = 127.0.0.1
And change the 127.0.0.1 to your IP address.

Step 3 (optional). Login mysql and set password for root
mysql -u root -p
Following that copy/paste this line:
mysql> set password for root@localhost = password('yourpassword');
(Make sure to change yourpassword to a password of your choice.)

Step 4. We are now going to install a program called phpMyAdmin which is an easy tool to edit your databases. Copy/paste the following line into Terminal:
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
It would ask you to choice http server type and input mysql password to set config.

Now just restart Apache and you are all set!
sudo /etc/init.d/apache2 restart

2013年10月16日 星期三

Get web content by telnet

We can get http content by telnet.

1. telnet to host server.
    telnet www.yahoo.com 80
2. send request
    GET / HTTP/1.1
    Host: www.yahoo.com









2013年6月28日 星期五

Convert from char* to wchar_t*

To Convert char* to wchar_t*

Assume we have a char* string named c

    const size_t cSize = strlen(c)+1;
    wchar_t* wc = new wchar_t[cSize];
    mbstowcs (wc, c, cSize);


Now you have wchar_t* string wc.

You can also refer to http://twnin.blogspot.tw/2012/03/convert-between-char.html for more type to convert.

2013年5月27日 星期一

[C#] Download files from website.

We have two ways to download files from website by using .net framework.

1. WebClient.
using System.Net;

private void button1_Click(object sender, EventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadFile("http://www.taifex.com.tw/DailyDownload/Daily_2013_05_24.zip", "d:\\Daily_2013_05_24.zip");
}


2. HttpRequest + Stream
using System.IO;
using System.Net;

private void button1_Click(object sender, EventArgs e)
{
    string url = "http://www.taifex.com.tw/DailyDownload/Daily_2013_05_24.zip";
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

    System.IO.Stream dataStream = httpResponse.GetResponseStream();
    byte[] buffer = new byte[8192];

    FileStream fs = new FileStream("d:\\Daily_2013_05_24.zip", FileMode.Create, FileAccess.Write);
    int size = 0;
    do
    {
        size = dataStream.Read(buffer, 0, buffer.Length);
        if (size > 0)
            fs.Write(buffer, 0, size);
    } while (size > 0);
    fs.Close();

    httpResponse.Close();

    Console.WriteLine("Done at " + DateTime.Now.ToString("HH:mm:ss.fff"));
}



Reference:
http://blog.darkthread.net/post-2008-10-14-download-file-with-c.aspx

2013年5月11日 星期六

N Coin Problem

Question:
Given a list of 'N' coins, their values being in an array A[], return the minimum number of coins required to sum to 'S' (you can use as many coins you want). If it's not possible to sum to 'S', return -1

For Example, input N coins array { 1, 3, 5 } and S as 11, the answer should be 3



My Answer:  (I am not sure if it is correct.)
int minCoins(int* a, int count, int target)
{
    int N = count;
    int S = target;

    int *mina=NULL;
    mina = new int[S+1];
 
    mina[0]=0;
     
    for(int i=1;i<=S;i++)
    {
        mina[i]=-1;
 
        for(int j=0;j<N;j++)
        {
            if(a[j]<=i && mina[i-a[j]] != -1)  
            {
                if(mina[i]==-1 || mina[i-a[j]]+1 < mina[i])
                {
                   mina[i] = mina[i-a[j]]+1;
                }
            }
        }
    }
 
    return mina[S];
}

2013年5月10日 星期五

Circle sorted array searching.

Question:
Given a circle sorted array, please write a function to search a number and output its position.

Example:
Find number 6 in array { 1,2,3,4,5,6,7 }, output is 5
Find number 6 in array { 5,6,7,1,2,3,4 }, output is 1

My Answer:  (I am no sure if it is correct.)

#include "stdafx.h"
#include 

using namespace std;

int binarySearch(int n, int* a, int l, int r);

int _tmain(int argc, _TCHAR* argv[])
{
 int a[7] = { 4, 5, 6, 7, 1, 2, 3 };
 
 cout << binarySearch(6, a, 0, 6) << endl;
 cout << binarySearch(2, a, 0, 6) << endl;
 cout << binarySearch(5, a, 0, 6) << endl;

 cin.get();
 return 0;
}

int binarySearch(int n, int* a, int l, int r)
{
 int i = (l + r) / 2;
 if (n == a[i])
  return i;

 if (a[l] < a[r])
 {
  if (n > a[i])
  {
   l += 1;
  }
  else
  {
   r = i - 1;
  }
 }
 else
 {
  if (n > a[i] || n < a[l])
  {
   l += 1;
  }
  else
  {
   r = i - 1;
  }
 }

 return binarySearch(n, a, l, r);
}

2013年5月9日 星期四

Number Complement.

Question: 
A complement of a number is defined as inversion (if the bit value = 0, change it to 1 and vice-versa) of all bits of the number starting from the leftmost bit that is set to 1. 

For example, if N = 5, N is 101 in binary. The complement of N is 010, which is 2 in decimal. Similarly if N = 50, then complement of N is 13 
Complete the function getIntegerComplement(). This function takes N as it's parameter. The function should return the complement of N.  (The N >=0)


My Answer:  (I am not sure if it is correct.)
int getComplement(int n)
{
    if (n == 0)
        return 1;

    int b = 0;
    int a = n;
    while (a > 0)
    {
        a >>= 1;
        b++;
    };

    int mask = pow(2.0, b) - 1;
    int result = n ^ mask;
    return result;
}

2013年5月8日 星期三

Get Nth power of number.

Question:
Given two integer a and b (b >= 0), please write a function to return the result of "a to the power of b".

My Anwser: (I am no sure if it is correct.)
The easiest way is recursively multiply integer a.
int power(int a, int b)
{
    if (b <= 0)
        return 1;

    return a * power(a, b-1);
}


Question:
Improve time complexity to log(n)

My Anwser: (I am no sure if it is correct.)
Consider a to the power of 15 is power(a, 8) * power(a, 4) * power(a, 2) * power(a, 1) * power(a, 0)
#include "stdafx.h"
#include 

using namespace std;

int getPower(int a, int b);
int power(int a, int b);

int _tmain(int argc, _TCHAR* argv[])
{
    int a = 2, b = 15;
 
    cout << power(a, b) << endl;

    cin.get();
    return 0;
}

int getPower(int a, int logb)
{
    if (logb <= 0)
        return 1;

    return a * getPower(a*a, logb-1);
}

int power(int a, int b)
{
    if (b == 0)
        return 1;

    int logb = 0;
    while(b>0)
    {
        logb++;
        b >>= 1;
    }

    return getPower(a, logb);
}