2015年6月18日 星期四
[2015][Quiz][Week12]Quiz06 - Team05
主程式:Form1.cs
父類別:Shape3D
子類別:Ball,Cube,Cylinder,Pyramid
靜態類別:GeoConstant
[2015][Quiz][Week12]Quiz06 - Team03
按我顯示
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;
}
}
}
}
}
2015年6月17日 星期三
[2015][Quiz][Week12]Quiz06 - Team02
< !--more-->
主程式:Form1.cs
父類別:Shape3D
子類別:Ball,Cube,Cylinder,Pyramid
靜態類別:GeoConstant
Form1
父類別:Shape3D
子類別:Ball,Cube,Cylinder,Pyramid
靜態類別:GeoConstant
Form1
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
{
private ArrayList shapeArr=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:
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;
}
txt_Message.AppendText(((Shape3D)shapeArr[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)shapeArr[i]).ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
private bool CompareByVolumeAscent(Shape3D a, Shape3D b)
{
if (a.Volume() > b.Volume())
return true;
else
return false;
}
private bool CompareByVolumeDescent(Shape3D a, Shape3D b)
{
if (a.Volume() < b.Volume())
return true;
else
return false;
}
private bool CompareByWeightAscent(Shape3D a, Shape3D b)
{
if (a.Weight() > b.Weight())
return true;
else
return false;
}
private bool CompareByWeightDescent(Shape3D a, Shape3D b)
{
if (a.Weight() < b.Weight())
return true;
else
return false;
}
private delegate bool CompareFunc(Shape3D a, Shape3D b);
private void BubbleSort(ArrayList arr, CompareFunc cmp)
{
for (int i = 0; i < arr.Count; i++)
{
for (int j = 0; j < arr.Count - 1; j++)
{
if (cmp((Shape3D)arr[j], (Shape3D)arr[j + 1]))
{
Shape3D temp;
temp = (Shape3D)arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
private void Sort()
{
switch (cBSort.SelectedIndex)
{
case 0:
if (rBtnAscent.Checked)
BubbleSort(shapeArr, CompareByWeightAscent);
else if (rBtnDescent.Checked)
BubbleSort(shapeArr, CompareByWeightDescent);
break;
case 1:
if (rBtnAscent.Checked)
BubbleSort(shapeArr, CompareByVolumeAscent);
else if (rBtnDescent.Checked)
BubbleSort(shapeArr, CompareByVolumeDescent);
break;
}
}
private void btnConcern_Click(object sender, EventArgs e)
{
Sort();
txt_Display.Clear();
for (int i = 0; i < Shape3D.Amount; i++)
{
string str = (((Shape3D)shapeArr[i]).ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
}
}
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();
}
}
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;
}
}
}
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;
}
}
2015年5月20日 星期三
[2015][Quiz][Week12]Quiz06 - Team06
程式分為七個部分
Form1 為視窗主程式.進行排序
ConstantTable 為抽象類別
定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape3D 為父類別
產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法
Form1 為視窗主程式.進行排序
ConstantTable 為抽象類別
定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape3D 為父類別
產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法
2015年5月14日 星期四
訂閱:
文章 (Atom)