.Net 加總 平均值 眾數 調和平均數 幾何平均數

只需將數字存入 List 帶入 Function 即可計算出結果。

        /// <summary>
        /// 平均值
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        static double AVG(List<double> val)
        {
            try
            {
                double _sum = SUM(val);
                if (_sum > 0 && val.Count > 0)
                {
                    double _avg = _sum / (double)val.Count;
                    return _avg;
                }
                else
                    return 0;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 加總
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        static double SUM(List<double> val)
        {
            try
            {
                if (val.Count > 0)
                    return (from g in val select g).Sum();
                else
                    return 0;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 眾數
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        static double Mode(List<double> val)
        {
            try
            {
                val.Sort();
                double num = val.First();
                int count = 0;
                double max_num = num;
                int max_count = 0;
                foreach (double v in val)
                {
                    if (v == num)
                        count++;
                    else
                    {
                        if (max_count < count)
                        {
                            max_num = num;
                            max_count = count;
                        }
                        num = v;
                        count = 1;
                    }
                }
                return max_num;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 調和平均數
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        static double HarmonicAverage(List<double> val)
        {
            try
            {
                int i = 0;
                double num = 0;
                foreach (double v in val)
                {
                    if (v > 0)
                    {
                        i++;
                        num += 1 / v;
                    }
                }
                if (i > 0 && num > 0)
                    return i / num;
                else
                    return 0;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 幾何平均數
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        static double GeometricMean(List<double> val)
        {
            try
            {
                int r = 0;
                double prod = 1;
                foreach (double v in val)
                {
                    if (prod * v > double.MaxValue)
                    {
                        prod = Math.Pow(prod, 1.0 / r);
                        r = 0;
                    }
                    r++;
                    prod *= v;
                }
                return Math.Pow(prod, 1.0 / r);
            }
            catch
            {
                return 0;
            }
        }

  2012-11-11      ez      .NET
.Net 計算標準差

只需將數字存入 List 帶入 Function 即可計算出結果。

        /// <summary> 
        /// 標準差(StandardDifference) 
        /// </summary> 
        /// <param name="val"></param> 
        /// <returns></returns> 
        public double SD(List<double> val)
        {
            if (val.Count > 1)
            {
                double avg = AVG(val);
                double _result = (from a in val select Math.Pow(a - avg, 2)).Sum();
                if (avg > 0 && _result > 0)
                {
                    double _sum = _result / (double)(val.Count - 1);
                    double _Sqrt = Math.Sqrt(_sum);
                    return _Sqrt;
                }
                else
                    return 0;
            }
            else if (val.Count == 1)
            {
                return 0;
            }
            else
            {
                return 0;
            }
        }

  2012-11-11      ez      .NET
.Net 利用 taskkill 或 tskill 關閉指定軟體

Windows 7 含以上支援 taskkill 指令,Windows XP 只支援 tskill 指令,所以這邊偷懶利用 try cache,當 taskkill 無法使用時換成 tskill。

利用指令關閉 iexplore.exe:

while (Process.GetProcessesByName("iexplore").Length > 0)
{
     Console.WriteLine("關閉瀏覽器");
     try {
          Process P = new Process();
          P.StartInfo.FileName = "taskkill";
          P.StartInfo.Arguments = "/F /IM iexplore.exe";
          P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
          P.StartInfo.CreateNoWindow = true;
          P.Start();
          P.WaitForExit();
     }
     catch {
          Process P = new Process();
          P.StartInfo.FileName = "tskill";
          P.StartInfo.Arguments = "iexplore.exe /A /V";
          P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
          P.StartInfo.CreateNoWindow = true;
          P.Start();
          P.WaitForExit();
     }
}

  2012-11-11      ez      .NET
.Net 序列化 及 反序列化 轉為 XML

首先加入Function

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        static T DoDeserialize<T>(string s)
        {
            XmlDocument XD = new XmlDocument();
            try
            {
                XD.LoadXml(s);
                XmlNodeReader XNR = new XmlNodeReader(XD.DocumentElement);
                XmlSerializer XS = new XmlSerializer(typeof(T));
                object obj = XS.Deserialize(XNR);
                return (T)obj;
            }
            catch
            {
                return default(T);
            }
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        static string DoSerialize(object o)
        {
            XmlSerializer XS = new XmlSerializer(o.GetType());
            StringBuilder SB = new StringBuilder();
            StringWriter SW = new StringWriter(SB);
            XS.Serialize(SW, o);
            return SB.ToString();
        }

  2012-11-11      ez      .NET
.Net Console 視窗 最上層顯示

可以利用 Win32 API 控制 Console 視窗 最大化 或 最小化。

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int uFlags);
private const int HWND_TOPMOST = -1;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOSIZE = 0x0001;

要控制視窗時只要呼叫:

IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;
SetWindowPos(hWnd, new IntPtr(HWND_TOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

  2012-11-11      ez      .NET
.Net 指定程式 視窗 最大化 最小化

可以利用 Win32 API 控制 Console 視窗 最大化 或 最小化。

[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
private enum CommandShow : int { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_NORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_MAXIMIZE = 3, SW_SHOWNOACTIVATE = 4, SW_SHOW = 5, SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_FORCEMINIMIZE = 11, SW_MAX = 11 } ;

將 iexplore.exe 視窗最大化:

Process[] p = Process.GetProcessesByName("iexplore");
if (p.Length > 0)
{
    for (int i = 0; i < p.Length; i++)
    {
        int hwnd = p[i].MainWindowHandle.ToInt32();
        ShowWindow(hwnd, (int)CommandShow.SW_NORMAL);
    }
}
p = null;

  2012-11-11      ez      .NET
.Net Console 視窗 最大化 最小化

可以利用 Win32 API 控制 Console 視窗 最大化 或 最小化。

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int uFlags);
private const int HWND_TOPMOST = -1;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOSIZE = 0x0001;

要控制視窗時只要呼叫:

ShowWindow(ThisConsole, MAXIMIZE);

  2012-11-11      ez      .NET
.Net 好用的 SquishIt 網頁檔案 優化 壓縮 CSS 及 JS (網頁優化)

.Net 可以使用 SquishIt 進行 CSS 及 JS 組合並壓縮,首先必須先下載檔案。

官方網址:http://www.codethinked.com/squishit-the-friendly-aspnet-javascript-and-css-squisher

本站下載:jetheredge-SquishIt-d9184c7

以往寫網頁會加入一堆 CSS 及 JS 如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="CSS/a.css" rel="stylesheet" type="text/css" />
    <link href="CSS/b.css" rel="stylesheet" type="text/css" />
    <link href="CSS/c.css" rel="stylesheet" type="text/css" />
    <link href="CSS/d.css" rel="stylesheet" type="text/css" />
    <link href="CSS/e.css" rel="stylesheet" type="text/css" />
    <script src="JS/a.js" type="text/javascript"></script>
    <script src="JS/b.js" type="text/javascript"></script>
    <script src="JS/c.js" type="text/javascript"></script>
    <script src="JS/d.js" type="text/javascript"></script>
    <script src="JS/e.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

當網頁讀取時需要載入多個 CSS 及 JS,這樣需要浪費較多的 Request 及頻寬,如下圖:

11次Request,約 295KB


  2012-08-28      ez      .NET
.Net 修改 Header 中的 Content-Type 資訊 (網頁優化)

如果檔案為 .Net 倒是很好處理,不過如果為 html 就沒辦法透過程式加上 Content-Type,所以此時只要修改 web.config 即可達到效果。

打開 web.config 進行編輯,在 <system.webServer> 下的 <staticContent> 裡加入以下:

      <remove fileExtension=".htm" />
      <mimeMap fileExtension=".htm" mimeType="text/html; charset=UTF-8" />
      <remove fileExtension=".html" />
      <mimeMap fileExtension=".html" mimeType="text/html; charset=UTF-8" />


  2012-08-24      ez      .NET