目前分類:程式語言 (190)

瀏覽方式: 標題列表 簡短摘要

C# 的正規表達式 (Regular Expression)

文章標籤

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

C# 中的資料結構

文章標籤

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

C# 與檔案處理

簡介

C# 當中的檔案主要以串流 (Stream) 的形式呈現,串流讀取器 (StreamReader) 與串流寫入器 (StreamWriter) 是兩個主要的檔案處理類別。另外像 File,FileInfo, DirectoryInfo 等,則是用來存取檔案屬性與資料夾的類別。而BufferedStream、FileStream、MemoryStream、NetworkStream 則是分別對應到緩衝、檔案、記憶體、網路等類型的串流物件。因此串流可以說是檔案與網路的共同介面。

文章標籤

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
    interface Printable
    {
        void print();
    }
    abstract class Shape
    {
        public abstract double area();
    }
    class Rectangle : Shape, Printable
    {
        public double width, height;
        public Rectangle(double w, double h)
        {
            width = w;
            height = h;
        }
        public override double area()
        {
            return width * height;
        }
        public void print()
        {
            Console.WriteLine("Rectangle:width=" + width + " height=" + height 
                                    + " area()=" + area());
        }
    }
    class Circle : Shape, Printable
    {
        public double r;
        public Circle(double r)
        {
            this.r = r;
        }
        public override double area()
        {
            return 3.14 * r * r;
        }
        public void print()
        {
            Console.WriteLine("Circle:r=" + r + " area()=" + area());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r1 = new Rectangle(5.0, 3.0);
            r1.print();
            Rectangle r2 = new Rectangle(4.0, 6.0);
            r2.print();
            Circle c1 = new Circle(1.0);
            c1.print();
            Circle c2 = new Circle(2.0);
            c2.print();
            Shape s = r1;
            double a = s.area();
            double areaSum = 0.0;
            Shape[] sa = new Shape[] { r1, r2, c1, c2 };
            for (int i = 0; i < sa.GetLength(0); i++)
            {
                double area = sa[i].area();
                areaSum = areaSum + area;
            }
            Console.WriteLine("areaSum=" + areaSum);
            Printable[] pa = new Printable[] { r1, r2, c1, c2 };
            foreach (Printable p in pa)
            {
                p.print();
            }
        }
    }
}

文章轉載:陳鍾誠 (2011年10月04日),(網頁標題) 形狀、矩形與圓形與可列印物件 — 介面 (interface) 與封裝、繼承、多型的概念整合。,(網站標題) 免費電子書:C# 程式設計,2011年10月04日,取自 http://cs0.wikidot.com/printableobject ,網頁修改第 1 版。

文章標籤

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
    abstract class Shape
    {
        public abstract double area();
    }
    class Rectangle : Shape
    {
        public double width, height;
        public Rectangle(double w, double h)
        {
            width = w;
            height = h;
        }
        public override double area()
        {
            return width * height;
        }
        public void print()
        {
            Console.WriteLine("width=" + width + " height=" + height + " area()=" + area());
        }
    }
    class Circle : Shape
    {
        public double r;
        public Circle(double r)
        {
            this.r = r;
        }
        public override double area()
        {
            return 3.14 * r * r;
        }
        public void print()
        {
            Console.WriteLine("r=" + r + " area()=" + area());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r1 = new Rectangle(5.0, 3.0);
            r1.print();
            Rectangle r2 = new Rectangle(4.0, 6.0);
            r2.print();
            Circle c1 = new Circle(1.0);
            c1.print();
            Circle c2 = new Circle(2.0);
            c2.print();
            Shape s = r1;
            double a = s.area();
            double areaSum = 0.0;
            Shape[] sa = new Shape[] { r1, r2, c1, c2 };
            for (int i = 0; i < sa.GetLength(0); i++)
            {
                double area = sa[i].area();
                areaSum = areaSum + area;
            }
            Console.WriteLine("areaSum=" + areaSum);
        }
    }
}

文章轉載:陳鍾誠 (2011年10月04日),(網頁標題) 形狀、矩形與圓形 — 封裝、繼承、多型的概念整合。,(網站標題) 免費電子書:C# 程式設計,2011年10月04日,取自 http://cs0.wikidot.com/shapeobject ,網頁修改第 0 版。

文章標籤

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
    class Rectangle
    {
        public double width, height;
        public Rectangle(double w, double h)
        {
            width = w;
            height = h;
        }
        public double area()
        {
            return width * height;
        }
        public void print()
        {
            Console.WriteLine("width=" + width + " height=" + height + " area()=" + area());
        }
    }
    class Circle
    {
        public double r;
        public Circle(double r)
        {
            this.r = r;
        }
        public double area()
        {
            return 3.14 * r * r;
        }
        public void print()
        {
            Console.WriteLine("r=" + r + " area()=" + area());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(5.0, 3.0);
            r.print();
            Rectangle r2 = new Rectangle(4.0, 6.0);
            r2.print();
            Circle c1 = new Circle(1.0);
            c1.print();
            Circle c2 = new Circle(2.0);
            c2.print();
        }
    }
}

文章轉載:陳鍾誠 (2011年10月04日),(網頁標題) 矩形與圓形物件,(網站標題) 免費電子書:C# 程式設計,2011年10月04日,取自 http://cs0.wikidot.com/circleobject ,網頁修改第 1 版。

