
執行檔的連結
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 HW7
{
public partial class Form1 : Form
{
// Member variables
private Label[] lbl_ArrayFeature;
private TextBox[] txt_ArrayFeature;
private List Shape3DList = new List();
private int CreatedOrder = 0;
// Constructors & Finalizers
public Form1()
{
InitializeComponent();
lbl_ArrayFeature = new Label[] { lbl_Feature1, lbl_Feature2 };
txt_ArrayFeature = new TextBox[] { txt_Feature1, txt_Feature2 };
}
// Methods (Controls)
private void cmo_ObjectSelect_SelectedIndexChanged(object sender, EventArgs e) // 改變顯示的輸入欄位
{
ChangeVisible();
}
private void btn_Add_Click(object sender, EventArgs e) // 加入
{
// 判斷是否可以將字串轉換成數字
double[] TxtNumber = new double[lbl_ArrayFeature.Length];
bool[] CanConvert = new bool[lbl_ArrayFeature.Length];
bool CanCreate = false;
for (int i = 0; i < lbl_ArrayFeature.Length; i++)
{
CanConvert[i] = double.TryParse(txt_ArrayFeature[i].Text, out TxtNumber[i]);
}
// 建立物件
switch (cmo_Object.SelectedIndex)
{
case 0:
if (CanConvert[0])
{
Cube ThisCube = Cube.Create(GeoConst.DensityTable[cmo_Material.SelectedIndex],TxtNumber[0]);
if (ThisCube != null)
{
// 若使用Shape3DList.Count會有bug!
ThisCube.Order = CreatedOrder;
Shape3DList.Add(ThisCube);
CanCreate = true;
}
}
break;
case 1:
if (CanConvert[0])
{
Ball ThisBall = Ball.Create(GeoConst.DensityTable[cmo_Material.SelectedIndex],TxtNumber[0]);
if (ThisBall != null)
{
ThisBall.Order = CreatedOrder;
Shape3DList.Add(ThisBall);
CanCreate = true;
}
}
break;
case 2:
if (CanConvert[0] && CanConvert[1])
{
Cylinder ThisCylinder = Cylinder.Create(GeoConst.DensityTable[cmo_Material.SelectedIndex],TxtNumber[0],TxtNumber[1]);
if (ThisCylinder != null)
{
ThisCylinder.Order = CreatedOrder;
Shape3DList.Add(ThisCylinder);
CanCreate = true;
}
}
break;
case 3:
if (CanConvert[0] && CanConvert[1])
{
Pyramid ThisPyramid = Pyramid.Create(GeoConst.DensityTable[cmo_Material.SelectedIndex],TxtNumber[0],TxtNumber[1]);
if (ThisPyramid != null)
{
ThisPyramid.Order = CreatedOrder;
Shape3DList.Add(ThisPyramid);
CanCreate = true;
}
}
break;
default:
break;
}
// 顯示建立結果
if (CanCreate)
{
CreatedOrder++;
ShowResult("Number");
ShowResult("CreateOk");
}
else
{
ShowResult("CreateFalse");
}
}
private void btn_Remove_Click(object sender, EventArgs e) // 移除
{
int RemoveNumber = 0;
bool HasRemove = false;
bool CanConvert = int.TryParse(txt_RemoveNumber.Text, out RemoveNumber);
int CountBeforeRemove = Shape3DList.Count;
if (CanConvert) // 如果能將字串轉數字才進行
{
if ((RemoveNumber < Shape3DList.Count) && (RemoveNumber >= 0))
{
Shape3DList.RemoveAt(RemoveNumber);
GC.Collect();
GC.WaitForPendingFinalizers();
if (Shape3DList.Count < CountBeforeRemove) // double check
{
HasRemove = true;
}
}
}
if (HasRemove)
{
ShowResult("RemoveOk");
ShowResult("Number");
}
else
{
ShowResult("RemoveFalse");
}
}
private void btn_Roll_Click(object sender, EventArgs e) // 顯示滾動距離
{
ShowResult("RollingDis");
}
private void btn_Show_Click(object sender, EventArgs e) // 顯示質量與體積
{
ShowResult("Mass&Volume");
}
private void btn_Sort_Click(object sender, EventArgs e) // 排序
{
switch (cmb_SortBy.SelectedIndex) // 選擇排序方法
{
case 0:
Shape3DList.Sort(CompareByOrder);
break;
case 1:
Shape3DList.Sort(CompareByShape);
break;
case 2:
Shape3DList.Sort(CompareByMaterial);
break;
case 3:
Shape3DList.Sort(CompareByVolume);
break;
case 4:
Shape3DList.Sort(CompareByMass);
break;
default:
break;
}
}
// Methods (For Sort)
private static int CompareByOrder(Shape3D S1, Shape3D S2)
{
if (S1.Order > S2.Order)
{
return 1;
}
else
{
return -1;
}
}
private static int CompareByShape(Shape3D S1, Shape3D S2)
{
if ((int)S1.Shape > (int)S2.Shape)
{
return 1;
}
else
{
return -1;
}
}
private static int CompareByMaterial(Shape3D S1, Shape3D S2)
{
if (S1.Density > S2.Density)
{
return 1;
}
else
{
return -1;
}
}
private static int CompareByVolume(Shape3D S1, Shape3D S2)
{
if (S1.CalculateVolume() > S2.CalculateVolume())
{
return 1;
}
else
{
return -1;
}
}
private static int CompareByMass(Shape3D S1, Shape3D S2)
{
if (S1.CalculateMass() > S2.CalculateMass())
{
return 1;
}
else
{
return -1;
}
}
// Methods (Front Panel I/O)
bool[,] VisibleTable = { { true, false },
{ true, false },
{ true, true },
{ true, true } };
string[,] FeatureText = { { "邊長", "" },
{ "半徑", "" },
{ "半徑", "高度" },
{ "邊長", "高度" } };
private void ChangeVisible()
{
int Index = cmo_Object.SelectedIndex;
for (int i = 0; i < lbl_ArrayFeature.Length; i++)
{
lbl_ArrayFeature[i].Visible = VisibleTable[Index, i];
txt_ArrayFeature[i].Visible = VisibleTable[Index, i];
lbl_ArrayFeature[i].Text = FeatureText[Index, i];
}
}
private void ShowResult(string DoWhat)
{
StringBuilder sb = new StringBuilder();
switch (DoWhat)
{
case "CreateFalse": // 顯示建立失敗資訊
sb.AppendLine("建立失敗,請重新確認參數!");
txt_Display.AppendText(sb.ToString());
break;
case "CreateOk" : // 顯示建立成功資訊
sb.AppendLine(Shape3DList[Shape3DList.Count - 1].ShowParameter());
txt_Display.AppendText(sb.ToString());
break;
case "Mass&Volume": // 顯示質量與體積
foreach (var element in Shape3DList)
{
sb.Append(element.ShowParameter());
sb.AppendLine(element.ShowMassAndVolome());
}
txt_MassVol.Text = sb.ToString();
break;
case "Number": // 顯示數量
sb.Append("Cube\t");
sb.Append("Ball\t");
sb.Append("Cylinder\t");
sb.Append("Pyramid\t");
sb.AppendLine("Total");
sb.Append(Cube.Amount.ToString());
sb.Append("\t");
sb.Append(Ball.Amount.ToString());
sb.Append("\t");
sb.Append(Cylinder.Amount.ToString());
sb.Append("\t");
sb.Append(Pyramid.Amount.ToString());
sb.Append("\t");
sb.Append(Shape3D.Amount.ToString());
sb.AppendLine("\t");
txt_Amount.Text = sb.ToString();
break;
case "RollingDis": // 顯示滾動距離
foreach (var element in Shape3DList)
{
IRollable Temp = element as IRollable;
if (Temp != null)
{
sb.Append(element.ShowParameter());
sb.AppendLine(Temp.CalculateRollingDistance().ToString("F2"));
}
}
txt_Roll.Text = sb.ToString();
break;
case "RemoveOk": // 顯示移除成功資訊
sb.Append("已移除第 ");
sb.Append(txt_RemoveNumber.Text);
sb.AppendLine(" 個物件!");
txt_Display.AppendText(sb.ToString());
break;
case "RemoveFalse": // 顯示移除失敗資訊
sb.AppendLine("移除失敗,請重新確認參數與數量!");
txt_Display.AppendText(sb.ToString());
break;
default:
break;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
abstract class Shape3D
{
// Member variables
private double _density = 0;
private static int _amount = 0;
private int _order = 0;
private Shape3DType _shape = Shape3DType.Unknown;
// Constructors & Finalizers
public Shape3D()
{
Amount++;
}
public Shape3D(double density)
{
Density = density;
Amount++;
}
~Shape3D()
{
Amount--;
}
// Nested Enums, Structs, and Classes.
public enum Shape3DType { Unknown = 0, Ball, Cube, Cylinder, Pyramid };
// Properties
public double Density // 密度
{
get
{
return _density;
}
set
{
_density = value;
}
}
public static int Amount // 數量
{
get
{
return _amount;
}
private set
{
_amount = value;
}
}
public int Order // 創建順序的編號
{
get
{
return _order;
}
set
{
_order = value;
}
}
public Shape3DType Shape // 物件型別
{
get
{
return _shape;
}
protected set
{
_shape = value;
}
}
// Methods
public double CalculateMass() // 計算質量
{
return Density * CalculateVolume();
}
public abstract double CalculateVolume(); // 計算體積
public string ShowMassAndVolome() // 顯示質量與體積
{
string s = "";
s += System.String.Format("{0:F2}", CalculateVolume());
s += '\t';
s += System.String.Format("{0:F2}", CalculateMass());
s += '\t';
return s;
}
public abstract string ShowParameter(); // 顯示參數
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Cube : Shape3D
{
// Member variables
private double _side = 0;
private static int _amount = 0;
// Constructors & Finalizers
public Cube()
: base()
{
Shape = Shape3DType.Cube;
Amount++;
}
private Cube(double density, double side)
: base(density)
{
Side = side;
Shape = Shape3DType.Cube;
Amount++;
}
~Cube()
{
Amount--;
}
// Properties
public double Side // 邊長
{
get
{
return _side;
}
set
{
if (value > 0)
{
_side = value;
}
}
}
public new static int Amount // 數量
{
get
{
return _amount;
}
private set
{
_amount = value;
}
}
// Methods
public override double CalculateVolume() // 計算體積
{
return Side * Side * Side;
}
public override string ShowParameter() // 顯示參數
{
StringBuilder sb = new StringBuilder();
sb.Append("Cube");
sb.Append('\t');
sb.Append('\t');
sb.Append(Side.ToString("F2"));
sb.Append('\t');
sb.Append('\t');
sb.Append(Density.ToString("F2"));
sb.Append('\t');
return sb.ToString();
}
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 HW7
{
class Ball : Shape3D, IRollable
{
// Member variables
private double _radius = 0;
private static int _amount = 0;
// Constructors & Finalizers
public Ball()
: base()
{
Shape = Shape3DType.Ball;
Amount++;
}
private Ball(double density, double radius)
: base(density)
{
Radius = radius;
Shape = Shape3DType.Ball;
Amount++;
}
~Ball()
{
Amount--;
}
// Properties
public double Radius // 邊長
{
get
{
return _radius;
}
set
{
if (value > 0)
{
_radius = value;
}
}
}
public new static int Amount // 數量
{
get
{
return _amount;
}
private set
{
_amount = value;
}
}
// Methods
public override double CalculateVolume() // 計算體積
{
return 4.0 * GeoConst.pi * Radius * Radius * Radius / 3.0;
}
public double CalculateRollingDistance() // 計算滾動距離
{
return Radius * Radius;
}
public override string ShowParameter() // 顯示參數
{
StringBuilder sb = new StringBuilder();
sb.Append("Ball");
sb.Append('\t');
sb.Append('\t');
sb.Append(Radius.ToString("F2"));
sb.Append('\t');
sb.Append('\t');
sb.Append(Density.ToString("F2"));
sb.Append('\t');
return sb.ToString();
}
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 HW7
{
class Cylinder : Shape3D, IRollable
{
// Member variables
private double _radius = 0;
private double _height = 0;
private static int _amount = 0;
// Constructors & Finalizers
public Cylinder()
: base()
{
Shape = Shape3DType.Cylinder;
Amount++;
}
private Cylinder(double density, double radius, double height)
: base(density)
{
Radius = radius;
Height = height;
Shape = Shape3DType.Cylinder;
Amount++;
}
~Cylinder()
{
Amount--;
}
// Properties
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;
}
private set
{
_amount = value;
}
}
// Methods
public override double CalculateVolume() // 計算體積
{
return GeoConst.pi * Radius * Radius * Height;
}
public double CalculateRollingDistance() // 計算滾動距離
{
return 5.0 * Radius;
}
public override string ShowParameter() // 顯示參數
{
StringBuilder sb = new StringBuilder();
sb.Append("Cylinder");
sb.Append('\t');
sb.Append(Radius.ToString("F2"));
sb.Append('\t');
sb.Append(Height.ToString("F2"));
sb.Append('\t');
sb.Append(Density.ToString("F2"));
sb.Append('\t');
return sb.ToString();
}
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 HW7
{
class Pyramid : Shape3D
{
// Member variables
private double _side = 0;
private double _height = 0;
private static int _amount = 0;
// Constructors & Finalizers
public Pyramid()
: base()
{
Shape = Shape3DType.Pyramid;
Amount++;
}
private Pyramid(double density, double side, double height)
: base(density)
{
Side = side;
Height = height;
Shape = Shape3DType.Pyramid;
Amount++;
}
~Pyramid()
{
Amount--;
}
// Properties
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;
}
private set
{
_amount = value;
}
}
// Methods
public override double CalculateVolume() // 計算體積
{
return Side * Side * Height / 3.0;
}
public override string ShowParameter() // 顯示參數
{
StringBuilder sb = new StringBuilder();
sb.Append("Pyramid");
sb.Append('\t');
sb.Append('\t');
sb.Append(Side.ToString("F2"));
sb.Append('\t');
sb.Append(Height.ToString("F2"));
sb.Append('\t');
sb.Append(Density.ToString("F2"));
sb.Append('\t');
return sb.ToString();
}
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);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
interface IRollable
{
double CalculateRollingDistance();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class GeoConst
{
public static double pi = 3.1415926;
public static double[] DensityTable = { 2.70, 7.90, 8.90, 11.35}; // 鋁鐵銅鉛密度表
}
}
PPT背景右下角Precision Motion Control Lab是否忘了改?
回覆刪除===第七組===