執行檔的連結
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
{
Label[] arr_lbl_Feature;
TextBox[] arr_txt_Feature;
List Shape3DList = new List();
List RollList = new List();
/******** 建構子 ********/
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) // 加入
{
// 判斷是否可以將字串轉換成數字
double[] TxtNumber = new double[arr_lbl_Feature.Length];
bool[] CanConvert = new bool[arr_lbl_Feature.Length];
bool CanCreate = false;
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])
{
Cube ThisCube = Cube.Create(GeoConst.DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0]);
if (ThisCube != null)
{
Shape3DList.Add(ThisCube);
CanCreate = true;
}
}
break;
case 1:
if (CanConvert[0])
{
Ball ThisBall = Ball.Create(GeoConst.DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0]);
if (ThisBall != null)
{
Shape3DList.Add(ThisBall);
RollList.Add(ThisBall);
CanCreate = true;
}
}
break;
case 2:
if (CanConvert[0] && CanConvert[1])
{
Cylinder ThisCylinder = Cylinder.Create(GeoConst.DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0], TxtNumber[1]);
if (ThisCylinder != null)
{
Shape3DList.Add(ThisCylinder);
RollList.Add(ThisCylinder);
CanCreate = true;
}
}
break;
case 3:
if (CanConvert[0] && CanConvert[1])
{
Pyramid ThisPyramid = Pyramid.Create(GeoConst.DensityTable[cmo_MaterialSelect.SelectedIndex], TxtNumber[0], TxtNumber[1]);
if (ThisPyramid != null)
{
Shape3DList.Add(ThisPyramid);
CanCreate = true;
}
}
break;
default:
break;
}
// 顯示建立結果
if (CanCreate)
{
ShowResult("Number");
ShowResult("Ok");
}
else
{
ShowResult("False");
}
}
private void btn_Show_Click(object sender, EventArgs e) // 顯示質量與體積
{
ShowResult("Mass&Volume");
}
private void btn_Roll_Click(object sender, EventArgs e) // 顯示滾動距離
{
ShowResult("RollingDis");
}
/******** 與前端輸入輸出相關函數 ********/
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 DoWhat)
{
string s = "";
switch (DoWhat)
{
case "False": // 顯示建立失敗資訊
s += "建立失敗,請重新確認參數!";
s += Environment.NewLine;
txt_Display.AppendText(s);
break;
case "Ok" : // 顯示建立成功資訊
s = Shape3DList[Shape3DList.Count-1].ShowPar();
s += Environment.NewLine;
txt_Display.AppendText(s);
break;
case "Mass&Volume": // 顯示質量與體積
foreach (var element in Shape3DList)
{
s += element.ShowPar();
s += element.ShowMassVol();
s += Environment.NewLine;
}
txt_MassVol.Text = s;
break;
case "RollingDis": // 顯示滾動距離
foreach (var element in RollList)
{
s += ((Shape3D)element).ShowPar();
s += element.RollingDistance().ToString("F2");
s += Environment.NewLine;
}
txt_Roll.Text = s;
break;
case "Number":
txt_Amount.Text = Shape3DList.Count.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
{
/******** 欄位 ********/
private double density = 0;
private static int amount = 0;
/******** 屬性 ********/
public double Density // 密度
{
get
{
return density;
}
set
{
density = value;
}
}
public static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Shape3D()
{
amount++;
}
public Shape3D(double density)
{
this.density = density;
amount++;
}
/******** 方法 ********/
public double Mass() // 計算質量
{
return Density * Volume();
}
public abstract double Volume(); // 計算體積
public string ShowMassVol() // 顯示質量與體積
{
string s = "";
s += System.String.Format("{0:F2}", Volume());
s += '\t';
s += System.String.Format("{0:F2}", Mass());
s += '\t';
return s;
}
public abstract string ShowPar(); // 顯示參數
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Cube : Shape3D
{
/******** 欄位 ********/
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()
{
amount++;
}
private Cube(double density, double side)
: base(density)
{
Side = side;
amount++;
}
/******** 方法 ********/
public override double Volume() // 計算體積
{
return Side * Side * Side;
}
public override string ShowPar() // 顯示參數
{
string s = "";
s += "Cube" + '\t' + '\t';
s += Side.ToString("F2") + '\t';
s += '\t';
s += Density.ToString("F2") + '\t';
return s;
}
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
{
/******** 欄位 ********/
private double radius = 0;
private static int amount = 0;
/******** 屬性 ********/
public double Radius // 邊長
{
get
{
return radius;
}
set
{
if (value > 0)
{
radius = value;
}
}
}
public new static int Amount // 數量
{
get
{
return amount;
}
}
/******** 建構子 ********/
public Ball()
: base()
{
amount++;
}
private Ball(double density, double radius)
: base(density)
{
Radius = radius;
amount++;
}
/******** 方法 ********/
public override double Volume() // 計算體積
{
return 4.0 * GeoConst.pi * Radius * Radius * Radius / 3.0;
}
public double RollingDistance() // 計算滾動距離
{
return Radius * Radius;
}
public override string ShowPar() // 顯示參數
{
string s = "";
s += "Ball" + '\t' + '\t';
s += Radius.ToString("F2") + '\t';
s += '\t';
s += Density.ToString("F2") + '\t';
return s;
}
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
{
/******** 欄位 ********/
private double radius = 0;
private double height = 0;
private static int amount = 0;
/******** 屬性 ********/
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()
{
amount++;
}
private Cylinder(double density, double radius, double height)
: base(density)
{
Radius = radius;
Height = height;
amount++;
}
/******** 方法 ********/
public override double Volume() // 計算體積
{
return GeoConst.pi * Radius * Radius * Height;
}
public double RollingDistance() // 計算滾動距離
{
return 5.0 * Radius;
}
public override string ShowPar() // 顯示參數
{
string s = "";
s += "Cylinder" + '\t';
s += Radius.ToString("F2") + '\t';
s += Height.ToString("F2") + '\t';
s += Density.ToString("F2") + '\t';
return s;
}
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
{
/******** 欄位 ********/
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()
{
amount++;
}
private Pyramid(double density, double side, double height)
: base(density)
{
Side = side;
Height = height;
amount++;
}
/******** 方法 ********/
public override double Volume() // 計算體積
{
return Side * Side * Height / 3.0;
}
public override string ShowPar() // 顯示參數
{
string s = "";
s += "Pyramid" + '\t' + '\t';
s += Side.ToString("F2") + '\t';
s += Height.ToString("F2") + '\t';
s += Density.ToString("F2") + '\t';
return s;
}
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 RollingDistance();
}
}
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}; // 鋁鐵銅鉛密度表
}
}
沒有留言:
張貼留言