執行檔的連結
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace HW6
{
public partial class Form1 : Form
{
Label[] arr_lbl_Feature;
TextBox[] arr_txt_Feature;
ArrayList ObjectList = new ArrayList();
double[] DensityTable = { 2.70, 7.90, 8.90 }; // 鋁鐵銅密度表
/******** 建構子 ********/
public Form1()
{
InitializeComponent();
arr_lbl_Feature = new Label[] { lbl_feature1, lbl_feature2 };
arr_txt_Feature = new TextBox[] { txt_feature1, txt_feature2 };
}
/******** 控制項函數 ********/
private void cmo_ObjectSelect_SelectedIndexChanged(object sender, EventArgs e)
{
ChangeVisible();
}
private void btn_Add_Click(object sender, EventArgs e)
{
ShowResult("clear");
ShowResult("starting");
// 判斷是否可以將字串轉換成數字
Object ThisObject3D = null;
double[] TxtNumber = new double[arr_lbl_Feature.Length];
bool[] CanConvert = new bool[arr_lbl_Feature.Length];
for (int i = 0; i < arr_lbl_Feature.Length; i++)
{
CanConvert[i] = double.TryParse(arr_txt_Feature[i].Text, out TxtNumber[i]);
}
// 建立物件
switch (cmo_ObjectSelect.SelectedIndex)
{
case 0:
if (CanConvert[0])
{
ThisObject3D = Cube.Create(DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0]);
}
break;
case 1:
if (CanConvert[0])
{
ThisObject3D = Ball.Create(DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0]);
}
break;
case 2:
if (CanConvert[0] && CanConvert[1])
{
ThisObject3D = Cylinder.Create(DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0], TxtNumber[1]);
}
break;
case 3:
if (CanConvert[0] && CanConvert[1])
{
ThisObject3D = Pyramid.Create(DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0], TxtNumber[1]);
}
break;
default:
break;
}
// 顯示建立結果
if (ThisObject3D == null)
{
ShowResult("false");
}
else
{
ObjectList.Add(ThisObject3D);
ShowResult("OK");
}
}
private void btn_Show_Click(object sender, EventArgs e)
{
ShowResult("clear");
ShowResult("result");
}
/******** 與前端輸入輸出相關函數 ********/
bool[,] VisibleTable = { { true, false }, { true, false }, { true, true }, { true, true } };
string[,] arr_lbl_Feature_txt = { { "邊長", "" }, { "半徑", "" }, { "半徑", "高度" }, { "邊長", "高度" } };
private void ChangeVisible()
{
int Index = cmo_ObjectSelect.SelectedIndex;
for (int i = 0; i < arr_lbl_Feature.Length; i++)
{
arr_lbl_Feature[i].Visible = VisibleTable[Index, i];
arr_txt_Feature[i].Visible = VisibleTable[Index, i];
arr_lbl_Feature[i].Text = arr_lbl_Feature_txt[Index, i];
}
}
private void ShowResult(string s)
{
switch (s)
{
case "clear": // 清空
txt_Display.Text = "";
break;
case "starting": // 顯示建立中
txt_Display.AppendText("建立中..." + Environment.NewLine);
break;
case "false": // 顯示建立失敗資訊
txt_Display.AppendText("建立失敗,請重新確認參數!" + Environment.NewLine);
break;
case "OK" : // 顯示建立成功資訊
txt_Display.AppendText("建立成功!" + Environment.NewLine);
txt_Display.AppendText("已建立的物 體數量: "+ Convert.ToString(Object3D.Amount) + Environment.NewLine);
txt_Display.AppendText("已建立的正方體數量: " + Convert.ToString(Cube.Amount) + Environment.NewLine);
txt_Display.AppendText("已建立的球 體數量: " + Convert.ToString(Ball.Amount) + Environment.NewLine);
txt_Display.AppendText("已建立的圓柱體數量: " + Convert.ToString(Cylinder.Amount) + Environment.NewLine);
txt_Display.AppendText("已建立的金字塔數量: " + Convert.ToString(Pyramid.Amount) + Environment.NewLine);
break;
case "result": // 顯示結果
txt_Display.AppendText("物體類型 體積 質量" + Environment.NewLine);
txt_Display.AppendText("----------------------------------------" + Environment.NewLine);
Cube ThisCube;
Ball ThisBall;
Cylinder ThisCylinder;
Pyramid ThisPyramid;
foreach (var element in ObjectList)
{
if (element is Cube)
{
ThisCube = element as Cube;
txt_Display.AppendText("正方體 " + ThisCube.Volume().ToString("F3") + " " + ThisCube.Quality().ToString("F3") + Environment.NewLine);
}
else if (element is Ball)
{
ThisBall = element as Ball;
txt_Display.AppendText("球體 " + ThisBall.Volume().ToString("F3") + " " + ThisBall.Quality().ToString("F3") + Environment.NewLine);
}
else if (element is Cylinder)
{
ThisCylinder = element as Cylinder;
txt_Display.AppendText("圓柱體 " + ThisCylinder.Volume().ToString("F3") + " " + ThisCylinder.Quality().ToString("F3") + Environment.NewLine);
}
else if (element is Pyramid)
{
ThisPyramid = element as Pyramid;
txt_Display.AppendText("金字塔 " + ThisPyramid.Volume().ToString("F3") + " " + ThisPyramid.Quality().ToString("F3") + Environment.NewLine);
}
}
break;
default:
break;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW6
{
class Object3D
{
/******** 欄位 ********/
private double density = 0;
private static int amount = 0;
private string objectType = "Object3D";
/******** 屬性 ********/
public double Density // 密度
{
get
{
return density;
}
set
{
density = value;
}
}
public static int Amount // 數量
{
get
{
return amount;
}
}
public string ObjectType // 類型(讀)
{
get
{
return objectType;
}
}
protected string ObjectType_Write // 類型(寫)
{
set
{
objectType = value;
}
}
/******** 建構子 ********/
protected Object3D()
{
amount++;
}
protected Object3D(double density)
{
this.density = density;
amount++;
}
/******** 方法 ********/
public virtual double Volume() // 計算體積
{
return 0.0;
}
public double Quality() // 計算質量
{
return Density * Volume();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW6
{
class Cube : Object3D
{
/******** 欄位 ********/
private double side = 0;
private static int amount = 0;
/******** 屬性 ********/
public double Side // 邊長
{
get
{
return side;
}
set
{
if (value > 0)
{
side = value;
}
}
}
public new static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Cube( ) : base()
{
ObjectType_Write = "Cube";
amount++;
}
private Cube(double density, double side)
: base(density)
{
Side = side;
ObjectType_Write = "Cube";
amount++;
}
/******** 方法 ********/
public override double Volume()
{
return Side * Side * Side;
}
public static Cube Create(double density, double side)
{
if ((density < 0) || (side < 0))
{
return null;
}
else
{
return new Cube(density, side);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW6
{
class Ball : Object3D
{
/******** 欄位 ********/
private double radius = 0;
private static int amount = 0;
public const double pi = 3.1415926;
/******** 屬性 ********/
public double Radius // 邊長
{
get
{
return radius;
}
set
{
if (value > 0)
{
radius = value;
}
}
}
public new static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Ball()
: base()
{
ObjectType_Write = "Ball";
amount++;
}
private Ball(double density, double radius)
: base(density)
{
Radius = radius;
ObjectType_Write = "Ball";
amount++;
}
/******** 方法 ********/
public override double Volume()
{
return 4.0 * pi * Radius * Radius * Radius / 3.0;
}
public static Ball Create(double density, double radius)
{
if ((density < 0) || (radius < 0))
{
return null;
}
else
{
return new Ball(density, radius);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW6
{
class Cylinder : Object3D
{
/******** 欄位 ********/
private double radius = 0;
private double height = 0;
private static int amount = 0;
public const double pi = 3.1415926;
/******** 屬性 ********/
public double Radius // 半徑
{
get
{
return radius;
}
set
{
if (value > 0)
{
radius = value;
}
}
}
public double Height // 高度
{
get
{
return height;
}
set
{
if (value > 0)
{
height = value;
}
}
}
public new static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Cylinder()
: base()
{
ObjectType_Write = "Cylinder";
amount++;
}
private Cylinder(double density, double radius, double height)
: base(density)
{
Radius = radius;
Height = height;
ObjectType_Write = "Cylinder";
amount++;
}
/******** 方法 ********/
public override double Volume()
{
return 0.5 * pi * Radius * Radius * Height;
}
public static Cylinder Create(double density, double radius, double height)
{
if ((density < 0) || (radius < 0) || (height < 0))
{
return null;
}
else
{
return new Cylinder(density, radius, height);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW6
{
class Pyramid : Object3D
{
/******** 欄位 ********/
private double side = 0;
private double height = 0;
private static int amount = 0;
/******** 屬性 ********/
public double Side // 邊長
{
get
{
return side;
}
set
{
if (value > 0)
{
side = value;
}
}
}
public double Height // 高度
{
get
{
return height;
}
set
{
if (value > 0)
{
height = value;
}
}
}
public new static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Pyramid()
: base()
{
ObjectType_Write = "Pyramid";
amount++;
}
private Pyramid(double density, double side, double height)
: base(density)
{
Side = side;
Height = height;
ObjectType_Write = "Pyramid";
amount++;
}
/******** 方法 ********/
public override double Volume()
{
return Side * Side * Height / 3.0;
}
public static Pyramid Create(double density, double side, double height)
{
if ((density < 0) || (side < 0) || (height < 0))
{
return null;
}
else
{
return new Pyramid(density, side, height);
}
}
}
}
Show result 的處理方式不好, 用多型的方法來處理會比較好...
回覆刪除多型的課程將在星期五之前上網