主程式:Form1.cs
父類別:Shape3D
子類別:Ball,Cube,Cylinder,Pyramid
靜態類別:GeoConstant
Form1.cs
using System;
using System.Collections;
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 Hw6
{
public partial class Form1 : Form
{
ArrayList arr=new ArrayList();
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)
{
int amount = Shape3D.Amount;
double density = densityArr[cbox_MaterialSelect.SelectedIndex];
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
arr.Add(new Ball(double.Parse(txt_Para1.Text), density));
break;
case 1:
arr.Add(new Cube(double.Parse(txt_Para1.Text), density));
break;
case 2:
arr.Add(new Cylinder(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text),density));
break;
case 3:
arr.Add(new Pyramid(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density));
break;
default:
break;
}
txt_Message.AppendText(((Shape3D)arr[amount]).ShapeProperty()+Environment.NewLine);
txt_AmountOfShape.Text = Shape3D.Amount.ToString();
}
private void btn_Calculate_Click(object sender, EventArgs e)
{
txt_Display.Clear();
for (int i = 0; i < Shape3D.Amount;i++ )
{
string str = (((Shape3D)arr[i]).ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
private void btnCompare_Click(object sender, EventArgs e)
{
txt_Display.Clear();
switch(cboCompareType.SelectedIndex)
{
case 0:
if (rdbAscent.Checked)
BubbleSort(arr, SortByVolumeAscent);
else
BubbleSort(arr, SortByVolumeDescent);
break;
case 1:
if (rdbAscent.Checked)
BubbleSort(arr, SortByWeightAscent);
else
BubbleSort(arr, SortByWeightDescent);
break;
default:
break;
}
for (int i = 0; i < Shape3D.Amount; i++)
{
string str = (((Shape3D)arr[i]).ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
private delegate bool CompareFunction(Shape3D a, Shape3D b);
private bool SortByVolumeAscent(Shape3D a, Shape3D b)
{
if (a.Volume() > b.Volume())
return true;
else
return false;
}
private bool SortByVolumeDescent(Shape3D a, Shape3D b)
{
if (a.Volume() < b.Volume())
return true;
else
return false;
}
private bool SortByWeightAscent(Shape3D a, Shape3D b)
{
if (a.Weight() > b.Weight())
return true;
else
return false;
}
private bool SortByWeightDescent(Shape3D a, Shape3D b)
{
if (a.Weight() < b.Weight())
return true;
else
return false;
}
private void BubbleSort(ArrayList arr, CompareFunction compare)
{
for (int pass = 0; pass < arr.Count; pass++)
{
for (int i = 0; i < arr.Count-1; i++)
{
if (compare((Shape3D)arr[i], (Shape3D)arr[i + 1]))
{
Shape3D temp;
temp = (Shape3D)arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
}
}
Shape3D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
abstract class Shape3D
{
protected double density;
private static int amount = 0;
public Shape3D()
{
density = 0;
amount++;
}
public Shape3D(double d)
{
Density = d;
amount++;
}
public double Density
{
get { return density; }
set
{
if (density < 0)
density = 0;
else
density = value;
}
}
public static int Amount
{
get { return amount; }
}
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();
}
}
GeoConstant
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
//靜態類別, 不能實體化物件
static class GeoConstant
{
public const double pi = 3.1415926;
}
}
Ball
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Ball : Shape3D
{
private double radius;
// private static double pi = 3.1415926;
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
}
public double Radius
{
get { return radius; }
set
{
if (radius < 0)
radius = 0;
else
radius = value;
}
}
public override double Volume()
{
return 4.0/3*GeoConstant.pi*radius*radius*radius;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Ball");
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
Cube
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Cube : Shape3D
{
private double side;
public Cube(double side, double density)
: base(density)
{
Side = side;
}
public double Side
{
get { return side; }
set
{
if (side < 0)
side = 0;
else
side = value;
}
}
public override double Volume()
{
return side*side*side;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Cube");
str += '\t';
str += string.Format("{0,8:F2}", side);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
Cylinder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Cylinder : Shape3D
{
private double radius;
private double height;
// private static double pi = 3.1415926;
public Cylinder(double radius, double height,double density)
: base(density)
{
Radius = radius;
Height = height;
}
public double Radius
{
get { return radius; }
set
{
if (radius < 0)
radius = 0;
else
radius = value;
}
}
public double Height
{
get { return height; }
set
{
if (height < 0)
height = 0;
else
height = value;
}
}
public override double Volume()
{
return GeoConstant.pi * radius * radius * height;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Cylinder");
str += '\t';
str += string.Format("{0,8:F2}", radius);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
}
}
Pyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hw6
{
class Pyramid : Shape3D
{
private double side;
private double height;
public Pyramid(double side, double height,double density)
: base(density)
{
Side = side;
Height = height;
}
public double Side
{
get { return side; }
set
{
if (side < 0)
side = 0;
else
side = value;
}
}
public double Height
{
get { return height; }
set
{
if (height < 0)
height = 0;
else
height = value;
}
}
public override double Volume()
{
return 1.0/3*side*side*height;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Pyramid");
str += '\t';
str += string.Format("{0,8:F2}", side);
str += '\t';
str += string.Format("{0,8:F2}", height);
return str;
}
}
}
沒有留言:
張貼留言