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][Quiz][Week07]Quiz04 - 40075040H
按我顯示
PinBoard
public class PinBoard
{
public int rows ;
public int cols;
public double xInterval;
public double yInterval;
public MyPoint[,] PinArray = null;
public PinBoard(int row,int col,double deltaX,double deltaY)
{
rows = row ;
cols = col;
xInterval = deltaX;
yInterval = deltaY;
}
public void createPins()
{
PinArray=new MyPoint[rows ,cols];
for (int i = 0; i < rows ; i++)
for (int j = 0; j < cols; j++)
PinArray[i, j] = new MyPoint();
}
public void setPinsPositions()
{
for(int i=0 ; i < rows ; i++)
for(int j=0 ; j < cols ; j++)
{
PinArray[i, j].x = xInterval * j;
PinArray[i, j].y = yInterval * i;
}
}
}
Point
public class MyPoint
{
public double x;
public double y;
public double distanceTo(MyPoint p)
{
return (Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)));
}
}
Triangle
public class triangle
{
public MyPoint[] PointArray = new MyPoint[3];
public triangle(MyPoint p1,MyPoint p2,MyPoint p3)
{
PointArray[0] = p1;
PointArray[1] = p2;
PointArray[2] = p3;
}
public double distanceBetweenPoints(double x1,double y1,double x2,double y2)
{
return((Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
}
public void setPointArray(MyPoint[] pArray)
{
for (int i = 0; i < 3; i++)
pArray[i] = new MyPoint();
}
public double perimeter()
{
double s1 = PointArray[0].distanceTo(PointArray[1]);
double s2 = PointArray[1].distanceTo(PointArray[2]);
double s3 = PointArray[2].distanceTo(PointArray[0]);
return s1 + s2 + s3;
}
public double area()
{
double s1 = PointArray[0].distanceTo(PointArray[1]);
double s2 = PointArray[1].distanceTo(PointArray[2]);
double s3 = PointArray[2].distanceTo(PointArray[0]);
double s = (s1 + s2 + s3) / 2;
return(Math.Sqrt(s*(s-s1)*(s-s2)*(s-s3)));
}
public bool isValid()
{
double s1 = PointArray[0].distanceTo(PointArray[1]);
double s2 = PointArray[1].distanceTo(PointArray[2]);
double s3 = PointArray[2].distanceTo(PointArray[0]);
return ((s1 + s2 > s3) || (s3 + s2 > s1) || (s1 + s3 > s2));
}
public double radiusOfCircle()
{
double a = PointArray[0].distanceTo(PointArray[1]);
double b = PointArray[1].distanceTo(PointArray[2]);
double c = PointArray[2].distanceTo(PointArray[0]);
double cosa=(b*b+c*c-a*a)/(2*b*c);
double sina=Math.Sqrt(1-cosa*cosa);
return 0.5*a/sina;
}
}
Forms
public partial class Form1 : Form
{
public String mess;
public PinBoard MyBoard;
public triangle tri;
public Random rnd=new Random();
public Form1()
{
InitializeComponent();
}
private void btnGenerate_Click(object sender, EventArgs e)
{
MyBoard = new PinBoard(Convert.ToInt32(txtRow.Text), Convert.ToInt32(txtColumn.Text),Convert.ToDouble( txtXinterval.Text),Convert.ToDouble( txtYinterval.Text));
MyBoard.createPins();
MyBoard.setPinsPositions();
txtP1Col.Text = rnd.Next( Convert.ToInt32(txtColumn.Text)).ToString();
txtP2Col.Text = rnd.Next(Convert.ToInt32(txtColumn.Text)).ToString();
txtP3Col.Text = rnd.Next(Convert.ToInt32(txtColumn.Text)).ToString();
txtP1Row.Text = rnd.Next(Convert.ToInt32(txtRow.Text)).ToString();
txtP2Row.Text = rnd.Next(Convert.ToInt32(txtRow.Text)).ToString();
txtP3Row.Text = rnd.Next(Convert.ToInt32(txtRow.Text)).ToString();
txtP1X.Text = (Convert.ToInt32(txtP1Col.Text) * MyBoard.xInterval).ToString();
txtP2X.Text = (Convert.ToInt32(txtP2Col.Text) * MyBoard.xInterval).ToString();
txtP3X.Text = (Convert.ToInt32(txtP3Col.Text) * MyBoard.xInterval).ToString();
txtP1Y.Text = (Convert.ToInt32(txtP1Row.Text) * MyBoard.yInterval).ToString();
txtP2Y.Text = (Convert.ToInt32(txtP2Row.Text) * MyBoard.yInterval).ToString();
txtP3Y.Text = (Convert.ToInt32(txtP3Row.Text) * MyBoard.yInterval).ToString();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
MyPoint P1=new MyPoint();
MyPoint P2 = new MyPoint();
MyPoint P3 = new MyPoint();
P1.x = Convert.ToDouble(txtP1X.Text);
P1.y = Convert.ToDouble(txtP1Y.Text);
P2.x = Convert.ToDouble(txtP2X.Text);
P2.y = Convert.ToDouble(txtP2Y.Text);
P3.x = Convert.ToDouble(txtP3X.Text);
P3.y = Convert.ToDouble(txtP3Y.Text);
tri = new triangle(P1,P2,P3);
mess+=Environment.NewLine;
mess += "周長是:";
mess += tri.perimeter().ToString();
mess += (Environment.NewLine+"面積是:");
mess += tri.area().ToString();
mess += Environment.NewLine+"外接圓半徑是:";
mess += tri.radiusOfCircle().ToString();
txtDisplay.Text = mess;
}
}
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;
}
}
訂閱:
意見 (Atom)