檔案連結
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;
namespace HW08
{
public partial class Form1 : Form
{
private double density;
List shapeArr = new List();
public Form1()
{
InitializeComponent();
Lbl_1.Text = "";
Lbl_2.Text = "";
TxtBox_1.Visible = false;
TxtBox_2.Visible = false;
}
//*****************初始設定**************************
private void CMB_2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (CMB_2.SelectedIndex)
{
case 0:
Lbl_1.Text = "半徑";
Lbl_2.Text = "";
TxtBox_1.Visible = true;
TxtBox_2.Visible = false;
break;
case 1:
Lbl_1.Text = "邊長";
Lbl_2.Text = "";
TxtBox_1.Visible = true;
TxtBox_2.Visible = false;
break;
case 2:
Lbl_1.Text = "邊長";
Lbl_2.Text = "高";
TxtBox_1.Visible = true;
TxtBox_2.Visible = true;
break;
case 3:
Lbl_1.Text = "半徑";
Lbl_2.Text = "高";
TxtBox_1.Visible = true;
TxtBox_2.Visible = true;
break;
}
}
//*****************材料參數**************************
private void CMB_1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (CMB_1.SelectedIndex)
{
case 0:
density = 2.7;
break;
case 1:
density = 7.87;
break;
case 2:
density = 11.3;
break;
}
}
//*****************創造物件**************************
private void Btn_1_Click(object sender, EventArgs e)
{
if (CMB_1.SelectedIndex < 0 || CMB_2.SelectedIndex < 0 || TxtBox_1.Text == "0" || (TxtBox_1.Text == "0" && TxtBox_2.Text == "0"))
{
MessageBox.Show("請輸入正確參數!");
return;
}
switch (CMB_2.SelectedIndex)
{
case 0:
Ball ball = Ball.Create(double.Parse(TxtBox_1.Text), density);
shapeArr.Add(ball);
ball.Order = shapeArr.Count;
break;
case 1:
Cube cube = Cube.Create(double.Parse(TxtBox_1.Text), density);
shapeArr.Add(cube);
cube.Order = shapeArr.Count;
break;
case 2:
Pyramid pyramid = Pyramid.Create(double.Parse(TxtBox_1.Text), double.Parse(TxtBox_2.Text), density);
shapeArr.Add(pyramid);
pyramid.Order = shapeArr.Count;
break;
case 3:
Cylinder cylinder = Cylinder.Create(double.Parse(TxtBox_1.Text), double.Parse(TxtBox_2.Text), density);
shapeArr.Add(cylinder);
cylinder.Order = shapeArr.Count;
break;
}
TxtBox_4.Text = shapeArr.Count.ToString();
BallNum_txt.Text = Ball.Amount.ToString();
CubeNum_txt.Text = Cube.Amount.ToString();
PyramidNum_txt.Text = Pyramid.Amount.ToString();
CylinderNum_txt.Text = Cylinder.Amount.ToString();
TxtBox_3.AppendText(shapeArr[shapeArr.Count-1].ShapeProperty());
}
//*****************顯示創造物件**************************
private void Btn_2_Click(object sender, EventArgs e)
{
TxtBox_5.Clear();
Sort();
for (int i = 0; i < shapeArr.Count; i++)
{
TxtBox_5.AppendText(shapeArr[i].ShowAllProperty() + "\n");
}
}
//*****************滾動距離計算**************************
private void button1_Click(object sender, EventArgs e)
{
TxtBox_6.Clear();
Sort();
foreach (var shape3D in shapeArr)
{
IRollable rShape3D = shape3D as IRollable;
if (rShape3D != null)
{
TxtBox_6.AppendText(rShape3D.RollDistance());
}
}
}
//*****************消滅物件**************************
private void button2_Click(object sender, EventArgs e)
{
shapeArr.Sort(CompareByOrder);
int order = int.Parse(CancelNum_txt.Text);
if (shapeArr.Count < order)
{
return;
}
shapeArr[order] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
shapeArr.RemoveAt(order);
TxtBox_4.Text = shapeArr.Count.ToString();
BallNum_txt.Text = Ball.Amount.ToString();
CubeNum_txt.Text = Cube.Amount.ToString();
PyramidNum_txt.Text = Pyramid.Amount.ToString();
CylinderNum_txt.Text = Cylinder.Amount.ToString();
TxtBox_3.Clear();
for (int i = 0; i < shapeArr.Count; i++)
{
TxtBox_3.AppendText(shapeArr[i].ShapeProperty() + "\n");
}
}
//*****************排序**************************
private int CompareByOrder(Shape3D shape_1, Shape3D shape_2)
{
if (shape_1.Order > shape_2.Order)
return 1;
return -1;
}
private int CompareByShape(Shape3D shape_1, Shape3D shape_2)
{
if (shape_1.ShaType > shape_2.ShaType)
return 1;
return -1;
}
private int CompareByDensity(Shape3D shape_1, Shape3D shape_2)
{
if (shape_1.Density > shape_2.Density)
return 1;
return -1;
}
private int CompareByVolume(Shape3D shape_1, Shape3D shape_2)
{
if (shape_1.Volume() > shape_2.Volume())
return 1;
return -1;
}
private int CompareByMass(Shape3D shape_1, Shape3D shape_2)
{
if (shape_1.Mass() > shape_2.Mass())
return 1;
return -1;
}
private void Sort()
{
switch (cmb_sort.SelectedIndex)
{
case 0:
shapeArr.Sort(CompareByOrder);
break;
case 1:
shapeArr.Sort(CompareByShape);
break;
case 2:
shapeArr.Sort(CompareByDensity);
break;
case 3:
shapeArr.Sort(CompareByVolume);
break;
case 4:
shapeArr.Sort(CompareByMass);
break;
default:
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void TxtBox_3_TextChanged(object sender, EventArgs e)
{
}
}
}
Shap3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
public class Shape3D
{
protected double density;
protected static int amount;
protected int order;
protected Shape3DType.Types shatype;
public Shape3D(double density)
{
this.Density = density;
amount++;
}
public double Density
{
get
{
return density;
}
set
{
density = value;
}
}
public virtual double Volume()
{
return 0.0;
}
public static int Amount
{
get { return amount; }
set { amount = value; }
}
public double Mass()
{
return density * Volume();
}
public string ShowAllProperty()
{
string str = ShapeProperty();
str += '\t';
str += string.Format("{0,8:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Mass());
return str;
}
public virtual string ShapeProperty()
{
return "";
}
public int Order
{
get { return order; }
set { order = value; }
}
public int ShaType
{
get { return (int)shatype; }
}
protected static class Shape3DType
{
public enum Types { Unkown = 0, Ball, Cube, Pyramid, Cylinder };
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
public class Ball : Shape3D, IRollable
{
private double radius;
new private static int amount;
public Ball(double radius, double density)
: base(density)
{
this.Radius = radius;
amount++;
shatype = Shape3DType.Types.Ball;
}
~Ball()
{
amount--;
}
public static Ball Create(double radius, double density)
{
Ball B = new Ball(radius, density);
return B;
}
public double Radius
{
get{return radius;}
set{radius=value;}
}
public override double Volume()
{
return radius * radius * radius * Geomproperty.pi * 4 / 3.0;
}
public string RollDistance()
{
return ShowAllProperty() + "\t" + radius * radius + "\n";
}
public override string ShapeProperty()
{
string str = "Ball\t";
str += radius + "\t\t";
str += density + "\n";
return str;
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
public class Cube:Shape3D
{
private double side;
new private static int amount;
public Cube(double side, double density)
: base(density)
{
this.Side = side;
amount++;
shatype = Shape3DType.Types.Cube;
}
~Cube()
{
amount--;
}
public static Cube Create(double side, double density)
{
Cube C = new Cube(side, density);
return C;
}
public double Side
{
get{return side;}
set{side=value;}
}
public override double Volume()
{
return side * side * side;
}
public override string ShapeProperty()
{
string str = "Cube\t";
str += side + "\t\t";
str += density + "\n";
return str;
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
public class Pyramid:Shape3D
{
private double side;
private double high;
new private static int amount;
public Pyramid(double side, double high, double density)
:base(density)
{
this.Side = side;
this.High = high;
amount++;
shatype = Shape3DType.Types.Pyramid;
}
~Pyramid()
{
amount--;
}
public static Pyramid Create(double side, double high, double density)
{
Pyramid P = new Pyramid(side, high, density);
return P;
}
public double Side
{
get { return side; }
set { side = value; }
}
public double High
{
get { return high; }
set { high = value; }
}
public override double Volume()
{
return side * side * high / 3.0;
}
public override string ShapeProperty()
{
string str = "Pyramid\t";
str += side + "\t";
str += high + "\t";
str += density + "\n";
return str;
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
public class Cylinder : Shape3D, IRollable
{
private double radius;
private double high;
new private static int amount;
public Cylinder(double radius, double high, double density)
: base (density)
{
this.Radius = radius;
this.High = high;
amount++;
shatype = Shape3DType.Types.Cylinder;
}
~Cylinder()
{
amount--;
}
public static Cylinder Create(double radius, double high, double density)
{
Cylinder CL = new Cylinder(radius, high, density);
return CL;
}
public double Radius
{
get { return radius; }
set { radius = value; }
}
public double High
{
get { return high; }
set { high = value; }
}
public override double Volume()
{
return radius * radius * high * Geomproperty.pi;
}
public string RollDistance()
{
return ShowAllProperty() + "\t" + 5 * radius + "\n";
}
public override string ShapeProperty()
{
string str = "Cylinder\t";
str += radius + "\t";
str += high + "\t";
str += density + "\n";
return str;
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
}
}
IRollable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
interface IRollable
{
string RollDistance();
}
}
Geomproperty.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW08
{
class Geomproperty
{
public static double pi = 3.1415926;
}
}
沒有留言:
張貼留言