C# 的正規表達式 (Regular Expression)
目前分類:程式語言 (190)
- Nov 10 Tue 2015 20:24
C# 的正規表達式 (Regular Expression)
- Nov 10 Tue 2015 20:17
C# 與檔案處理
C# 與檔案處理
簡介
C# 當中的檔案主要以串流 (Stream) 的形式呈現,串流讀取器 (StreamReader) 與串流寫入器 (StreamWriter) 是兩個主要的檔案處理類別。另外像 File,FileInfo, DirectoryInfo 等,則是用來存取檔案屬性與資料夾的類別。而BufferedStream、FileStream、MemoryStream、NetworkStream 則是分別對應到緩衝、檔案、記憶體、網路等類型的串流物件。因此串流可以說是檔案與網路的共同介面。
- Nov 05 Thu 2015 09:49
物件導向程式設計 -- 使用 C# 語言-C# 形狀、矩形與圓形與可列印物件 — 介面 (interface) 與封裝、繼承、多型的概念整合
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 版。
- Nov 05 Thu 2015 09:45
物件導向程式設計 -- 使用 C# 語言-C# 形狀、矩形與圓形 — 封裝、繼承、多型的概念整合
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 版。
- Nov 05 Thu 2015 09:44
物件導向程式設計 -- 使用 C# 語言-C# 矩形與圓形物件
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 版。
- Nov 05 Thu 2015 09:41
物件導向程式設計 -- 使用 C# 語言-C# 矩形物件
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 版。
- Nov 05 Thu 2015 09:38
物件導向程式設計 -- 使用 C# 語言-C# 向量物件
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:加上內積的函數,並寫出呼叫範例。
- Nov 05 Thu 2015 09:34
物件導向程式設計 -- 使用 C# 語言-C# 與物件導向中的多型技術 (Polymorphism)
物件導向的多型機制,是指當兩個以上的類別繼承同一種父類別時,我們可以用父類別型態容納子類別的物件,真正進行函數呼叫時會呼叫到子類別的函數,此種特性稱之為多型。
- Nov 05 Thu 2015 09:32
物件導向程式設計 -- 使用 C# 語言-C# 與物件導向的繼承 (Inheritance)
範例一:矩形、圓形繼承形狀 (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());
}
}
- Nov 05 Thu 2015 09:29
物件導向程式設計 -- 使用 C# 語言-C# 語言中的物件封裝 (Encapsulation)
- Nov 05 Thu 2015 09:26
物件導向程式設計 -- 使用 C# 語言-C# 的物件
- Nov 04 Wed 2015 17:15
C# 的例外處理-2.視窗版範例 — 一個可以進行加減乘除的小計算機。
專案下載: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 = "錯誤";
}
}
}
}
- Nov 04 Wed 2015 17:06
C# 的例外處理-1.命令列範例 — 例外處理的 try catch 語法
簡介
C# 支援例外處理機制,當有任何的例外錯誤發生時,程式會立刻中斷,然後跳出到外層。此時,如果有任何例外處理的程式 (try … catch) 位於外層,就會接到這個例外,並可以即時處理之。否則,該例外會一直被往外丟,假如都沒有被處理,則程式將被迫中斷,系統會自行輸出例外訊息。
- Nov 04 Wed 2015 17:01
C# 的函數
- Nov 04 Wed 2015 16:56
C# 的陣列
習題:陣列練習題 — http://cs0.wikidot.com/arrayex
- Nov 04 Wed 2015 16:53
C# 的流程控制
簡介
在結構化的程式語言中,流程控制是以判斷 (if, switch) 與迴圈 (for, while) 為主的。C# 也不例外,我們將在本文中介紹 C# 的流程控制語法,包含如何利用條件判斷語法控制程式的分支情況,以及用迴圈語法重複運行某些程式碼。
C# 中的條件判斷語法
C# 的條件判斷以 if 為主,語法完全繼承 C 語言的語法,其語法如下:
- Nov 04 Wed 2015 16:51
C# 的變數與運算
簡介
以下是一個完整的 C# 程式 (Hello.cs),可以印出 Hello !。
using System; // 引用 System 函式庫
public class Hello // 宣告類別 Hello
{
public static void Main() // 類別 Hello 的主程式
{
Console.WriteLine("Hello !"); // 印出 Hello !
}
}
- Nov 04 Wed 2015 16:49
C# 的開發環境
簡介
C# 語言是微軟 (Microsoft) 所主導設計出來的,主要的開發工具是微軟的 Visual Studio。Visual Studio 是一個功能很強大的整合開發環境,包含視覺化設計模式、整合除錯環境、方便的程式編輯環境等等。因此,大部分人都會使用 Visual Studio 作為 C# 的開發工具。
目前 Visual Studio 可分為三個版本,企業版 (Enterprise Edition)、專業版 (Professional Edition) 以及簡潔版 (Express Edition),其中企業版與專業版是需要付費購買的,而簡潔版則是免費的。通常一般的學習者使用簡潔版就綽綽有餘了,簡潔版的功能其實已經相當強大,除了缺少一些系統分析、團隊合作、以及企業報表等工具之外,其他的部分,像是視覺化設計模式、整合除錯環境、方便的程式編輯環境等,都已經包括在內,因此相當適合學生學習使用。