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 HW8
{
public partial class Form1 : Form
{
private double[] dens = new double[] { 11.34, 8.9, 2.7 };//鉛銅鋁的密度陣列
List shapeArr = new List();
public Form1()
{
InitializeComponent();
//初始化版面,FrontPanel設定上ListView內容將View設定為"Details"
listView_temp.Columns.Add("形狀", 60, HorizontalAlignment.Center);
listView_temp.Columns.Add("材質", 60, HorizontalAlignment.Center);
listView_Output.Columns.Add("形狀", 60, HorizontalAlignment.Center);
listView_Output.Columns.Add("材質", 60, HorizontalAlignment.Center);
listView_Output.Columns.Add("體積", 80, HorizontalAlignment.Center);
listView_Output.Columns.Add("重量", 80, HorizontalAlignment.Center);
}
private void btn_Caculate_Click(object sender, EventArgs e)
{
Sort();
listView_Output.Items.Clear();
PrintAllData();
}
private void btn_Add_Click(object sender, EventArgs e)
{
if (!CheckData(cbox_shape.SelectedIndex)) //檢查是否有資料忘記填寫
return;
switch (cbox_shape.SelectedIndex)
{
//定義各形狀之建構子傳入參數為參數一,參數二,材料,密度
case 0: //圓球
Ball ball = new Ball(double.Parse(txtb_left.Text), dens[cbox_material.SelectedIndex], cbox_material.SelectedItem.ToString());
shapeArr.Add(ball);
ball.Order = shapeArr.Count;
txtb_BallNum.Text = Ball.Amount.ToString();
break;
case 1: //金字塔
Pyramid pyr = new Pyramid(double.Parse(txtb_left.Text), double.Parse(txtb_right.Text), dens[cbox_material.SelectedIndex], cbox_material.SelectedItem.ToString());
shapeArr.Add(pyr);
pyr.Order = shapeArr.Count;
txtb_PyrNum.Text = Pyramid.Amount.ToString();
break;
case 2: //圓柱
Cylinder cyl = new Cylinder(double.Parse(txtb_left.Text), double.Parse(txtb_right.Text), dens[cbox_material.SelectedIndex], cbox_material.SelectedItem.ToString());
shapeArr.Add(cyl);
cyl.Order = shapeArr.Count;
txtb_CylNum.Text = Cylinder.Amount.ToString();
break;
case 3:
Cube cub = new Cube(double.Parse(txtb_left.Text), dens[cbox_material.SelectedIndex], cbox_material.SelectedItem.ToString());
shapeArr.Add(cub);
cub.Order = shapeArr.Count;
txtb_CubNum.Text = Cube.Amount.ToString();
break;
}
txt_TolalNum.Text = Shape.Amount.ToString();
ListViewItem Data = new ListViewItem(shapeArr[Shape.Amount - 1].GetShapeName());
Data.SubItems.Add(shapeArr[Shape.Amount - 1].Material);
listView_temp.Items.Add(Data);
}
//形狀切換
private void cbox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_shape.SelectedIndex)
{
case 0:
lbl_left.Text = "半徑";
txtb_right.Visible = false;
lbl_right.Visible = false;
break;
case 1:
lbl_left.Text = "邊長";
lbl_right.Text = "高";
txtb_right.Visible = true;
lbl_right.Visible = true;
break;
case 2:
lbl_left.Text = "半徑";
lbl_right.Text = "高";
txtb_right.Visible = true;
lbl_right.Visible = true;
break;
case 3:
lbl_left.Text = "邊長";
txtb_right.Visible = false;
lbl_right.Visible = false;
break;
}
}
//將面板重置
private void ResetFrontPanel()
{
txtb_rollOut.Clear();
listView_temp.Items.Clear();
listView_Output.Items.Clear();
}
//列印所有資料
private void PrintAllData()
{
for (int i = 0; i < Shape.Amount; i++)
{
ListViewItem DataMamber = new ListViewItem(shapeArr[i].GetShapeName());
DataMamber.SubItems.Add(shapeArr[i].Material);
DataMamber.SubItems.Add(shapeArr[i].GetVolume().ToString("0.##"));
DataMamber.SubItems.Add(shapeArr[i].GetWeight().ToString("0.##"));
listView_Output.Items.Add(DataMamber);
}
}
private void PrintRollData()
{
foreach (var shape in shapeArr)
{
IRollable iroll = shape as IRollable;
if (iroll != null)
{
txtb_rollOut.AppendText(iroll.RollDist());
}
}
}
//檢查數字是否有輸入
private bool CheckData(int flag)
{
switch (flag)
{
case 0:
case 3:
if (txtb_left.Text == "")
{
MessageBox.Show("資料請輸入完整 !");
return false;
}
break;
case 1:
case 2:
if (txtb_left.Text == "" || txtb_right.Text == "")
{
MessageBox.Show("資料請輸入完整 !");
return false;
}
break;
}
if (cbox_material.Text == "請選擇" || cbox_shape.Text == "請選擇")
{
MessageBox.Show("請選擇形狀與材質 !");
return false;
}
return true;
}
private void btn_clear_Click(object sender, EventArgs e)
{
ResetFrontPanel();
shapeArr.Clear();
Shape.Amount = 0;
Ball.Amount = 0;
Pyramid.Amount = 0;
Cylinder.Amount = 0;
Cube.Amount = 0;
txtb_BallNum.Text = "0";
txtb_CylNum.Text = "0";
txtb_PyrNum.Text = "0";
txtb_CubNum.Text = "0";
txt_TolalNum.Text = "0";
txtb_left.Text = "";
txtb_right.Text = "";
cbox_material.Text = "請選擇";
cbox_shape.Text = "請選擇";
cbx_sort.Text = "請選擇";
}
private void btn_Roll_Click(object sender, EventArgs e)
{
txtb_rollOut.Clear();
PrintRollData();
}
private void btn_cancel_Click(object sender, EventArgs e)
{
int order = int.Parse(txtb_order.Text);
if (shapeArr.Count < order)
return;
shapeArr[order] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
shapeArr.RemoveAt(order);
txtb_BallNum.Text = Ball.Amount.ToString();
txtb_PyrNum.Text = Pyramid.Amount.ToString();
txtb_CylNum.Text = Cylinder.Amount.ToString();
txtb_CubNum.Text = Cube.Amount.ToString();
txt_TolalNum.Text = Shape.Amount.ToString();
}
private void Sort()
{
switch(cbx_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(CompareByWeight);
break;
default:
break;
}
}
private int CompareByOrder(Shape a,Shape b)
{
if (a.Order > b.Order)
return 1;
else
return -1;
}
private int CompareByShape(Shape a,Shape b)
{
if (a.ShapeType > b.ShapeType)
return 1;
else
return -1;
}
private int CompareByDensity(Shape a,Shape b)
{
if (a.Density > b.Density)
return 1;
else
return -1;
}
private int CompareByVolume(Shape a,Shape b)
{
if (a.GetVolume() > b.GetVolume())
return 1;
else
return -1;
}
private int CompareByWeight(Shape a,Shape b)
{
if (a.GetWeight() > b.GetWeight())
return 1;
else
return -1;
}
}
}
Shape.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
abstract class Shape
{
//****************成員*************
protected static double pi = 3.1415926;
private static int amount = 0; //用來紀錄全部共有幾個形狀
private string material = "";
private double density = 0;
protected int order;
protected ShapeTypes.Types shapetype;
//****************屬性*************
public double Density
{
set
{
if (value >= 0)
density = value;
else
density = 0;
}
get { return density; }
}
public string Material
{
set { material = value; }
get { return material; }
}
public static int Amount
{
set { amount = value; }
get { return amount; }
}
public int Order
{
set { order = value; }
get { return order; }
}
public int ShapeType
{
get { return (int)shapetype; }
}
//***************建構子************
public Shape()
{
amount++;
}
~Shape()
{
amount--;
}
//***************方法**************
abstract public double GetVolume();
abstract public double GetWeight();//抽象方法,可讓各子類別計算各自重量
abstract public string GetShapeName();//抽象方法,各子類別return形狀名稱
}
static class ShapeTypes
{
public enum Types { Unknown = 0, Ball, Cylinder, Pyramid,Cube };
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Cube:Shape
{
//*********成員**********
private static int amount = 0;
private static string ShapeName = "正方體";
private double side = 0;
//*********屬性**********
public double Side
{
set
{
if (value >= 0)
side = value;
else
side = 0;
}
get { return side; }
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
//**********建構子********
public Cube(double s, double d, string m)
{
Material = m;
Side = s;
Density = d;
shapetype = ShapeTypes.Types.Cube;
amount++;
}
~Cube()
{
amount--;
}
//*********方法**********
public override double GetVolume()
{
return Side * Side * Side;
}//虛擬方法,可讓各子類別計算各自體積
public override string GetShapeName()
{
return ShapeName;
}
public override double GetWeight()
{
return GetVolume() * Density;
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Ball : Shape,IRollable
{
//*********成員**********
private static int amount = 0;
private static string ShapeName = "圓球";
private double radius = 0;
//*********屬性**********
public double Radius
{
set
{
if (value >= 0)
radius = value;
else
radius = 0;
}
get { return radius; }
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
//**********建構子********
public Ball(double r, double d, string m)
{
Material = m;
Radius = r;
Density = d;
shapetype = ShapeTypes.Types.Ball;
amount++;
}
~Ball()
{
amount--;
}
//*********方法**********
public override double GetVolume()
{
return 4.0 * pi * Radius * Radius * Radius / 3.0;
}//虛擬方法,可讓各子類別計算各自體積
public override string GetShapeName()
{
return ShapeName;
}
public override double GetWeight()
{
return GetVolume() * Density;
}
public string RollDist()
{
string str_Output = "";
str_Output += GetShapeName() + "\t" + Radius.ToString() + "\t" + "\t" + Density.ToString()
+ "\t" + (Radius * Radius).ToString("#.00") + "\n";
return str_Output;
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Cylinder : Shape,IRollable
{
//*********成員**********
private static int amount = 0;
private static string ShapeName = "圓柱";
private double parameter = 0;
private double height = 0;
//**********屬性**********
public double Parameter
{
set
{
if (value >= 0)
parameter = value;
else
parameter = 0;
}
get { return parameter; }
}
public double Height
{
set
{
if (value >= 0)
height = value;
else
height = 0;
}
get { return height; }
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
//**********建構子********
public Cylinder(double p, double h, double d, string m)
{
Material = m;
Parameter = p;
Height = h;
Density = d;
shapetype = ShapeTypes.Types.Cylinder;
amount++;
}
~Cylinder()
{
amount--;
}
//**********方法**********
public override double GetVolume()
{
return Parameter * Parameter * pi * Height;
}//虛擬方法,可讓各子類別計算各自體積
public override string GetShapeName()
{
return ShapeName;
}
public override double GetWeight()
{
return GetVolume() * Density;
}
public string RollDist()
{
string str_Output = "";
str_Output += GetShapeName() + "\t" +Parameter.ToString("#.##")+"\t"+Height.ToString("#.##")+"\t"+ Density.ToString("#.##") + "\t" + (Parameter * Parameter).ToString("#.##") + "\n";
return str_Output;
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Pyramid : Shape
{
//*********成員**********
private static int amount = 0;
private static string ShapeName = "金字塔";
private double length = 0;
private double height = 0;
//***********屬性**************
public double Length
{
set
{
if (value >= 0)
length = value;
else
length = 0;
}
get { return length; }
}
public double Height
{
set
{
if (value >= 0)
height = value;
else
height = 0;
}
get { return height; }
}
new public static int Amount
{
get { return amount; }
set { amount = value; }
}
//**********建構子********
public Pyramid(double l, double h, double d, string m)
{
Material = m;
Length = l;
Height = h;
Density = d;
shapetype = ShapeTypes.Types.Pyramid;
amount++;
}
~Pyramid()
{
amount--;
}
//***********方法**************
public override double GetVolume()
{
return Length * Length * Height / 3.0;
}//虛擬方法,可讓各子類別計算各自體積
public override string GetShapeName()
{
return ShapeName;
}
public override double GetWeight()
{
return GetVolume() * Density;
}
}
}
IRollable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
interface IRollable
{
string RollDist();//介面方法,滾動距離
}
}
沒有留言:
張貼留言