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.Windows.Forms;
namespace Hw7_40273032H
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LengthFieldInvisible();
}
private List shapeList=new List();
public void cbox_shape_SelectedIndexChanged(object sender, EventArgs e)//隨形狀改變的邊長條件
{
LengthFieldInvisible();
switch (cbox_shape.SelectedIndex)
{
case 0://球
lbl_length1.Text = "半徑";
break;
case 1://正方體
lbl_length1.Text = "邊長";
break;
case 2://圓柱體
lbl_length1.Text = "半徑";
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
break;
case 3://金字塔
lbl_length1.Text = "邊長";
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
break;
}
}
public void btn_add_Click(object sender, EventArgs e)//建立物件
{
if (cbox_material.SelectedIndex < 0 || cbox_shape.SelectedIndex < 0)
{
MessageBox.Show("沒選到材料或形狀");
return;
}
switch (cbox_shape.SelectedIndex)
{
case 0://球
Ball ball = null;
ball = Ball.Create(txt_length1.Text
, Const.Density[cbox_material.SelectedIndex], Const.MaterialName[cbox_material.SelectedIndex]);
if (ball == null)
{
MessageBox.Show("請檢查輸入是否正確!");
return;
}
else
{
ball.Order = shapeList.Count;
shapeList.Add(ball);
txt_ballNum.Text = Ball.BallNum.ToString();
}
break;
case 1://正方體
Cube cube = Cube.Create(txt_length1.Text
, Const.Density[cbox_material.SelectedIndex], Const.MaterialName[cbox_material.SelectedIndex]);
if (cube == null)
{
MessageBox.Show("請檢查輸入是否正確!");
return;
}
else
{
cube.Order = shapeList.Count;
shapeList.Add(cube);
txt_cubeNum.Text = Cube.CubeNum.ToString();
}
break;
case 2://圓柱體
Cylinder cylinder = Cylinder.Create(txt_length1.Text
, txt_length2.Text, Const.Density[cbox_material.SelectedIndex], Const.MaterialName[cbox_material.SelectedIndex]);
if (cylinder == null)
{
MessageBox.Show("請檢查輸入是否正確!");
return;
}
else
{
cylinder.Order = shapeList.Count;
shapeList.Add(cylinder);
txt_cylinderNum.Text = Cylinder.CylinderNum.ToString();
}
break;
case 3://金字塔
Pyramid pyramid = Pyramid.Create(txt_length1.Text
, txt_length2.Text, Const.Density[cbox_material.SelectedIndex], Const.MaterialName[cbox_material.SelectedIndex]);
if (pyramid == null)
{
MessageBox.Show("請檢查輸入是否正確!");
return;
}
else
{
pyramid.Order = shapeList.Count;
shapeList.Add(pyramid);
txt_pyramidNum.Text = Pyramid.PyramidNum.ToString();
}
break;
}
txt_pile.AppendText(shapeList[shapeList.Count-1].ShowField()+'\n');
txt_objNum.Text =shapeList.Count.ToString();
}
public void LengthFieldInvisible()
{
lbl_length2.Visible = false;
txt_length2.Visible = false;
txt_length1.Text = "";
txt_length2.Text = "";
}
private void btn_compute_Click(object sender, EventArgs e)
{
int i = 0;//編號
Sort();
txt_display.Clear();
foreach (Shape3D shape in shapeList)
{
txt_display.AppendText(i+shape.ShowResault());
i++;
}
}
private void btn_rockNRoll_Click(object sender, EventArgs e)
{
Sort();
txt_RockNRoll.Clear();
foreach (Shape3D shape in shapeList)
{
Ball ball = shape as Ball;
if(ball!=null)
txt_RockNRoll.AppendText(ball.RockNRoll()+'\n');
Cylinder cylinder = shape as Cylinder;
if(cylinder!=null)
txt_RockNRoll.AppendText(cylinder.RockNRoll() + '\n');
}
}
private void btn_cancel_Click(object sender, EventArgs e)
{
if (txt_cancelNum.Text == "" || int.Parse(txt_cancelNum.Text) >= shapeList.Count)
{
MessageBox.Show("刪除編號錯誤");
return;
}
shapeList[int.Parse(txt_cancelNum.Text)] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
shapeList.RemoveAt(int.Parse(txt_cancelNum.Text));
txt_ballNum.Text = Ball.BallNum.ToString();
txt_cubeNum.Text = Cube.CubeNum.ToString();
txt_cylinderNum.Text = Cylinder.CylinderNum.ToString();
txt_pyramidNum.Text = Pyramid.PyramidNum.ToString();
txt_objNum.Text = shapeList.Count.ToString();
}
public void Sort()
{
if (cbox_sortingType.SelectedIndex < 0)
{
cbox_sortingType.SelectedIndex = 0;
}
switch (cbox_sortingType.SelectedIndex)
{
case 0:
shapeList.Sort(CompareByOrder);
break;
case 1:
shapeList.Sort(CompareByShapeType);
break;
case 2:
shapeList.Sort(CompareByMaterial);
break;
case 3:
shapeList.Sort(CompareByVolume);
break;
case 4:
shapeList.Sort(CompareByWeight);
break;
}
}
private int CompareByOrder(Shape3D a, Shape3D b)
{
if (a.Order >= b.Order)
return 1;
else
return -1;
}
private int CompareByShapeType(Shape3D a, Shape3D b)
{
if (a.ShapeType >= b.ShapeType)
return 1;
else
return -1;
}
private int CompareByMaterial(Shape3D a, Shape3D b)
{
if (a.Density >= b.Density)
return 1;
else
return -1;
}
private int CompareByVolume(Shape3D a, Shape3D b)
{
if (a.CalculateVolume() >= b.CalculateVolume())
return 1;
else
return -1;
}
private int CompareByWeight(Shape3D a, Shape3D b)
{
if (a.CalculateWeight() >= b.CalculateWeight())
return 1;
else
return -1;
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
abstract class Shape3D
{
/*****************Parameter*********************/
protected int order;
protected double density;
protected string material;
protected int shapeType;
public enum Types { Ball=0, Cube, Cylinder, Pyramid };
/*****************Attribute*********************/
public int Order
{
get { return order; }
set { order = value; }
}
public int ShapeType
{
get { return shapeType; }
}
public double Density
{
get { return density; }
}
/******************Methods********************/
public abstract double CalculateVolume();//回傳體積
public abstract double CalculateWeight();//回傳重量
public abstract string SheapName();//回傳型狀
public abstract string Length1();//回傳第一邊長
public abstract string Length2();//回傳第二邊長
public string ShowField()
{
string str = "";
str += string.Format("{0,1}", material);
str += '\t';
str +=SheapName();
str += '\t';
str += string.Format("{0,5}", Length1());
str += '\t';
str += string.Format("{0,5}", Length2());
str += string.Format("{0,5}", density);
str += '\t';
return str;
}
public string ShowResault()
{
string str = "";
str+=ShowField();
str += "體積:";
str += string.Format("{0,8:F2}", CalculateVolume());
str += "重量:";
str += string.Format("{0,8:F2}", CalculateWeight());
str += '\n';
return str;
}
}
}
Const.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
class Const
{
public static double pi = 3.1415926;
public static string[] MaterialName = { "鋁", "鐵", "鉛" };
public static double[] Density = { 2.7, 7.9, 11.34 };//材料密度
}
}
IRollable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
interface IRollable
{
string RockNRoll();
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
class Pyramid : Shape3D
{
/*****************Parameter*********************/
private double length, high;
private static int pyramidNum = 0;
/*****************Attribute*********************/
public double Length
{
get { return length; }
set
{
length = value;
}
}
public double High
{
get { return high; }
set
{
high = value;
}
}
public static int PyramidNum
{
get { return pyramidNum; }
set
{
pyramidNum = value;
}
}
/*****************Constructor*******************/
public Pyramid(string length, string high, double density, string material)
{
this.length = Convert.ToDouble(length);
this.high = Convert.ToDouble(high);
this.density = density;
this.material = material;
shapeType =(int)Shape3D.Types.Pyramid;
PyramidNum++;
}
//Destructor
~Pyramid()
{
PyramidNum--;
}
/******************Methods********************/
public override double CalculateVolume()
{
return 1.0 / 3 * Length * Length * High;
}
public override double CalculateWeight()
{
return CalculateVolume() * density;
}
public override string SheapName()
{
return "金字塔\t";
}
public override string Length1()
{
return Length.ToString();
}
public override string Length2()
{
return High.ToString() + '\t';
}
/*************Class Method******************/
public static Pyramid Create(string length, string high, double density, string material)
{
Pyramid pyramid = null;
if (length==""|| high==""|| double.Parse(length) <= 0 || double.Parse(high) <= 0 || density <= 0)
{
return pyramid;
}
else
{
pyramid = new Pyramid(length, high, density, material);
return pyramid;
}
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
class Cylinder : Shape3D,IRollable
{
/*****************Parameter*********************/
private double radius, high;
private static int cylinderNum = 0;
/*****************Attribute*********************/
public double Radius
{
get { return radius; }
set
{
radius = value;
}
}
public double High
{
get { return high; }
set
{
radius = value;
}
}
public static int CylinderNum
{
get { return cylinderNum; }
set
{
cylinderNum = value;
}
}
/*****************Constructor*******************/
public Cylinder(string radius, string high, double density, string material)
{
this.radius = Convert.ToDouble(radius);
this.high = Convert.ToDouble(high);
this.density = density;
this.material = material;
shapeType = (int)Shape3D.Types.Cylinder;
CylinderNum++;
}
//Destructor
~Cylinder()
{
CylinderNum--;
}
/******************Methods********************/
public override double CalculateVolume()
{
return Radius * Radius * Const.pi * High;
}
public override double CalculateWeight()
{
return CalculateVolume() * density;
}
public override string SheapName()
{
return "圓柱體\t";
}
public override string Length1()
{
return Radius.ToString();
}
public override string Length2()
{
return High.ToString()+'\t';
}
public string RockNRoll()
{
return base.ShowField() + string.Format("{0,6}", Radius * 5);
}
/*************Class Method******************/
public static Cylinder Create(string radius,string high, double density, string material)
{
Cylinder cylinder = null;
if (radius==""||high==""|| double.Parse(radius) <= 0 || double.Parse(high) <= 0 || density <= 0)
{
return cylinder;
}
else
{
cylinder = new Cylinder(radius, high, density, material);
return cylinder;
}
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
class Cube : Shape3D
{
/*****************Parameter*********************/
private double length;
private static int cubeNum = 0;
/*****************Attribute*********************/
public double Length
{
get { return length; }
set
{
length = value;
}
}
public static int CubeNum
{
get { return cubeNum; }
set
{
cubeNum = value;
}
}
/*****************Constructor*******************/
public Cube(string length, double density, string material)
{
this.length = Convert.ToDouble(length);
this.density = density;
this.material = material;
shapeType = (int)Shape3D.Types.Cube;
CubeNum++;
}
//Destructor
~Cube()
{
CubeNum--;
}
/******************Methods********************/
public override double CalculateVolume()
{
return Length * Length * Length;
}
public override double CalculateWeight()
{
return CalculateVolume() * density;
}
public override string SheapName()
{
return "正立方體";
}
public override string Length1()
{
return Length.ToString();
}
public override string Length2()
{
return "\t";
}
/*************Class Method******************/
public static Cube Create(string length, double density, string material)
{
Cube cube = null;
if (length==""|| double.Parse(length) <= 0 || density <= 0)
{
return cube;
}
else
{
cube = new Cube(length, density, material);
return cube;
}
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw7_40273032H
{
class Ball : Shape3D,IRollable
{
/*****************Parameter*********************/
private double radius;
private static int ballNum = 0;
/*****************Attribute*********************/
public double Radius
{
get { return radius; }
set
{
radius = value;
}
}
public static int BallNum
{
get { return ballNum; }
set
{
ballNum = value;
}
}
/*****************Constructor*******************/
public Ball(string radius, double density, string material)
{
this.radius = Convert.ToDouble(radius);
this.density = density;
this.material = material;
shapeType = (int)Shape3D.Types.Ball;
BallNum++;
}
//Destructor
~Ball()
{
BallNum--;
}
/******************Functions********************/
public override double CalculateVolume()
{
return 3.0 / 4 * Const.pi * Radius * Radius * Radius;
}
public override double CalculateWeight()
{
return CalculateVolume() * density;
}
public override string SheapName()
{
return "球\t";
}
public override string Length1()
{
return Radius.ToString();
}
public override string Length2()
{
return "\t";
}
public string RockNRoll()
{
return base.ShowField() + string.Format("{0,6}", Radius * Radius);
}
/*************Class Method******************/
public static Ball Create(string radius, double density, string material)
{
Ball ball = null;
if (radius==""||double.Parse(radius) <= 0 || density <= 0)
{
return ball;
}
else
{
ball = new Ball(radius, density, material);
return ball;
}
}
}
}
1. Form1的第146行,因為已經有用到介面,所以這邊轉型成IRollable會更加簡潔。
回覆刪除2. 欄位使用private,權限留給屬性去處理。