執行檔的連結
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 static string strImform1 = "編號" + '\t' + "形狀" + '\t' + "邊長/半徑" + '\t' + "高" + '\t' + "密度" + '\n';
private static string strImform2 = "編號" + '\t' + "形狀" + '\t' + "邊長/半徑" + '\t' + "高" + '\t' + "密度" + '\t' + "體積" + '\t' + "重量" + '\n';
private static string strImform3 = "編號" + '\t' + "形狀" + '\t' + "邊長/半徑" + '\t' + "高" + '\t' + "密度" + '\t' + "滾動距離"+ '\n';
public Form1()
{
InitializeComponent();
txt_objdata.AppendText(strImform1);
txt_showVolume.AppendText(strImform2);
txt_showScoll.AppendText(strImform3);
}
//因形狀不同而改變出現的參數
private void cbox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_shape.SelectedIndex)
{
case 0:
lbl_para1.Text = "半徑";
txt_para2.Visible = false;
lbl_para2.Visible = false;
break;
case 1:
lbl_para1.Text = "邊長";
txt_para2.Visible = false;
lbl_para2.Visible = false;
break;
case 2:
lbl_para1.Text = "半徑";
txt_para2.Visible = true;
lbl_para2.Visible = true;
break;
case 3:
lbl_para1.Text = "邊長";
txt_para2.Visible = true;
lbl_para2.Visible = true;
break;
default:
break;
}
}
static double[] densityarr={2.7,7.87,11.3};
List shapeArr = new List();
//加入物件
private void btn_import_Click(object sender, EventArgs e)
{
if (CheckChoise())
{
double density = densityarr[cbox_material.SelectedIndex];
switch (cbox_shape.SelectedIndex)
{
case 0:
Ball ball = new Ball(double.Parse(txt_para1.Text), density);
shapeArr.Add(ball);
break;
case 1:
Cube cube = new Cube(double.Parse(txt_para1.Text), density);
shapeArr.Add(cube);
break;
case 2:
Cylinder cylinder = new Cylinder(double.Parse(txt_para1.Text), double.Parse(txt_para2.Text), density);
shapeArr.Add(cylinder);
break;
case 3:
Pyramid pyramid = new Pyramid(double.Parse(txt_para1.Text), double.Parse(txt_para2.Text), density);
shapeArr.Add(pyramid);
break;
default:
break;
}
//存入編號
shapeArr[shapeArr.Count - 1].Order = shapeArr.Count - 1;
//顯示各形狀的數量
ShapeNum();
//將物件加入清單
txt_objdata.AppendText(shapeArr[shapeArr.Count - 1].material() + '\n');
}
}
//刪除物件
private void btn_delete_Click(object sender, EventArgs e)
{
shapeArr[int.Parse(txt_deletenum.Text)] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
shapeArr.RemoveAt(int.Parse(txt_deletenum.Text));
//顯示各形狀的數量
ShapeNum();
txt_objdata.Text = strImform1;
foreach (var shape3D in shapeArr)
{
txt_objdata.AppendText(shape3D.material() + '\n');
}
}
//計算體積
private void btn_calculateV_Click(object sender, EventArgs e)
{
switch (cbox_sortmethod.SelectedIndex)
{
case 0:
shapeArr.Sort(CompareByOrder);
break;
case 1:
shapeArr.Sort(CompareByShape);
break;
case 2:
shapeArr.Sort(CompareByMaterial);
break;
case 3:
shapeArr.Sort(CompareByVolume);
break;
case 4:
shapeArr.Sort(CompareByWeight);
break;
default:
MessageBox.Show("未選擇排序方法,請重新選擇!!");
break;
}
//重新清空顯示欄,並重新讀取shapeArr
txt_showVolume.Text = strImform2;
foreach (var shape3D in shapeArr)
{
txt_showVolume.AppendText(shape3D.datadisplay());
}
}
//計算滑動距離
private void btn_calculateScroll_Click(object sender, EventArgs e)
{
txt_showScoll.Text = strImform3;
foreach (var shape3D in shapeArr)
{
IRollable rollable = shape3D as IRollable;
if (rollable != null)
txt_showScoll.AppendText(rollable.ShowRolldistance());
}
}
//不同的比較方法
private int CompareByOrder(Shape3D a, Shape3D b)
{
if (a.Order > b.Order)
return 1;
else
return -1;
}
private int CompareByShape(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.volume() > b.volume())
return 1;
else
return -1;
}
private int CompareByWeight(Shape3D a, Shape3D b)
{
if (a.weight() > b.weight())
return 1;
else
return -1;
}
//刪除物件後重新編號
private void btn_ReSortNum_Click(object sender, EventArgs e)
{
for (int i = 0; i < shapeArr.Count; i++)
{
shapeArr[i].Order = i;
}
txt_objdata.Text = null;
foreach (var shape3D in shapeArr)
{
txt_objdata.AppendText(shape3D.material() + '\n');
}
}
//檢查是否有任何方法或參數沒有被設定
private bool CheckChoise()
{
if (cbox_material.SelectedIndex < 0)
{
MessageBox.Show("未選擇任何材質,請重新選擇!!");
return false;
}
else if (cbox_shape.SelectedIndex < 0)
{
MessageBox.Show("未選擇任何形狀,請重新選擇!!");
return false;
}
else if (txt_para1.Text=="")
{
MessageBox.Show("未輸入任何參數,請重新輸入!!");
return false;
}
else if (cbox_shape.SelectedIndex >= 2 && txt_para2.Text=="")
{
MessageBox.Show("仍有參數未輸入,請輸入完畢!!");
return false;
}
else
return true;
}
private void ShapeNum()
{
txt_amount.Text = Convert.ToString(shapeArr.Count);
txt_ballnum.Text = Convert.ToString(Ball.Amount);
txt_cubenum.Text = Convert.ToString(Cube.Amount);
txt_cylindernum.Text = Convert.ToString(Cylinder.Amount);
txt_pyramidnum.Text = Convert.ToString(Pyramid.Amount);
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
abstract class Shape3D
{
//Fields
protected double density;
protected int order;
protected ShapeType.Type shapetype;
//Contructor
public Shape3D()
{
Density = 0;
}
public Shape3D(double density)
{
Density = density;
}
//Atributes
public double Density
{
get { return density;}
set
{
if (value <= 0)
density = 0;
else
density =value;
}
}
public int Order
{
get { return order; }
set { order = value; }
}
public int Shapetype
{
get { return (int)shapetype; }
}
//abstract method
public abstract double volume();
public abstract string material();
//method
public double weight()
{
return volume()*Density;
}
public string datadisplay()
{
string str=null;
str += material();
str += '\t';
str += String.Format("{0:F2}", volume());
str += '\t';
str += String.Format("{0:F2}",weight());
str += '\n';
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Ball:Shape3D,IRollable
{
//Fields
private double radius;
private string shapename = "Ball";
private static int amount = 0;
//contructor
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
shapetype = ShapeType.Type.Ball;
amount++;
}
//destructor
~Ball()
{
amount--;
}
//Atributes
public double Radius
{
get{return radius;}
set{radius=value;}
}
public static int Amount
{
get { return amount; }
}
//override method
public override double volume()
{
return 4.0 / 3 * GeoConstant.pi * Math.Pow(Radius, 3);
}
public override string material()
{
string str = null;
str += Convert.ToString(Order);
str += '\t';
str += shapename;
str += '\t';
str += Convert.ToString(Radius);
str += '\t';
str += '\t';
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
public double Rolldistance()
{
return Radius * Radius;
}
public string ShowRolldistance()
{
return material() + '\t' + String.Format("{0:F2}", Rolldistance()) + '\n';
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Cube : Shape3D
{
//Fields
private double side;
private string shapename = "Cube";
private static int amount = 0;
//contructor
public Cube(double side, double density)
: base(density)
{
Side = side;
shapetype = ShapeType.Type.Cube;
amount++;
}
//destructor
~Cube()
{
amount--;
}
//Atributes
public double Side
{
get { return side; }
set { side = value; }
}
public static int Amount
{
get { return amount; }
}
//override method
public override double volume()
{
return Math.Pow(Side, 3);
}
public override string material()
{
string str = null;
str += Convert.ToString(Order);
str += '\t';
str += shapename;
str += '\t';
str += Convert.ToString(Side);
str += '\t';
str += '\t';
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Cylinder : Shape3D,IRollable
{
//Fields
private double radius;
private double height;
private string shapename = "Cylinder";
private static int amount = 0;
//contructor
public Cylinder(double radius, double height, double density)
: base(density)
{
Radius = radius;
Height = height;
shapetype = ShapeType.Type.Cylinder;
amount++;
}
//destructor
~Cylinder()
{
amount--;
}
//Atributes
public double Radius
{
get{return radius;}
set{radius=value;}
}
public double Height
{
get { return height; }
set { height = value; }
}
public static int Amount
{
get { return amount; }
}
//override method
public override double volume()
{
return Height * GeoConstant.pi * Math.Pow(Radius, 2);
}
public override string material()
{
string str = null;
str += Convert.ToString(Order);
str += '\t';
str += shapename;
str += '\t';
str += Convert.ToString(Radius);
str += '\t';
str += '\t';
str += Convert.ToString(Height);
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
public double Rolldistance()
{
return Radius*5;
}
public string ShowRolldistance()
{
return material()+'\t' + String.Format("{0:F2}", Rolldistance())+'\n';
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class Pyramid : Shape3D
{
//Fields
private double side;
private double height;
private string shapename = "Pyramid";
private static int amount = 0;
//contructor
public Pyramid(double side, double height, double density)
: base(density)
{
Side = side;
Height = height;
shapetype = ShapeType.Type.Pyramid;
amount++;
}
//destructor
~Pyramid()
{
amount--;
}
//Atributes
public double Side
{
get { return side; }
set { side = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public static int Amount
{
get { return amount; }
}
//override method
public override double volume()
{
return 1.0 / 3 * Math.Pow(Side, 2) * Height;
}
public override string material()
{
string str = null;
str += Convert.ToString(Order);
str += '\t';
str += shapename;
str += '\t';
str += Convert.ToString(Side);
str += '\t';
str += '\t';
str += Convert.ToString(Height);
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
interface IRollable
{
double Rolldistance();
string ShowRolldistance();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
static class ShapeType
{
public enum Type { Unknown = 0, Ball, Cube, Cylinder, Pyramid };
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW8
{
class GeoConstant
{
public static double pi = 3.1415926;
}
}

沒有留言:
張貼留言