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); } } }
2014年8月28日
[C#]列舉(enum)的使用
Code:
[C#]排序方法比較
問題:
透過內建函式做排序真的會比較快嗎?
前言:
一般而言函式都是透過優化而產出的,所以理論上而言會是比較快。
但是一切都要透過驗證,所以做了以下實驗,透過一些方式做實驗發現...
Code:
迴圈比較快...
備註:
End為執行的時間,如果驗證方式有誤,麻煩不吝指教。
透過內建函式做排序真的會比較快嗎?
前言:
一般而言函式都是透過優化而產出的,所以理論上而言會是比較快。
但是一切都要透過驗證,所以做了以下實驗,透過一些方式做實驗發現...
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:
引數的傳遞方式?
參考:稍微靠靠腰
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); } } }結果:
訂閱:
文章 (Atom)