2014年8月28日

[C#]列舉(enum)的使用

Code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum Shape : int { Circle=0, Line=1, Arc=2 } private void Draw(int vType) { Graphics g; Pen p = new Pen(Color.Red); g = this.CreateGraphics(); g.Clear(Color.White); switch (vType) { case 0: g.DrawEllipse(p, 90, 30, 90, 90); break; case 1: g.DrawLine(p, 90, 50, 180, 100); break; case 2: g.DrawArc(p, 90, 30, 90, 90, 0, 250); break; default: MessageBox.Show("沒這個喔"); break; } } private void Draw(Shape vType) { Graphics g; Pen p = new Pen(Color.Red); g = this.CreateGraphics(); g.Clear(Color.White); switch (vType) { case Shape.Circle: g.DrawEllipse(p, 60, 30, 90, 90); break; case Shape.Line: g.DrawLine(p, 60, 50, 180, 100); break; case Shape.Arc: g.DrawArc(p, 60, 30, 90, 90, 0, 250); break; default: MessageBox.Show("沒這個喔"); break; } } private void BTN_Circle_Click(object sender, EventArgs e) { //Draw(0); Draw(Shape.Circle); } private void BTN_LINE_Click(object sender, EventArgs e) { //Draw(1); Draw(Shape.Line); } private void BTN_Arc_Click(object sender, EventArgs e) { //Draw(2); Draw(Shape.Arc); } } }

[C#]排序方法比較

問題:
透過內建函式做排序真的會比較快嗎?

前言:
一般而言函式都是透過優化而產出的,所以理論上而言會是比較快。
但是一切都要透過驗證,所以做了以下實驗,透過一些方式做實驗發現...

Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int[] arrayMax = new int[10000000]; int[] arrayMax2 = new int[10000000]; int[] arrayMax3 = new int[10000000]; for (int i = 0; i <= arrayMax.GetUpperBound(0); i++) { Random rnd = new Random(); arrayMax[i] = rnd.Next(); arrayMax2[i] = arrayMax[i]; arrayMax3[i] = arrayMax[i]; } /* * 方法一,use sort */ DateTime start2 = DateTime.Now; Console.WriteLine("Start:" + start2.ToString() + "\n"); Array.Sort(arrayMax2); Console.WriteLine("Max is:" + arrayMax2[arrayMax2.Length-1] + "\n"); Console.WriteLine("End:" + DateTime.Now.Subtract(start2) + "\n"); Console.WriteLine("*** next one ***\n"); /* * 方法二,迴圈尋找 */ DateTime start = DateTime.Now; Console.WriteLine("Start:" + start.ToString() + "\n"); Console.WriteLine("Max is:{0}\n", GetMax(ref arrayMax)); Console.WriteLine("End:" + DateTime.Now.Subtract(start) + "\n"); /* * 方法三,use sort and Reverse */ DateTime start3 = DateTime.Now; Console.WriteLine("Start:" + start3.ToString() + "\n"); Array.Sort(arrayMax3); Array.Reverse(arrayMax3); Console.WriteLine("Max is:" + arrayMax3[0] + "\n"); Console.WriteLine("End:" + DateTime.Now.Subtract(start3) + "\n"); Console.WriteLine("*** next one ***\n"); Console.Read(); } private static int GetMax(ref int[] arrayMax) { int i, max; max = arrayMax[0]; for (i = 0; i <= arrayMax.GetUpperBound(0); i++) { if (max < arrayMax[i]) { max = arrayMax[i]; } } return max; } } }
結果:
迴圈比較快...

備註:
End為執行的時間,如果驗證方式有誤,麻煩不吝指教。

[C#]引數的傳遞方式

問題:
引數的傳遞方式?
參考:稍微靠靠腰

Code:
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\n *** 一般函式傳遞 *** \n");
            int a = 10, b = 20;
            
            Console.WriteLine("\n 未進入函式前\t\t:a={0} \t b={1}", a, b);
           
            CallValues(a, b);
            Console.WriteLine("\n 進入函式後\t\t:a={0} \t b={1}", a, b);

            CallValuesUseRef(ref a, ref b);
            Console.WriteLine("\n 進入函式後\t\t:a={0} \t b={1}", a, b);

            int initX, initY;
            Console.WriteLine("\n 進入函式前\t\t:initX,initY 沒有初始值");
            CallValuesUseOut(out initX, out initY);
            Console.WriteLine("\n 進入函式後\t\t:initX={0} \t initY={1}", initX, initY);

            Console.Read();
        }

        private static void CallValuesUseOut(out int x, out int y)
        {
            int z;
            x = 20;
            y = 30;
            Console.WriteLine("\n 函式內 交換前\t\t:x={0} \t y={1}", x, y);

            z = x;
            x = y;
            y = z;

            Console.WriteLine("\n 函式內 交換後\t\t:x={0} \t y={1}", x, y);

        }

        private static void CallValuesUseRef(ref int x, ref int y)
        {
            int z;
            x = 20;
            y = 30;
            Console.WriteLine("\n 函式內 交換前\t\t:x={0} \t y={1}", x, y);

            z = x;
            x = y;
            y = z;

            Console.WriteLine("\n 函式內 交換後\t\t:x={0} \t y={1}", x, y);
        }

        private static void CallValues(int x, int y)
        {
            int z;
            x = 20;
            y = 30;
            Console.WriteLine("\n函式內 交換前\t\t:x={0} \t y={1}", x, y);

            z = x;
            x = y;
            y = z;

            Console.WriteLine("\n函式內 交換後\t\t:x={0} \t y={1}", x, y);
        }
    }
}


