執行檔
專案檔
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 st_hw08
{
public partial class Form1 : Form
{
List shapeArr = new List();
private int createCounter = 0;
private static double[] densityArr = { 2.7, 7.87, 11.3 };
public Form1()
{
InitializeComponent();
cbox_ShapeSelect.SelectedIndex = 0;
cbox_MaterialSelect.SelectedIndex = 0;
}
private void cbox_ShapeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
lbl_Para1.Text = "半徑";
lbl_Para2.Visible = false;
txt_Para2.Visible = false;
break;
case 1:
lbl_Para1.Text = "邊長";
lbl_Para2.Visible = false;
txt_Para2.Visible = false;
break;
case 2:
lbl_Para1.Text = "半徑";
lbl_Para2.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
case 3:
lbl_Para1.Text = "邊長";
lbl_Para2.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
default:
break;
}
}
private void btn_Add_Click(object sender, EventArgs e)
{
bool CanConvert;
double para1, para2;
//check input
CanConvert = double.TryParse(txt_Para1.Text, out para1);
if (!CanConvert) //轉換失敗時
{
MessageBox.Show("參數輸入錯誤");
return;
}
CanConvert = double.TryParse(txt_Para2.Text, out para2);
if (!CanConvert) //轉換失敗時
{
MessageBox.Show("參數輸入錯誤");
return;
}
int amount = Shape3D.Amount;
double density = densityArr[cbox_MaterialSelect.SelectedIndex];
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
Ball ball = new Ball(Convert.ToDouble(txt_Para1.Text), density);
shapeArr.Add(ball);
ball.Order = ++createCounter;
txt_BallNum.Text = Ball.Amount.ToString();
break;
case 1:
Cube cube = new Cube(Convert.ToDouble(txt_Para1.Text), density);
shapeArr.Add(cube);
cube.Order = ++createCounter;
txt_CubeNum.Text = Cube.Amount.ToString();
break;
case 2:
Cylinder cylinder = new Cylinder(Convert.ToDouble(txt_Para1.Text), Convert.ToDouble(txt_Para2.Text), density);
shapeArr.Add(cylinder);
cylinder.Order = ++createCounter;
txt_CylinderNum.Text = Cylinder.Amount.ToString();
break;
case 3:
Pyramid pyramid = new Pyramid(Convert.ToDouble(txt_Para1.Text), Convert.ToDouble(txt_Para2.Text), density);
shapeArr.Add(pyramid);
pyramid.Order = ++createCounter;
txt_PyramidNum.Text = Pyramid.Amount.ToString();
break;
default:
break;
}
txt_Message.AppendText(shapeArr[amount].ShapeProperty() + "\n");
txt_AmountOfShape.Text = Shape3D.Amount.ToString();
}
private void btn_Calculate_Click(object sender, EventArgs e)
{
txt_Display.Clear();
int SelectedSortMethod = cmb_SortMethodOfDisplay.SelectedIndex;
if (SelectedSortMethod == -1)
{
MessageBox.Show("請選擇排序方法!");
return;
}
SortBeforeDisplay(SelectedSortMethod);
for (int i = 0; i < shapeArr.Count; i++)
{
string str = (shapeArr[i].ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
private void btn_RollDistance_Click(object sender, EventArgs e)
{
txt_distance.Clear();
for (int i = 0; i < shapeArr.Count ; i++)
{
string str_Output = "";
IRollable temp = shapeArr[i] as IRollable;
if (temp != null)
{
str_Output += temp.GetDistance() + "\n";
txt_distance.AppendText(str_Output);
}
}
}
private void btn_Delete_Click(object sender, EventArgs e)
{
int delNum;
bool CanConvert = int.TryParse(txt_DelObjectNum.Text, out delNum);
if (CanConvert)
delNum = Convert.ToInt32(txt_DelObjectNum.Text) - 1;
else
{
MessageBox.Show("請輸入數字");
return;
}
//is delNum out of range?
if (delNum >= shapeArr.Count || delNum < 0)
{
MessageBox.Show("不在範圍內,請重新輸入!");
return;
}
//delete object
shapeArr[delNum] = null;
shapeArr.RemoveAt(delNum);
GC.Collect();
GC.WaitForPendingFinalizers();
//refresh counter
txt_AmountOfShape.Text = Shape3D.Amount.ToString();
txt_BallNum.Text = Ball.Amount.ToString();
txt_CubeNum.Text = Cube.Amount.ToString();
txt_CylinderNum.Text = Cylinder.Amount.ToString();
txt_PyramidNum.Text = Pyramid.Amount.ToString();
//refresh object inform
txt_Message.Clear();
for (int i = 0; i < shapeArr.Count; i++)
{
txt_Message.AppendText(shapeArr[i].ShapeProperty() + "\n");
}
}
//----------sort method----------
private void SortBeforeDisplay(int SelectedSortMethod)
{
switch (SelectedSortMethod)
{
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(CompareByWeight);
break;
default:
MessageBox.Show("ERROR!!");
break;
}
}
//依創造順序的先後排列 (注意:要考慮相等時的情況)
private static int CompareByOrder(Shape3D shape1, Shape3D shape2)
{
if (shape1.Order > shape2.Order)
return 1;
else if (shape1.Order == shape2.Order)
return 0;
else return -1;
}
//依形狀排列 (注意:要考慮相等時的情況)
private static int CompareByShape(Shape3D shape1, Shape3D shape2)
{
if (shape1.ShapeNum > shape2.ShapeNum)
return 1;
else if (shape1.ShapeNum == shape2.ShapeNum)
return 0;
else return -1;
}
//依材質(密度)排列 (注意:要考慮相等時的情況)
private static int CompareByDensity(Shape3D shape1, Shape3D shape2)
{
if (shape1.Density > shape2.Density)
return 1;
else if (shape1.Density == shape2.Density)
return 0;
else return -1;
}
//依體積排列 (注意:要考慮相等時的情況)
private static int CompareByVolume(Shape3D shape1, Shape3D shape2)
{
if (shape1.Volume() > shape2.Volume())
return 1;
else if (shape1.Volume() == shape2.Volume())
return 0;
else return -1;
}
//依重量排列 (注意:要考慮相等時的情況)
private static int CompareByWeight(Shape3D shape1, Shape3D shape2)
{
if (shape1.Weight() > shape2.Weight())
return 1;
else if (shape1.Weight() == shape2.Weight())
return 0;
else return -1;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
abstract class Shape3D
{
protected double density;
protected static int amount = 0;
protected int shapeNum = (int)ShapeType.unknown;
protected int order = 0;
public enum ShapeType { unknown = 0, ball, cube, cylinder, pyramid };
public Shape3D()
{
Density = 0;
Amount++;
}
public Shape3D(double d)
{
Density = d;
Amount++;
}
~Shape3D()
{
Amount--;
}
public double Density
{
get { return density; }
set
{
if (value < 0)
density = 0;
else
density = value;
}
}
public static int Amount
{
get { return amount; }
set { amount = value;}
}
public int ShapeNum
{
get { return shapeNum; }
}
public int Order
{
get { return order; }
set { order = value; }
}
public double Weight()
{
return Density * Volume();
}
//Virtual Method
public abstract double Volume();
public string ShowVolumeWeight()
{
string str = ShapeProperty();
str += '\t';
str += string.Format("{0,8:F2}", density);
str += '\t';
str += string.Format("{0,8:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
public abstract string ShapeProperty();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
interface IRollable
{
double RollDistance();
string GetDistance();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
class Ball : Shape3D,IRollable
{
new private static int amount = 0;
private double radius;
public Ball(double radius, double density)
: base(density)
{
shapeNum = (int)ShapeType.ball;
Amount++;
Order++;
Radius = radius;
}
~Ball()
{
Amount--;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
new public static int Amount
{
get { return amount; }
set { amount = value;}
}
public override double Volume()
{
return 4.0/3.0*GeoConstant.pi*radius*radius*radius;
}
public override string ShapeProperty()
{
//string str = string.Format("{0,8}", "Ball");
string str = "Ball";
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
public static Ball Creat(double r, double density)
{
if (r < 0)
return null;
Ball ball=new Ball(r,density);
return ball;
}
public double RollDistance()
{
return radius * radius;
}
public string GetDistance()
{
string str_Output = "";
str_Output += ShapeProperty() + "\t" + Density.ToString("#.##") + "\t";
//滾動距離=0時,ToString("#.##")會return "". 所以要另外判斷
//也可改用string.Format("format", data);
if (RollDistance() == 0)
str_Output += 0.ToString() + "\t";
else str_Output += RollDistance().ToString("#.##") + "\t";
return str_Output;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
class Cube : Shape3D
{
new private static int amount = 0;
private double length;
public Cube(double length, double density)
: base(density)
{
Amount++;
shapeNum = (int)ShapeType.cube;
Length = length;
}
~Cube()
{
Amount--;
}
public double Length
{
get { return length; }
set
{
if (value < 0)
length = 0;
else
length = value;
}
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
public override double Volume()
{
return length * length * length;
}
public override string ShapeProperty()
{
//string str = string.Format("{0,8}", "Cube");
string str = "Cube";
str += '\t';
str += string.Format("{0,8:F2}", length);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
class Cylinder : Shape3D, IRollable
{
new private static int amount = 0;
private double radius;
private double height;
public Cylinder(double radius, double height,double density)
: base(density)
{
Amount++;
Order++;
shapeNum = (int)ShapeType.cylinder;
Radius = radius;
Height = height;
}
~Cylinder()
{
Amount--;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
height = 0;
else
height = value;
}
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
public override double Volume()
{
return GeoConstant.pi * radius * radius * height;
}
public override string ShapeProperty()
{
//string str = string.Format("{0,8}", "Cylinder");
string str = "Cylinder";
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
public double RollDistance()
{
return 5.0 * radius;
}
public string GetDistance()
{
string str_Output = "";
str_Output += ShapeProperty() + "\t" + Density.ToString("#.##") + "\t";
//滾動距離=0時,ToString("#.##")會return "". 所以要另外判斷
//也可改用string.Format("format", data);
if (RollDistance() == 0)
str_Output += 0.ToString() + "\t";
else str_Output += RollDistance().ToString("#.##") + "\t";
return str_Output;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace st_hw08
{
class Pyramid : Shape3D
{
new private static int amount = 0;
private double length;
private double height;
public Pyramid(double length, double height, double density)
: base(density)
{
Amount++;
Order++;
shapeNum = (int)ShapeType.pyramid;
Length = length;
Height = height;
}
~Pyramid()
{
Amount--;
}
public double Length
{
get { return length; }
set
{
if (value < 0)
length = 0;
else
length = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
height = 0;
else
height = value;
}
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
public override double Volume()
{
return 1.0 / 3.0 * length * length * height;
}
public override string ShapeProperty()
{
//string str = string.Format("{0,8}", "Pyramid");
string str = "Pyramid";
str += '\t';
str += string.Format("{0,8:F2}", length);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
}
}
沒有留言:
張貼留言