2013年9月29日 星期日

C#-陣列相加、相乘


題目:
           陣列相加、相乘(需使用double)。

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[,] a = { { 1.0, 2.3, 5.1 }, { 4.1, 0.2, 8.7 } }; // 宣告陣列A
            double[,] b = { { 0.1, 1.7, 8.5 }, { 6.1, 3.4, 7.2 } }; // 宣告陣列B
            double[,] d = { { 0.1, 6.1 }, { 1.7, 3.4 }, { 8.5, 7.2 } }; // 宣告陣列C

            matrixAdd(a, b); // 執行陣列相加函式
            Console.WriteLine("      "); // 空一行
            matrixMul(a, d); // 執行陣列相乘函式

            Console.ReadLine(); // 輸入,用來暫停程式
        }

        static void matrixAdd(double[,] a, double[,] b) // 相加函式
        {
            double[,] c = new double[a.GetLength(0), a.GetLength(1)]; // 宣告空陣列C(X向量為3,Y向量為2)
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    c[i, j] = a[i, j] + b[i, j]; // 陣列C=陣列A+陣列B
                    Console.WriteLine("c[" + i + "," + j + "]=" + c[i, j] + " "); // 印出陣列的值
                }
            }

        }

        static void matrixMul(double[,] a, double[,] b) // 相乘函式
        {
            double[,] c = new double[a.GetLength(0), b.GetLength(1)]; // 宣告空陣列C(X向量為2,Y向量為2)
            for (int i = 0; i < a.GetLength(0); i++)
            {
                for (int j = 0; j < a.GetLength(1); j++)
                {
                    c[i, i - i] += a[i, j] * b[j, i - i]; // 陣列C=陣列A與陣列B的內積
                    c[i, i - i + 1] += a[i, j] * b[j, i - i + 1];
                }  
                Console.WriteLine("c[" + i + "," + (i-i) + "]=" + c[i, i-i] + " "); // 印出陣列C[i,i-i]
                Console.WriteLine("c[" + i + "," + (i - i + 1) + "]=" + c[i, i - i + 1] + " "); // 印出陣列C[i,i-i+1]
             }
        }
    }
}

檔案:AddMultiply.cs

C#-內積


題目:

將兩向量做內積。


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] a = { 1.2, 2.5, 3.0, 4.7, 5.1 }; // 宣告陣列A
            double[] b = { 5.9, 4.4, 3.1, 2.3, 1.8 }; // 宣告陣列B

            innerProduct(a, b); // 執行內積函式

            Console.ReadLine(); // 輸入值,用來讓程式暫停
        }

        static void innerProduct(double[] a, double[] b)
        {
            int i; // 宣告變數 i
            double sum = 0; // 宣告變數 sum 為0 ,用來表示內積
            double[] c = new double[a.Length]; // 宣告空陣列C
            for (i = 0; i < a.Length; i++) // 從0開始、到陣列A的長度
            {
                c[i] = a[i] * b[i]; // 每個陣列C=A、B向量的相乘
                sum += c[i]; // 將每個陣列C的值相加
//                Console.WriteLine("c[i" + i + "]=" + c[i] + " "); // 顯示每個陣C的值
            }
            Console.WriteLine("內積=" + sum + " "); // 顯示內積
        }
    }
}

檔案: InnerProduct.cs

R-與誰最有緣

從班上學生隨機抽10000次,看看與誰最有緣。(本班有59人)

> x=sample(1:59, 10000, TRUE) // X樣本數從1到59,抽10000次,可放回重抽
> hist(x, breaks=0.5:60) // 顯示X樣本
結果我與14號最有緣。

R-感受中央極限定理


作業:
有100頂帽子,每次從中拿起一頂,可再放回,重複1000000次。

>x=sample(1:100, 1000000, T)  //X的樣本1000000次,每次從1到100中隨機選擇1個數字,T為可放回
>y=sample(1:100, 1000000, T)
>z=sample(1:100, 1000000, T)
>hist(x, breaks=0.5:101)  //  顯示X的可能 X斷點設為0.5,到101,最大為100.5

>hist(y, breaks=0.5:101)  // 顯示Y的可能

>hist(z, breaks=0.5:101)  // 顯示Z的可能

>hist(x+y, breaks=1.5:203) // 顯示X+Y的可能

>hist(x+y+z, breaks=2.5:305)  //顯示X+Y+Z的可能

2013年9月21日 星期六

C# 單位換算器

簡單的來說就是單位換算,因為Combobox的值需要因為Button切換而改變,故採用以下寫法。
// Bind combobox to dictionary
Dictionary<string, string>test = new Dictionary<string, string>();
        test.Add("1", "dfdfdf");
        test.Add("2", "dfdfdf");
        test.Add("3", "dfdfdf");
        comboBox1.DataSource = new BindingSource(test, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";

// Get combobox selection (in handler)
string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;

Debug過程:
1.float num_ori= Convert.ToDouble(txt.Text),無法一開始宣告,因為txt.Text一開始不存在,所以不宣告而採用在後面判斷時直接轉換。
2.Combobox的取值時機,在SelectedIndexChanged事件下,當選取長度、質量、容積,任一button時,SelectedIndex跟SelectedText的值就會觸發而帶入,所得到的值為0,以至於後續無法轉換;Click事件下,則是選取Combobox時,造成值的觸發,結果帶入一樣為0;SelectionChangeCommitted事件下,則是當確定選取完成時,SelectedIndex跟SelectedText的值才會帶入,故後面判斷才能進行。

用cmd執行 cd  Documents\Visual Studio 2010\Projects\unitconversion\unitconversion 到檔案所在的資料夾底下,再輸入 csc Form1.cs  Form1.Designer.cs Program.cs  ,最後打上 Program.exe執行。

以下附上一張執行成果


檔案連結:

參考資料:

ComboBox: Adding Text and Value to an Item