結果:


2013年12月6日

[Android]使用BlueStacks、Eclipse進行開發除錯




前言


使用Android模擬器進行開發與除錯時,Windows作業系統上的執行時間會比較長。
對於某些專案在分秒必爭的情況下,模擬器反而造成許多開發上的不便。

解決方案

使用BlueStacks與Eclipse進行開發與除錯。


關於


  1. BlueStacks:一套可以模擬一般Android手機或平板APP正常執行的軟體,相信許多人都使用這個軟體來玩一些APP遊戲,如神魔之塔...等。
  2. Eclipse:一套用java開發的好用程式編輯軟體。(作者就是使用此軟體來開發Android的)

 如何使用

  1. 先進行BlueStacks 及 Eclipse安裝,並且都順利安中與執行。
  2. 打開Eclipse的Device(查詢目前有哪些裝置在線上),如圖1。開啟位置:Windows>Show View>Others>Device。
    圖1.Eclpse的Device列表
如果沒有找到裝置,請重新啟動adb,如圖2。
圖2.Restart adb

2013年8月3日

[SVN]建立版本管理工具

前言

最近在執行專案時,一直在思考管理版本問題。起初因為一人建立專案,所以有自己的一套管理方式。但是如果多人專案就需要專業的工具啦。

關於SVN

Apache Subversion(簡稱SVN,svn),是一個開放原始碼的版本控制系統,相對於的RCS、CVS,採用了分支管理系統,它的設計目標就是取代CVS。網際網路上越來越多的控制服務從CVS轉移到Subversion。

參考:Subversion(SVN)概念與工具介紹

安裝SVN時,需要一個Server(伺服端)跟一個Client(客戶端)。
筆者一直失敗就是因為只安裝Client,所以一直上傳失敗。...夠蠢的

安裝SVN Server(作者環境為 Windows 7)

Server此部份就是將大家的心血(檔案)存放的地方,因為作者也是參考其它人的安裝方式安裝成功,因此附上幾個連結讓大家參考。

安裝部份:Demo 小舖
軟體下載:VISUALSVN SERVER
Port 參考(如果發生Port衝突):通訊埠 (port) 介紹及常用 port 對照

注意:作者在安裝SVN前已經有安裝了XAMPP(整合Apache, MySQL, PHP....)
因此在安裝到Server Port時,Port 443發生衝突,因此更換了其它的Port。


安裝SVN Client

本作者使用TortoiseSVN作為上傳工具,因為他比較.....視覺化吧。
看個人喜好囉。

安裝與操作:版本控制工具TortoiseSVN初體驗

2013年7月16日

關於Appserv 漏洞一事(一)

事由:
今天研究室發生了駭客攻擊,有一台電腦變成了殭屍。
(還好不會咬人)
原因:
電腦的AppServ 2.5.10被竄改,導致一直對外發送封包(典型的DDos攻擊)

經過:
早上電腦使用者反應網路連不上,發現是連線速度異常。後來網路組人員通知,有異常流量從此電腦IP發出。因此先IP封鎖,拔除網路線。

解決方法:
早上電腦使用者反應網路連不上,發現是連線速度異常。後來網路組人員通知,有異常流量從此電腦IP發出。因此先IP封鎖,拔除網路線。

參考:
如何預防Appserv遭受入侵
資訊組蔡玉貴(friber)部落格日誌_appserv程式漏洞_首頁被竄改

最後學弟選擇安裝了其它的軟體:XAMPP,線上文件說明都還滿完整的。
但是安裝完後沒有繁體中文,最後發現語言檔只有簡體中文。
本想一鼓作氣將他翻譯完,拿來造福人群。
但是網路的東西太過強大,所以上網找到人家翻好的繁體中文檔
只能說世界上好心人還是有的。

(待續)

2013年6月29日

質數計算機



不知道質數為何物嗎?質數有多大呢?我們來看看你電腦的效能,可以計算多少質數囉。

(你會發現,每使用一次,計算速度越會來越快喔。)







關於「質數」


質數,指在一個大於1的自然數中,除了1和此整數自身外,無法被其他自然數整除的數。

by 維基百科