using System;
class Vector
{
    double[] a;
    public Vector(double[] array)
    {
        a = new double[array.GetLength(0)];
        for (int i = 0; i < a.GetLength(0); i++)
        {
            a[i] = array[i];
        }
    }
    public Vector add(Vector v2)
    {
        Vector rv = new Vector(v2.a);
        for (int i = 0; i < rv.a.GetLength(0); i++)
        {
            rv.a[i] = this.a[i] + v2.a[i];
        }
        return rv;
    }
    public void print()
    {
        for (int i = 0; i < a.GetLength(0); i++)
        {
            Console.Write(a[i] + " ");
        }
        Console.WriteLine();
    }
}
class Test
{
    public static void Main(string[] args)
    {
        Vector v1 = new Vector(new double[] { 1.0, 2.0, 3.0 });
        Vector v2 = new Vector(new double[] { 4.0, 5.0, 6.0 });
        Vector v3 = v1.add(v2);
        v1.print();
        v2.print();
        v3.print();
    }
}

習題1:加上內積的函數,並寫出呼叫範例。
習題2:寫出矩陣物件 (有加法、乘法) (方法一:用二維陣列、方法二:用 Vector)。

文章轉載:陳鍾誠 (2011年09月27日),(網頁標題) 向量物件,(網站標題) 免費電子書:C# 程式設計,2011年09月27日,取自 http://cs0.wikidot.com/vectorobject ,網頁修改第 0 版。

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()