Shape3D
//可將程式碼放於此處,請至文章的html模式編輯
public class Shape3D
{
//欄位
protected double _density;
public static int Amount = 0;
//建構子
public Shape3D(double density)//有密度
{
_density = density;
Amount++;
}
public Shape3D()//沒密度
{
_density = 0;
Amount++;
}
//屬性
public double Density
{
set
{
if (value > 0)
_density = value;
else
return ;
}
get { return _density; }
}
//方法
public virtual double Volume()
{
return 0.0;
}
public double Weight()
{
return Volume() * _density;
}
public virtual string ShowProperty()
{
return "";
}
public string ShowResult()
{
string output = ShowProperty();
output += string.Format("{0,8:F2}", Density) + "\t"
+ string.Format("{0,8:F2}", Volume()) + "\t"
+ string.Format("{0,8:F2}", Weight());
return output;
}
}
Ball
//可將程式碼放於此處,請至文章的html模式編輯
class Ball:Shape3D
{
//欄位
private double _radius;
//建構子
public Ball(double density, double radius)
: base(density)
{
_radius = radius;
}
public Ball()
: base()
{
_radius = 0;
}
//屬性
public double Radius
{
//set { _radius = value; }
get { return _radius; }
}
//方法
public override double Volume()//計算體積
{
return 3.0/4.0 *Constant.Pi*_radius * _radius * _radius;
}
public override string ShowProperty()//秀屬性
{
string output="";
output += string.Format("{0:8}","Ball")+"\t" + string.Format("{0,8:F2}", _radius) + "\t" + string.Format("{0,8}","") + "\t";
return output;
}
}
Cube
//可將程式碼放於此處,請至文章的html模式編輯
class Cube:Shape3D
{
//欄位
private double _side;
//建構子
public Cube(double density, double radius)
: base(density)
{
_side = radius;
}
public Cube()
: base()
{
_side = 0;
}
//屬性
public double Side
{
//set { _radius = value; }
get { return _side; }
}
//方法
public override double Volume()//計算體積
{
return _side * _side * _side;
}
public override string ShowProperty()//秀屬性
{
string output = "";
output += string.Format("{0:8}", "Cube") + "\t" + string.Format("{0,8:F2}", _side) + "\t" + string.Format("{0,8}", "") + "\t";
return output;
}
}
Cylinder
//可將程式碼放於此處,請至文章的html模式編輯
class Cylinder:Shape3D
{
//欄位
private double _radius;
private double _hight;
//建構子
public Cylinder(double density, double radius, double hight)
: base(density)
{
_radius = radius;
_hight = hight;
}
public Cylinder()
: base()
{
_radius = 0;
_hight = 0;
}
//屬性
public double Radius
{
//set { _radius = value; }
get { return _radius; }
}
public double Hight
{
//set { _hight = value; }
get { return _hight; }
}
//方法
public override double Volume()//計算體積
{
return Constant.Pi*_radius * _radius * _hight;
}
public override string ShowProperty()//秀屬性
{
string output="";
output += string.Format("{0:8}","Cylinder")+"\t"+ string.Format("{0,8:F2}", _radius) +"\t" + string.Format("{0,8:F2}", _hight) + "\t";
return output;
}
}
Pryamid
//可將程式碼放於此處,請至文章的html模式編輯
class Pyramid:Shape3D
{
//欄位
private double _side;
private double _hight;
//建構子
public Pyramid(double density, double side, double hight)
: base(density)
{
_side = side;
_hight = hight;
}
public Pyramid()
: base()
{
_side = 0;
_hight = 0;
}
//屬性
public double Side
{
//set { _radius = value; }
get { return _side; }
}
public double Hight
{
//set { _hight = value; }
get { return _hight; }
}
//方法
public override double Volume()//計算體積
{
return (1.0/3.0) * _side * _side * _hight;
}
public override string ShowProperty()//秀屬性
{
string output="";
output += string.Format("{0:8}", "Pyramid") + "\t" + string.Format("{0,8:F2}", _side) + "\t" + string.Format("{0,8:F2}", _hight) + "\t";
return output;
}
}
Constant
//可將程式碼放於此處,請至文章的html模式編輯
class Constant
{
public static double Pi = 3.1415926;
}
Form1
//可將程式碼放於此處,請至文章的html模式編輯
public partial class Form1 : Form
{
//static Shape3D[] myshape = new Shape3D[100];
ArrayList myshape=new ArrayList();
public double[] densityInfo = { 2.7, 7.87, 11.3 };
public Form1()
{
InitializeComponent();
comboBoxMaterial.SelectedIndex = 0;
comboBoxShape.SelectedIndex = 0;
}
private void btnAdd_Click(object sender, EventArgs e)
{
double density=densityInfo[comboBoxMaterial.SelectedIndex];
switch(comboBoxShape.SelectedIndex)
{
case 0:
Ball ball = new Ball(density, Convert.ToDouble(txtPara1.Text));
myshape.Add(ball);
break;
case 1:
Cube cube = new Cube(density, Convert.ToDouble(txtPara1.Text));
myshape.Add(cube);
break;
case 2:
Cylinder cylinder = new Cylinder(density, Convert.ToDouble(txtPara1.Text), Convert.ToDouble(txtPara2.Text));
myshape.Add(cylinder);
break;
default:
Pyramid pyramid = new Pyramid(density, Convert.ToDouble(txtPara1.Text), Convert.ToDouble(txtPara2.Text));
myshape.Add(pyramid);
break;
}
string msg = ((Shape3D)myshape[Shape3D.Amount - 1]).ShowProperty();
txtProperty.AppendText( msg + Environment.NewLine);
txtAmount.Text= Shape3D.Amount.ToString();
}
private void btnCompute_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
for(int i=0;i shape2.Weight())
{
return true;
}
else return false;
}
private bool SortByWeightDecrease(Shape3D shape1, Shape3D shape2)
{
if (shape1.Weight() < shape2.Weight())
{
return true;
}
else return false;
}
private bool SortByVolIncrease(Shape3D shape1, Shape3D shape2)
{
if (shape1.Volume() > shape2.Volume())
{
return true;
}
else return false;
}
private bool SortByVolDecrease(Shape3D shape1, Shape3D shape2)
{
if (shape1.Volume() < shape2.Volume())
{
return true;
}
else return false;
}
private delegate bool Compare(Shape3D shape1, Shape3D shape2);
private void BubbleSort(ArrayList arr, Compare cmp)
{
for (int i = 0; i < arr.Count; i++)
{
for (int j = 0; j < arr.Count - 1; j++)
{
if (cmp((Shape3D)arr[i], (Shape3D)arr[j]))
{
Shape3D temp;
temp = (Shape3D)arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
沒有留言:
張貼留言