文章標籤

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ObjectTest
{
    class Rectangle
    {
        public double width, height;
        public Rectangle(double w, double h)
        {
            width = w;
            height = h;
        }
        public double area()
        {
            return width * height;
        }
        public void print()
        {
            Console.WriteLine("width=" + width + " height=" + height + " area()=" + area());
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(5.0, 3.0);
            r.print();
            Rectangle r2 = new Rectangle(4.0, 6.0);
            r2.print();
        }
    }
}

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

文章標籤

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

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:加上內積的函數,並寫出呼叫範例。

文章標籤

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

物件導向的多型機制,是指當兩個以上的類別繼承同一種父類別時,我們可以用父類別型態容納子類別的物件,真正進行函數呼叫時會呼叫到子類別的函數,此種特性稱之為多型。

文章標籤

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

範例一:矩形、圓形繼承形狀 (Shape)

using System;
class Shape
{
    public virtual double area() { return 0.0; }
}
class Rectangle : Shape
{
    double width, height;
    public Rectangle(double w, double h)
    {
        width = w;
        height = h;
    }
    public override double area()
    {
        return width * height;
    }
}
class Circle : Shape
{
    double r;
    public Circle(double r)
    {
        this.r = r;
    }
    public override double area()
    {
        return 3.1416 * r * r;
    }
}
class Test
{
    public static void Main(string[] args)
    {
        Shape s = new Shape();
        Console.WriteLine("s.area() = " + s.area());
        Rectangle r = new Rectangle(5.0, 8.0);
        Console.WriteLine("r.area() = " + r.area());
        Circle c = new Circle(2);
        Console.WriteLine("c.area() = " + c.area());
    }
}
        
文章標籤

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

習題一:向量物件

文章標籤

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

簡介

C# 是很好的物件導向語言,而且微軟的 .NET Framework 函式庫設計得相當優美,這使得 C# 的魅力相當大。

文章標籤

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

專案下載:WError.zip

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WError
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double x = double.Parse(textBox1.Text);
                double y = double.Parse(textBox2.Text);
                String op = comboBox1.Text;
                double result = 0.0;
                switch (op)
                {
                    case "+": result = x + y; break;
                    case "-": result = x - y; break;
                    case "*": result = x * y; break;
                    case "/": result = x / y; break;
                    default: throw new Exception("出現錯誤!");
                }
                textBox3.Text = result.ToString();
            }
            catch
            {
                textBox3.Text = "錯誤";
            }
        }
    }
}
        
文章標籤

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

簡介

C# 支援例外處理機制,當有任何的例外錯誤發生時,程式會立刻中斷,然後跳出到外層。此時,如果有任何例外處理的程式 (try … catch) 位於外層,就會接到這個例外,並可以即時處理之。否則,該例外會一直被往外丟,假如都沒有被處理,則程式將被迫中斷,系統會自行輸出例外訊息。

文章標籤

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

簡介

C# 分為靜態函數與成員函數兩類,靜態函數附屬於類別,呼叫時可以直接指定類別名稱即可。成員函數附屬於物件,呼叫時必須透過物件變數進行呼叫。

文章標籤

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

習題:陣列練習題 — http://cs0.wikidot.com/arrayex

文章標籤

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

簡介

在結構化的程式語言中,流程控制是以判斷 (if, switch) 與迴圈 (for, while) 為主的。C# 也不例外,我們將在本文中介紹 C# 的流程控制語法,包含如何利用條件判斷語法控制程式的分支情況,以及用迴圈語法重複運行某些程式碼。

C# 中的條件判斷語法

C# 的條件判斷以 if 為主,語法完全繼承 C 語言的語法,其語法如下:

文章標籤

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

簡介

以下是一個完整的 C# 程式 (Hello.cs),可以印出 Hello !。

using System;                           // 引用 System 函式庫
public class Hello                      // 宣告類別 Hello
{
   public static void Main()            // 類別 Hello 的主程式
   {
      Console.WriteLine("Hello !");     // 印出 Hello !
   }
}
        
文章標籤

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

簡介

C# 語言是微軟 (Microsoft) 所主導設計出來的,主要的開發工具是微軟的 Visual Studio。Visual Studio 是一個功能很強大的整合開發環境,包含視覺化設計模式、整合除錯環境、方便的程式編輯環境等等。因此,大部分人都會使用 Visual Studio 作為 C# 的開發工具。

目前 Visual Studio 可分為三個版本,企業版 (Enterprise Edition)、專業版 (Professional Edition) 以及簡潔版 (Express Edition),其中企業版與專業版是需要付費購買的,而簡潔版則是免費的。通常一般的學習者使用簡潔版就綽綽有餘了,簡潔版的功能其實已經相當強大,除了缺少一些系統分析、團隊合作、以及企業報表等工具之外,其他的部分,像是視覺化設計模式、整合除錯環境、方便的程式編輯環境等,都已經包括在內,因此相當適合學生學習使用。

文章標籤

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

C# 是微軟所設計的一種物件導向語言,期設計理念受到 C 與 Java 語言的影響,採用類似 C 語言的語法,並使用類似 Java 語言的虛擬機架構,具備物件導向的能力,是微軟在其主力平台 .NET 上最重要的開發語言。

要使用 C# 開發程式,必須安裝 Visual C# Express 或是 Visual Studio,其中 Visual C# Express 是免費的,您可以下列網址當中下載其安裝程式。

文章標籤

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