程式分為十個部分
Form1 為視窗主程式
Ball 為球的類別
紀錄球速.飛行距離.飛行角度.委託球的飛行狀況給裁判
BaseBallField 為球場資料類別
紀錄球場大.小角度.委託廠地資訊給裁判
Batter 為打者類別
紀錄打者打擊球速上限.打擊球的委託
Fielder 為防守者的父類別
紀錄防守範圍.角度的最大.小值和接球
CenterFielder.Pitcher.SecondBaseMan 為防守者的子類別
Pitcher較不同有多投球的委託
Umpire 為裁判的類別
Pitcher判定好球.界外球.接殺...等等
2015年6月29日 星期一
2015年6月25日 星期四
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;
}
}
2015年6月11日 星期四
2015年5月21日 星期四
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日 星期四
2015年5月13日 星期三
[2015][Quiz][Week11]Quiz05 - Team04
Form1.cs為視窗設計
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
2015年5月12日 星期二
2015年5月9日 星期六
[2015][Quiz][Week11]Quiz05 - Team06
程式分為七個部分
Form1 為視窗主程式
ConstantTable 為抽象類別
定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape 為父類別
產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法
Form1 為視窗主程式
ConstantTable 為抽象類別
定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape 為父類別
產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法
2015年5月7日 星期四
[2015][Homework]Team04 - Hw07
Form1.cs為視窗設計
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
2015年5月6日 星期三
2015年5月4日 星期一
2015年4月23日 星期四
2015年4月19日 星期日
[2015][Homework]Team03 - Hw06
按我顯示
using System;
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 Poker
{
public partial class Poker : Form
{
CardDeck newCards = new CardDeck();
public Poker()
{
InitializeComponent();
newCards.CreateDeck();
}
private void btnComBox_Click(object sender, EventArgs e) //ComboBox
{
switch(cboxSelect.SelectedIndex)
{
case 0:
txtDisplay.AppendText("ComboBox 依花色排列 (原始設定)\n");
newCards.ResetDeck();
ShowDeckMessage();
break;
case 1:
txtDisplay.AppendText("ComboBox 亂數洗牌\n");
newCards.ShuffleDeck();
ShowDeckMessage();
break;
case 2:
txtDisplay.AppendText("ComboBox 依點數大小排列\n");
newCards.ArrangeDeck();
ShowDeckMessage();
break;
default:
txtDisplay.AppendText("選擇一個選項\n");
break;
}
}
private void btnCheckBox_Click(object sender, EventArgs e) //CheckBox
{
if (chkBoxReset.Checked)
{
txtDisplay.AppendText("CheckBox 依花色排列 (原始設定)\n");
newCards.ResetDeck();
ShowDeckMessage();
}
if (chkBoxShuffle.Checked)
{
txtDisplay.AppendText("CheckBox 亂數洗牌\n");
newCards.ShuffleDeck();
ShowDeckMessage();
}
if (chkBoxArrange.Checked)
{
txtDisplay.AppendText("CheckBox 依點數大小排列\n");
newCards.ArrangeDeck();
ShowDeckMessage();
}
}
public void ShowDeckMessage() //顯示整副牌
{
string message = "";
for (int i = 0; i < 52; i++)
{
message += (newCards.cards[i].PrintSuit() + newCards.cards[i].PrintRank() + " ");
if ((i + 1) % 13 == 0)
{
message += "\n";
}
}
MessageBox.Show(message);
}
}
}
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Poker
{
class CardDeck
{
public PokerCard[] cards = new PokerCard[52]; //建立一副牌
Random rand = new Random(); //建立亂數
public void CreateDeck() //產生52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i] = new PokerCard();
}
}
public void ResetDeck() //重置52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].rank = (i % 13) + 1;
cards[i].suit = (i / 13) ;
}
}
public void ShuffleDeck() //亂數洗牌
{
PokerCard tempCard = new PokerCard();
for (int i = 0; i < 52; i++) //交換
{
tempCard.rank = cards[i].rank;
cards[i].rank = cards[rand.Next(52)].rank;
cards[rand.Next(52)].rank = tempCard.rank;
tempCard.suit = cards[i].suit;
cards[i].suit = cards[rand.Next(52)].suit;
cards[rand.Next(52)].suit = tempCard.suit;
}
}
public void ArrangeDeck() //依照點數與花色回復52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].rank = (i / 4) + 1;
cards[i].suit = (i % 4);
}
}
}
}
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Poker
{
class PokerCard
{
public int rank; //設定點數
public int suit; //設定花色
public string PrintRank() //數字輸出
{ //將1.11.12.13轉化為A.J.Q.K輸出
switch(rank)
{
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return rank.ToString();
}
}
public string PrintSuit() //花色輸出
{
string suitSign = null;
switch (suit)
{
case 0: //梅花
suitSign = "\u2663";
break;
case 1: //方塊
suitSign = "\u2666";
break;
case 2: //愛心
suitSign = "\u2665";
break;
case 3: //黑桃
suitSign = "\u2660";
break;
default:
break;
}
return suitSign;
}
}
}
2015年4月16日 星期四
[2015][Quiz]MidExam_60373012H
按我顯示
倒水程式 JugPuzzle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ted
{
class JugPuzzle
{
public int SettingBottleA;
public int SettingBottleB;
public int SettingTarget;
public TextBox Message;
public void Solve()
{
int bottleA = 0;
int bottleB = 0;
int delta;
if (SettingTarget <= (SettingBottleA + SettingBottleB))
{
if (SettingTarget % gcd(SettingBottleA, SettingBottleB) == 0)
{
Message.AppendText("開始進行分水步驟 \n" );
do
{
if (bottleA == 0)
{
bottleA = SettingBottleA;
Message.AppendText("A水杯裝滿\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else if (bottleB == SettingBottleB)
{
bottleB = 0;
Message.AppendText("B水杯清空");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else if (bottleA == SettingBottleA)
{
bottleA -= SettingBottleA;
bottleB += SettingBottleA;
Message.AppendText("A水杯倒進B水杯");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
if (bottleB > SettingBottleB)
{
delta = bottleB - SettingBottleB;
bottleB = SettingBottleB;
bottleA = delta;
Message.AppendText("B水杯裝滿,A水杯有剩下的水");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
}
else
{
bottleB += bottleA;
Message.AppendText("A水杯的水倒進B水杯");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
if (bottleB > SettingBottleB)
{
delta = bottleB - SettingBottleA;
bottleB = SettingBottleB;
bottleA = delta;
Message.AppendText("B水杯裝滿,A水杯有剩下的水");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else
{
bottleA = 0;
Message.AppendText("A水杯清空");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
}
} while ((bottleA != SettingTarget) && (bottleB != SettingTarget) && ((bottleA + bottleB) != SettingTarget));
}
else
{
Message.AppendText("此題無解 \n" );
}
}
else
{
Message.AppendText("超過最大水杯,永遠達不到 \n");
}
}
int gcd(int m, int n)
{
int temp = 0;
while (n != 0)
{
temp = m % n;
m = n;
n = temp;
}
return m;
}
}
}
倒水程式視窗介面
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
JugPuzzle jug = new JugPuzzle();
jug.Message = OutPutText;
jug.SettingBottleA = Convert.ToInt32(InputBoxA.Text);
jug.SettingBottleB = Convert.ToInt32(InputBoxB.Text);
jug.SettingTarget = Convert.ToInt32(SettingTargetBox.Text);
jug.Solve()
}
}
}
課堂上寫的月曆視窗程式
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
MyDateTime MyDateTime = new MyDateTime();
MyDateTime._year = Convert.ToInt16(YearOfInput.Text);
MyDateTime._month = Convert.ToInt16(MonthOfInput.Text);
MyDateTime._day = Convert.ToInt16(DayOfInput.Text);
MyDateTime.Message=OutPutText;
if (IsLeapYear.Checked)
{
if((MyDateTime._year % 4 == 0 && MyDateTime._year % 100 !=0) || MyDateTime._year % 400 ==0)
{
}
else
{
}
}
if(DayCheck.Checked)
{
if (MyDateTime.)
{
}
else
{
}
}
if(DayConvertToWeek.Checked)
{
}
if(Calendar.Checked)
{
}
{
MessageBox.Show("沒有勾選喔!!");
}
}
private void button6_Click(object sender, EventArgs e)
{
OutPutText.Clear();
}
private void MonthOfInput_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
課堂上寫的月曆類別程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class MyDateTime
{
public int _year;
public int _month;
public int _day;
public TextBox Message;
public bool IsLeap();
{
if((_year % 4 == 0 && _year % 100 !=0) || _year % 400 ==0)
{
return true;
}
else
{
return false;
}
}
public int DaysOfMonth(_month);
{
}
public bool IsValid();
{
if( year % 4 = 0)
{
return true;
}
else
{
return false;
}
}
public int WeekDay();
public string Print ();
{
}
}
}
以下請勿算分,不是在課堂上寫出來的。
月曆視窗程式
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 Calendar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDateTime MyDateTime = new MyDateTime();
MyDateTime._year = Convert.ToInt16(InputYear.Text);
MyDateTime._month = Convert.ToInt16(MonthComBox.Text);
MyDateTime._day = Convert.ToInt16(DateComBox.Text);
MyDateTime.Message = OutputText;
TextBox Message = MyDateTime.Message;
if (IsLeapCheck.Checked)
{
if (MyDateTime.IsLeap())
{
Message.AppendText(MyDateTime._year + "年是閏年");
}
else
{
Message.AppendText(MyDateTime._year + "是平年");
}
}
else if (DateCheck.Checked)
{
if (MyDateTime.IsValid())
{
Message.AppendText(MyDateTime.Print() + "合法");
}
else
{
Message.AppendText(MyDateTime.Print() + "非法");
}
}
else if (DayToWeek.Checked)
{
switch (MyDateTime.WeekDay())
{
case 0: Message.AppendText(MyDateTime.Print() + "是星期日");
break;
case 1: Message.AppendText(MyDateTime.Print() + "是星期一");
break;
case 2: Message.AppendText(MyDateTime.Print() + "是星期二");
break;
case 3: Message.AppendText(MyDateTime.Print() + "是星期三");
break;
case 4: Message.AppendText(MyDateTime.Print() + "是星期四");
break;
case 5: Message.AppendText(MyDateTime.Print() + "是星期五");
break;
case 6: Message.AppendText(MyDateTime.Print() + "是星期六");
break;
}
}
else if (Calendar.Checked)//還在修正算法
{
Message.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\t\n");
int[] array = new int[MyDateTime.DaysOfMonth()];
for (int j = 0; j < MyDateTime.WeekDay(); j++)
{
Message.AppendText("\t");
}
for (int i = 0; i < MyDateTime.DaysOfMonth(); i++)
{
array[i] = i + 1;
Message.AppendText(array[i].ToString());
Message.AppendText("\t");
if(i%7==0)
{
Message.AppendText("\n");
Message.AppendText("\t");
}
}
}
else
{
MessageBox.Show("沒有勾選喔!!");
}
}
private void button2_Click(object sender, EventArgs e)
{
OutputText.Clear();
}
}
}
月曆類別程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calendar
{
class MyDateTime
{
public int _year;
public int _month;
public int _day;
public TextBox Message;
public bool IsLeap()
{
if ((_year % 4 == 0) || (_year % 100 != 0 && _year % 400 == 0))
{
return true;
}
return false;
}
public int DaysOfMonth()
{
switch (_month)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
return 31;
// break;
case 4:
case 6:
case 8:
case 10:
return 30;
// break;
case 2:
if (IsLeap())
{
return 29;
}
return 28;
// break;
default:
return 0;
}
}
public bool IsValid()
{
if(_day <= DaysOfMonth())
{
return true;
}
return false;
}
public int WeekDay()
{
double w = ((_day + ((2.6 * _month) - 0.2) + (_year % 100) + ((_year % 100) / 4) + ((_year / 100) / 4) - 2 * (_year / 100)) % 7);
int x = Convert.ToInt16(Math.Floor(w));
return x;
}
public string Print()
{
return _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString();
}
}
}
[2015][Quiz]MidExam_40073037H
Form1.cs
JugPuzzle.cs第二題
按我顯示
按我顯示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinJugPuzzle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_solve_Click(object sender, EventArgs e)
{
JugPuzzle jug=new JugPuzzle();
jug.Message = txt_Message;
jug.CupOfJugA=Convert.ToInt16(txt_CupOfJugA.Text);
jug.CupOfJugB=Convert.ToInt16(txt_CupOfJugB.Text);
jug.Target=Convert.ToInt16(txt_Target.Text);
jug.solve();
}
private void btn_Clear_Click(object sender, EventArgs e)
{
txt_Message.Text = "";
}
}
}
JugPuzzle.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinJugPuzzle
{
class JugPuzzle
{
public int CupOfJugA;
public int CupOfJugB;
public int Target;
public TextBox Message;
public int CupA=0;
public int CupB=0;
public void solve()
{
if(!IsSolved(CupOfJugA,CupOfJugB))//檢查有沒有解答
{
Message.AppendText("無解,你被唬了,Boom\n");
return;
}
Message.AppendText("開始進行分水步驟\n");
Message.AppendText("====================\n");
while(true)
{
if (CupA==0)//位置:容器A沒水,容器B不管他
{
CupA+=CupOfJugA;
Message.AppendText("將容器A裝滿\n");
Action();
if(Target==CupA||(CupA+CupB)==Target)
{
break;
}
//到達:容器A水滿,容器B不管他
}
else if(CupA==CupOfJugA)//位置:容器A水滿,容器B不管他
{
if(CupA<=(CupOfJugB-CupB))//
{
CupB+=CupA;
CupA=0;
Message.AppendText("將容器A倒進容器B\n");
Action();
}
else if(CupA>(CupOfJugB-CupB))
{
CupA-=(CupOfJugB-CupB);
CupB=CupOfJugB;
Message.AppendText("將容器A的水道進容器B,直到B滿為止\n");
Action();
}
}
else if(CupB==CupOfJugB)
{
CupB=0;
Message.AppendText("將容器B的水倒光\n");
Action();
if(Target==CupA||(CupA+CupB)==Target)
{
break;
}
}
else if(CupA!=0&&CupB==0)
{
if(CupA<=(CupOfJugB-CupB))
{
CupB+=CupA;
CupA=0;
Message.AppendText("將容器A倒進容器B\n");
Action();
}
else if(CupA>(CupOfJugB-CupB))
{
CupA-=(CupOfJugB-CupB);
CupB=CupOfJugB;
Message.AppendText("將容器A的水道進容器B,直到B滿為止\n");
Action();
}
}
}
Message.AppendText("**********************\n");
Message.AppendText("**********************\n");
Message.AppendText("將容器A與一起放在磅秤上,即為所求炸彈拆除");
}
public double gcd(int Num1, int Num2)
{
if (Num1 % Num2 == 0)
{
return Num2;
}
return gcd(Num2, (Num1 % Num2));
}
public bool IsSolved(int CupOfJugA, int CupOfJugB)
{
if (Target % gcd(CupOfJugA, CupOfJugB) != 0 || (CupOfJugA + CupOfJugB) < Target)
{
return false;
}
else
return true;
}
public void Action()
{
Message.AppendText("(A,B)=("+CupA.ToString()+','+CupB.ToString()+')');
Message.AppendText("\n");
Message.AppendText("====================================\n");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace exam_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lal_year_Click(object sender, EventArgs e)
{
}
private void lal_month_Click(object sender, EventArgs e)
{
}
private void lal_day_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn_run_Click(object sender, EventArgs e)
{
MyDAteTime mdt = new MyDAteTime();
mdt._year = int.Parse(txt_year.Text);
mdt._month = Convert.ToInt16( cob_month.SelectedItem.ToString());
mdt._Day = Convert.ToInt16(cob_day.SelectedItem.ToString());
string str = "";
if (rbtn_IsLeap.Checked)
{
str = Convert.ToString(mdt._year);
if (!mdt.IsLeap())
{
txt_Display.Text = str + "年是平年";
}
else
{
txt_Display.Text = str + "年是閏年";
}
}
if (rbtn_CheckDay.Checked)
{
str = "";
if (mdt.IsValid())
{
str += mdt.Print();
str += "合法";
txt_Display.Text = str;
}
else
{
str += mdt.Print();
str += "不合法";
txt_Display.Text = str;
}
}
/*if (rbtn_DatToWeek.Checked)
{
str += mdt.Print();
switch (mdt.WeekDay())
{
case 0:
str += "星期一";
break;
case 1:
str += "星期二";
break;
case 2:
str += "星期三";
break;
case 3:
str += "星期四";
break;
case 4:
str += "星期五";
break;
case 5:
str += "星期六";
break;
case 6:
str += "星期日";
break;
default:
str += "";
break;
}
txt_Display.Text = str;
}*/
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_Display.Text = "";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class MyDAteTime
{
public int _year;
public int _month;
public int _Day;
public bool IsLeap()
{
if ((_year % 4 == 0 && _year % 100 != 0 )|| _year % 400 == 0)
{
return true;
}
else
{
return false;
}
}
public bool IsValid()
{
if (_Day <= DaysOfMonth())
{
return true;
}
else
{
return false;
}
}
public int DaysOfMonth()
{
switch (_month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
return 31;
}
case 2:
if (IsLeap())
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
{
return 30;
}
default:
return 0;
}
}
public string Print()
{
string str = "";
str += _year.ToString();
str += '/';
str += _month.ToString();
str += '/';
str += _Day.ToString();
return str;
}
public int WeekDay()
{
int Y = _year;
int d = _Day;
int m = _month;
double w;
if (m == 1 || m == 2)
{
Y -= 1;
}
double y = Y % 100;
double c = Y / 100;
w = (d + Convert.ToInt16(Math.Floor(2.6 * m - 0.2)) + y + Convert.ToInt16(Math.Floor(y / 4)) + Convert.ToInt16(Math.Floor( c/ 4))-Convert.ToInt16( 2*c))/7;
if (w < 0)
{
w += 7;
return Convert.ToInt16(w);
}
else
{
return Convert.ToInt16(w);
}
}
}
2015年4月13日 星期一
2015年4月12日 星期日
2015年4月11日 星期六
[2015][Quiz][Week07]Quiz04-40173008H
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 Quiz4
{
public partial class pinboard : Form
{
PinBoard board = new PinBoard(); //建立板子
public pinboard()
{
InitializeComponent();
}
private void btn_calculate_Click(object sender, EventArgs e)
{
Triangle tri = new Triangle();
int[] row = new int[3];
int[] col = new int[3];
col[0] = int.Parse(txtCol1.Text);
row[0] = int.Parse(txtRow1.Text);
col[1] = int.Parse(txtCol2.Text);
row[1] = int.Parse(txtRow2.Text);
col[2] = int.Parse(txtCol3.Text);
row[2] = int.Parse(txtRow3.Text);
for (int i = 0; i <= 2; i++)
{
tri.pointArray[i] = board.pinArray[row[i], col[i]];
}
board.SetPinsPosition();
txtX1.Text = tri.pointArray[0].xCoordinate.ToString();
txtY1.Text = tri.pointArray[0].yCoordinate.ToString();
txtX2.Text = tri.pointArray[1].xCoordinate.ToString();
txtY2.Text = tri.pointArray[1].yCoordinate.ToString();
txtX3.Text = tri.pointArray[2].xCoordinate.ToString();
txtY3.Text = tri.pointArray[2].yCoordinate.ToString();
if (!tri.ConditionsOfTriangle())
txt_Display.AppendText("以上三點無法構成三角形,重新產生三點\n");
double Perimeter=tri.Perimeter();
txt_Display.AppendText(DisplayStringPerimeter(Perimeter));
double Area = tri.Area();
txt_Display.AppendText(DisplayStringArea(Area));
double radius = tri.RadiusOfCircumcircle();
txt_Display.AppendText(DisplayStringradius(radius));
}
private string DisplayStringValue(double Value)
{
string StrDisplay = "";
StrDisplay += Value;
StrDisplay += "\n";
return StrDisplay;
}
private string DisplayStringPerimeter(double Perimeter)
{
string StrDisplay = "";
StrDisplay += "周長";
StrDisplay += Perimeter;
StrDisplay += "\n";
return StrDisplay;
}
private string DisplayStringArea(double Area)
{
string StrDisplay = "";
StrDisplay += "面積";
StrDisplay += Area;
StrDisplay += "\n";
return StrDisplay;
}
private string DisplayStringradius(double radius)
{
string StrDisplay = "";
StrDisplay += "外接圓半徑";
StrDisplay += radius;
StrDisplay += "\n";
return StrDisplay;
}
private void button1_Click(object sender, EventArgs e)
{
board.rows = int.Parse(txt_rows.Text);
board.cols = int.Parse(txt_cols.Text);
board.rowInterval = double.Parse(txt_rowInterval.Text);
board.colInterval = double.Parse(txt_colInterval.Text);
Triangle tri = new Triangle(); //建立三角形
Random rand = new Random(); //產生亂數
board.CreatePins();
int[] row = new int[3];
int[] col = new int[3];
for(int i=0;i<=2;i++)
{
row[i] = rand.Next(board.rows);
col[i] = rand.Next(board.cols);
tri.pointArray[i] = board.pinArray[row[i], col[i]];
}
txtCol1.Text = col[0].ToString();
txtRow1.Text = row[0].ToString();
txtCol2.Text = col[1].ToString();
txtRow2.Text = row[1].ToString();
txtCol3.Text = col[2].ToString();
txtRow3.Text = row[2].ToString();
board.SetPinsPosition();
txtX1.Text=tri.pointArray[0].xCoordinate.ToString();
txtY1.Text=tri.pointArray[0].yCoordinate.ToString();
txtX2.Text=tri.pointArray[1].xCoordinate.ToString();
txtY2.Text=tri.pointArray[1].yCoordinate.ToString();
txtX3.Text=tri.pointArray[2].xCoordinate.ToString();
txtY3.Text=tri.pointArray[2].yCoordinate.ToString();
if (!tri.ConditionsOfTriangle())
txt_Display.AppendText("以上三點無法構成三角形,重新產生三點\n");
}
}
}
Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class Point
{
public double xCoordinate;
public double yCoordinate;
public double DistanceBetweenTwoPoints(Point pin) //計算該點和某點之間的距離
{
double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - this.xCoordinate), 2) + Math.Pow((pin.yCoordinate - this.yCoordinate), 2));
return Distance;
}
}
}
PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class PinBoard
{
public int rows = 0;
public int cols = 0;
public Double rowInterval = 0;
public Double colInterval = 0;
public Point[,] pinArray = null;
public void CreatePins() //產生矩陣各點之位置
{
pinArray = new Point[rows, cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
pinArray[i, j] = new Point();
}
}
}
public void SetPinsPosition() //依行列間距計算出各點x.y之座標
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
pinArray[i, j].yCoordinate = (i * rowInterval);
pinArray[i, j].xCoordinate = (j * colInterval);
}
}
}
}
}
Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class Triangle
{
public Point[] pointArray = new Point[3]; //建立三角形的三個點
public bool ConditionsOfTriangle()//判斷是否符合構成三角形的條件
{
double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
double temp = 0;
while (true)
{
if (lengthB > lengthA) //將lengthA變為最長邊
{
temp = lengthB;
lengthB = lengthA;
lengthA = temp;
}
else if (lengthC > lengthB)//lengthC變為最短邊
{
temp = lengthC;
lengthC = lengthB;
lengthB = temp;
}
else
{
break;
}
}
if (lengthA < (lengthB + lengthC)) //三角形兩邊和大於第三邊
{
return true;
}
else
{
return false;
}
}
public double Perimeter() //計算三角形的周長
{
//由DistanceTo()計算兩點間的距離算出三角形三邊之邊長
double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
double Perimeter = lengthA + lengthB + lengthC;
return Perimeter;
}
public double Area()
{
double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
double Perimeter = lengthA + lengthB + lengthC;
//以海龍公式計算三角形面積
double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - lengthA) * (0.5 * Perimeter - lengthB) * (0.5 * Perimeter - lengthC));
return Area;
}
public double RadiusOfCircumcircle()
{
double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
//由餘弦公式計算出角A之餘弦值
double cosA = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC);
double sinA = Math.Sqrt(1 - cosA * cosA);
//求得外接圓半徑
double radius = 0.5 * lengthA / sinA;
return radius;
}
}
}
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz4
{
static class Program
{
///
/// 應用程式的主要進入點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new pinboard());
}
}
}
2015年4月9日 星期四
[2015][Quiz][Week07]Quiz04-60373006H
namespace Quiz_4_9
{
public class Point//Point 類別
{
public double PointX;
public double PointY;
//計算輸入點與成員變數的距離
public double DistanceTo(Point Input)
{
return Math.Sqrt((Input.PointX - PointX) * (Input.PointX - PointX) + (Input.PointY - PointY) * (Input.PointY - PointY));
}
}
}
namespace Quiz_4_9
{
class PinBoard //釘版類別
{
public int Row;//列
public int Column;//行
public double RowInterval;
public double ColumnInterval;
public Point[,] PinArray;
public void CreatPinArray()
{
PinArray = new Point[Row, Column];
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Column; j++)
PinArray[i, j] = new Point();
}
}
public void SetPointPostion()
{
for (int i = 0; i < Row; )
{
for (double ValueRow = 0; i < Row; i++, ValueRow += RowInterval)
{
for (int j = 0; j < Column; )
{
for (double ValueCol = 0; j < Column; j++, ValueCol += ColumnInterval)
{
PinArray[i, j].PointX = ValueRow;
PinArray[i, j].PointY = ValueCol;
}
}
}
}
}
}
}
namespace Quiz_4_9
{
class Triangle //三角形類別
{
//成員三點
public Point[] PointofTriangle = new Point[3];
public bool IsRight()
{
double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
double s3 = PointofTriangle[0].DistanceTo(PointofTriangle[2]);
if ((s1 + s2 > s3) && (s2 + s3 > s1) && (s1 + s3 > s2))
return true;
else
return false;
}//判斷三角形是否合法
public double Preimeter()
{
double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
double s3 = PointofTriangle[0].DistanceTo(PointofTriangle[2]);
return (s1 + s2 + s3);
}//周長
public double Area()
{
double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
double s3 = PointofTriangle[2].DistanceTo(PointofTriangle[0]);
double s = (s1 + s2 + s3) / 2;
return Math.Sqrt(s * (s - s1) * (s - s2) * (s - s3));
}//面積
public double RadiusOfCircumcircle()
{
double a = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
double b = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
double c = PointofTriangle[2].DistanceTo(PointofTriangle[0]);
double cosAlpha = (b * b + c * c - a * a) / (2 * b * c);
double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha);
return (0.5 * a / sinAlpha);
}//外接圓半徑
}
}
namespace Quiz_4_9
{
public partial class Form1 : Form
{
private PinBoard board;
Triangle triangle;
Random rand;
public Form1()
{
InitializeComponent();
board = new PinBoard();
triangle = new Triangle();
rand = new Random();
}
//btn_GenerateTriangle功能創造PinArray
private void btn_GenerateTriangle_Click(object sender, EventArgs e)
{
if (txt_RowKeyin.Text != "" && txt_ColumnKeyin.Text != "" && txt_RowIntervalKeyin.Text != "" && txt_ColumnIntervalKeyin.Text != "")
{
//讀取txt
board.Row = Convert.ToInt16(txt_RowKeyin.Text);
board.Column = Convert.ToInt16(txt_ColumnKeyin.Text);
board.RowInterval = Convert.ToDouble(txt_RowIntervalKeyin.Text);
board.ColumnInterval = Convert.ToDouble(txt_ColumnIntervalKeyin.Text);
//創造PinBoard
board.CreatPinArray();
board.SetPointPostion();
PrintPoint();
}
else
MessageBox.Show("缺參數");
}
//亂數選擇三點作為三角形,並將三點塞進Triangle類別中,同時print出來
private void PrintPoint()
{
//亂數選點 && 塞進txt
txt_Point1Row.Text = rand.Next(board.Row).ToString();
txt_Point1Column.Text = rand.Next(board.Column).ToString();
txt_Point2Row.Text = rand.Next(board.Row).ToString();
txt_Point2Column.Text = rand.Next(board.Column).ToString();
txt_Point3Row.Text = rand.Next(board.Row).ToString();
txt_Point3Column.Text = rand.Next(board.Column).ToString();
//用選點結果,提出PinBoard點,並塞進Triangle類別
triangle.PointofTriangle[0] = board.PinArray[Convert.ToInt16(txt_Point1Row.Text), Convert.ToInt16(txt_Point1Column.Text)];
triangle.PointofTriangle[1] = board.PinArray[Convert.ToInt16(txt_Point2Row.Text), Convert.ToInt16(txt_Point2Column.Text)];
triangle.PointofTriangle[2] = board.PinArray[Convert.ToInt16(txt_Point3Row.Text), Convert.ToInt16(txt_Point3Column.Text)];
//顯示三角形的三點
txt_Point1X.Text = triangle.PointofTriangle[0].PointX.ToString();
txt_Point1Y.Text = triangle.PointofTriangle[0].PointY.ToString();
txt_Point2X.Text = triangle.PointofTriangle[1].PointX.ToString();
txt_Point2Y.Text = triangle.PointofTriangle[1].PointY.ToString();
txt_Point3X.Text = triangle.PointofTriangle[2].PointX.ToString();
txt_Point3Y.Text = triangle.PointofTriangle[2].PointY.ToString();
}
//將Triangle類別中的數字提出來做運算,並放進txtbox裡。
private void btn_Calulate_Click(object sender, EventArgs e)
{
//判斷三點是否為空
if (txt_Point1X.Text != "" && txt_Point1Y.Text != "" && txt_Point2X.Text != "" &&
txt_Point2Y.Text != "" && txt_Point3X.Text != "" && txt_Point3Y.Text != "")
{//判斷手動輸入數字是否超過板子大小
if (Convert.ToInt16(txt_Point1Row.Text) < board.Row && Convert.ToInt16(txt_Point1Column.Text) < board.Column &&
Convert.ToInt16(txt_Point2Row.Text) < board.Row && Convert.ToInt16(txt_Point2Column.Text) < board.Column &&
Convert.ToInt16(txt_Point3Row.Text) < board.Row && Convert.ToInt16(txt_Point3Column.Text) < board.Column)
{
txt_ResultPrint.Text = "";
//為手動輸入所以必須再寫一次
triangle.PointofTriangle[0] = board.PinArray[Convert.ToInt16(txt_Point1Row.Text), Convert.ToInt16(txt_Point1Column.Text)];
triangle.PointofTriangle[1] = board.PinArray[Convert.ToInt16(txt_Point2Row.Text), Convert.ToInt16(txt_Point2Column.Text)];
triangle.PointofTriangle[2] = board.PinArray[Convert.ToInt16(txt_Point3Row.Text), Convert.ToInt16(txt_Point3Column.Text)];
if (triangle.IsRight() == true)
{
txt_ResultPrint.Text += "周長:";
txt_ResultPrint.Text += triangle.Preimeter().ToString() + Environment.NewLine;
txt_ResultPrint.Text += "面積:";
txt_ResultPrint.Text += triangle.Area().ToString() + Environment.NewLine;
txt_ResultPrint.Text += "外接圓半徑:";
txt_ResultPrint.Text += triangle.RadiusOfCircumcircle().ToString() + Environment.NewLine;
}
else
txt_ResultPrint.Text = "the triangle is illegal";
}
else
MessageBox.Show("輸入數字超過板子大小");
}
else
MessageBox.Show("缺參數");
}
}
}
[2015][Homework]Team03 - Hw03
namespace practice
{
class Test
{
static void KeyInPara(MyDateTime date)
{
int year;
int month;
int day;
do
{
Console.WriteLine("輸入一個西元年份");
year = Convert.ToInt16(Console.ReadLine());
if (year > 0)
date.Year = year;
else
Console.WriteLine("重新輸入");
} while (date.Year < 0);
do
{
Console.WriteLine("輸入一個西元月份");
month = Convert.ToInt16(Console.ReadLine());
if (month > 0)
date.Month = month;
else
Console.WriteLine("重新輸入");
} while (date.Month < 0);
do
{
Console.WriteLine("輸入一個西元日期");
day = Convert.ToInt16(Console.ReadLine());
if (month > 0)
date.Day = day;
else
Console.WriteLine("\n重新輸入");
} while (date.Day < 0);
}
static void DetermineWeeks(MyDateTime date,int year, int month, int day)
{
Console.WriteLine("{0}/{1}/{2} : ", year,month,day);
switch(date.DetermineWeek(year,month,day))
{
case 0:
Console.WriteLine("星期日");
break;
case 1:
Console.WriteLine("星期一");
break;
case 2:
Console.WriteLine("星期二");
break;
case 3:
Console.WriteLine("星期三");
break;
case 4:
Console.WriteLine("星期四");
break;
case 5:
Console.WriteLine("星期五");
break;
case 6:
Console.WriteLine("星期六");
break;
}
}
static void Main(string[] args)
{
MyDateTime date = new MyDateTime();
string iscontinue="y";
do
{
KeyInPara(date);
Console.WriteLine("====================我是分隔線====================");
Console.WriteLine("{0}/{1}/{2}", date.Year, date.Month, date.Day);
if (date.DetermineLeapYear(date.Year) == true)
{
Console.WriteLine("{0}是閏年", date.Year);
Console.WriteLine("{0}年有366天", date.Year);
}
else
{
Console.WriteLine("{0}是平年", date.Year);
Console.WriteLine("{0}年有365天", date.Year);
}
Console.WriteLine("{0}/{1}月 有{2}天", date.Year, date.Month, date.CountDayofMonth(date.Year, date.Month));
DetermineWeeks(date, date.Year, date.Month, date.Day);
Console.WriteLine("====================我是分隔線====================");
Console.WriteLine("\n是否繼續(y)");
iscontinue = Console.ReadLine();
} while (iscontinue == "y");
Console.WriteLine("按任意鍵結束");
Console.ReadKey();
}
}
}
namespace practice
{
class MyDateTime
{
public int Year;
public int Month;
public int Day;
public bool DetermineLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //可被4整除 && 不可被100整除 ||可被400整除
return true;
return false;
}
public int CountDayofMonth(int year, int month)
{
if ((1 <= month) && (month <= 12) && month != 2)
{
if (month <= 7)
{
if ((month % 2) == 1)
return 31;
else
return 30;
}
else
{
if ((month % 2) == 1)
return 30;
else
return 31;
}
}
else
if (DetermineLeapYear(year) == true)
return 29;
else
return 28;
}
public int DetermineWeek(int year, int month, int day)
{
double frontofyears, behindofyear;
double week;
frontofyears = year / 100;
behindofyear = year % 100;
if (month == 1 || month == 2)
{
if (month == 1)
{
month = 13;
year--;
}
else
{
month = 14;
year--;
}
}
week = behindofyear + Math.Floor(behindofyear / 4) + Math.Floor(frontofyears / 4) - 2 * frontofyears +
Math.Floor((26 * ((double)month + 1)) / 10) + day - 1;
if (week < 0)
week = (week % 7 + 7) % 7;
else
week %= 7;
if (month == 13)
{
month = 1;
year++;
}
else
{
month = 2;
year++;
}
return (int)week;
}
public bool DetermineDayIslegal(int year,int month,int day)
{
if (year < 0)
return false;
else
if (month < 1 || month > 12)
return false;
else
if (day > CountDayofMonth(year, month))
return false;
else
return true;
}
public int CountDayBetweenOther(int year, int month, int day, int year2, int month2, int day2)
{
int sumOfDay = 0;
for (int i = year2; year < i; i--)
{
if (DetermineLeapYear(year++) == true)
sumOfDay += 366;
else
sumOfDay += 365;
}
for (int i = month2 - 1; i >= 1; i--)
sumOfDay += CountDayofMonth(year2, i);
for (int i = month - 1; i >= 1; i--)
sumOfDay -= CountDayofMonth(year2, i);
return sumOfDay + day2 - day - 1;
}
}
}
[2015][Quiz][Week07]Quiz04-R02945022
Program為主程式
Form1為視窗程式,包含輸入與輸出
Point為類別,計算兩點距離
PinBoard為類別,產生格點與格點座標
Triangle為類別,產生triangle三點座標,計算周長、面積與外接圓半徑
Form1為視窗程式,包含輸入與輸出
Point為類別,計算兩點距離
PinBoard為類別,產生格點與格點座標
Triangle為類別,產生triangle三點座標,計算周長、面積與外接圓半徑
[2015][Quiz][Week07] Quiz4 - 40173027H
程式分為五個部分
Form1為視窗程式
Program為主程式
Point為類別 點的x.y座標和到另一點的距離
PinBoard為類別 板子的大小.間距.創造點和設定點的位置
Triangle為類別 三個大頭針的位置.計算是否構成三角形.其周長.面積.外接圓半徑
Form1
Program
Point
PinBoard
Triangle
Form1為視窗程式
Program為主程式
Point為類別 點的x.y座標和到另一點的距離
PinBoard為類別 板子的大小.間距.創造點和設定點的位置
Triangle為類別 三個大頭針的位置.計算是否構成三角形.其周長.面積.外接圓半徑
Form1
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 Quiz4
{
public partial class Form1 : Form
{
PinBoard board = new PinBoard(); //建立板子
public Form1()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void btnTriangle_Click(object sender, EventArgs e)
{
Triangle tri = new Triangle(); //建立三角形
Random rand = new Random(); //產生亂數
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = double.Parse(txtXInterval.Text);
board.columnInterval = double.Parse(txtYInterval.Text);
board.CreatePins();
board.SetPinsPosition();
for (int i = 0; i < 3; i++)
{
tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)]; //以亂數產生三角形的三個點
}
txtP1Row.Text = (Convert.ToString(tri.pointArray[0].xCoordinate / board.rowInterval));
txtP1Col.Text = (Convert.ToString(tri.pointArray[0].yCoordinate / board.columnInterval));
txtP1XInt.Text = (Convert.ToString(tri.pointArray[0].xCoordinate));
txtP1YInt.Text = (Convert.ToString(tri.pointArray[0].yCoordinate));
txtP2Row.Text = (Convert.ToString(tri.pointArray[1].xCoordinate / board.rowInterval));
txtP2Col.Text = (Convert.ToString(tri.pointArray[1].yCoordinate / board.columnInterval));
txtP2XInt.Text = (Convert.ToString(tri.pointArray[1].xCoordinate));
txtP2YInt.Text = (Convert.ToString(tri.pointArray[1].yCoordinate));
txtP3Row.Text = (Convert.ToString(tri.pointArray[2].xCoordinate / board.rowInterval));
txtP3Col.Text = (Convert.ToString(tri.pointArray[2].yCoordinate / board.columnInterval));
txtP3XInt.Text = (Convert.ToString(tri.pointArray[2].xCoordinate));
txtP3YInt.Text = (Convert.ToString(tri.pointArray[2].yCoordinate));
}
private void btnCalculate_Click(object sender, EventArgs e)
{
Triangle tri = new Triangle(); //建立三角形
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = double.Parse(txtXInterval.Text);
board.columnInterval = double.Parse(txtYInterval.Text);
board.CreatePins();
board.SetPinsPosition();
for (int i = 0; i < 3; i++)
{
tri.pointArray[i] = new Point();
}
tri.pointArray[0].xCoordinate = (int.Parse(txtP1Row.Text)) * board.rowInterval;
tri.pointArray[0].yCoordinate = (int.Parse(txtP1Col.Text)) * board.columnInterval;
tri.pointArray[1].xCoordinate = (int.Parse(txtP2Row.Text)) * board.rowInterval;
tri.pointArray[1].yCoordinate = (int.Parse(txtP2Col.Text)) * board.columnInterval;
tri.pointArray[2].xCoordinate = (int.Parse(txtP3Row.Text)) * board.rowInterval;
tri.pointArray[2].yCoordinate = (int.Parse(txtP3Col.Text)) * board.columnInterval;
txtP1XInt.Text = (Convert.ToString(tri.pointArray[0].xCoordinate));
txtP1YInt.Text = (Convert.ToString(tri.pointArray[0].yCoordinate));
txtP2XInt.Text = (Convert.ToString(tri.pointArray[1].xCoordinate));
txtP2YInt.Text = (Convert.ToString(tri.pointArray[1].yCoordinate));
txtP3XInt.Text = (Convert.ToString(tri.pointArray[2].xCoordinate));
txtP3YInt.Text = (Convert.ToString(tri.pointArray[2].yCoordinate));
if (tri.IsRight())
{
txtPrint.Text = null;
txtPrint.AppendText("三角形的周長是:" + Convert.ToString(tri.Perimeter()) + "\n");
txtPrint.AppendText("三角形的面積是:" + Convert.ToString(tri.Area()) + "\n");
txtPrint.AppendText("三角形的外接圓半徑是:" + Convert.ToString(tri.RadiusOfCircumcircle()) + "\n");
}
else
{
txtPrint.Text = null;
txtPrint.AppendText("產生的三點不構成三角形!\n");
}
}
private void lblTriXInterval_Click(object sender, EventArgs e)
{
}
}
}
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz4
{
static class Program
{
///
/// 應用程式的主要進入點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class Point
{
public double xCoordinate;
public double yCoordinate;
public double DistanceTo(Point pin) //計算該點和某點之間的距離
{
double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
return Distance;
}
}
}
PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class PinBoard
{
public int rows = 10;
public int columns = 10;
public Double rowInterval = 10;
public Double columnInterval = 10;
public Point[,] pinArray = null;
public void CreatePins() //產生矩陣各點之位置
{
pinArray = new Point[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
pinArray[i, j] = new Point();
}
}
}
public void SetPinsPosition() //依行列間距計算出各點x.y之座標
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
pinArray[i, j].xCoordinate = (i * rowInterval);
pinArray[i, j].yCoordinate = (j * columnInterval);
}
}
}
}
}
Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class Triangle
{
public Point[] pointArray = new Point[3]; //建立三角形的三個點
public double Perimeter() //計算三角形的周長
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double Perimeter = length1 + length2 + length3; //三邊和為周長
return Perimeter;
}
public double Area()
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double Perimeter = length1 + length2 + length3; //已求得三邊長度以海龍公式計算三角形面積
double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - length1) * (0.5 * Perimeter - length2) * (0.5 * Perimeter - length3));
return Area;
}
public bool IsRight()
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double temp = 0;
double copyLength1 = length1; //複製三邊邊長以便依大小排序
double copyLength2 = length2;
double copyLength3 = length3;
while (true)
{
if (copyLength2 > copyLength1) //將copyLength1變為最長邊
{
temp = copyLength2;
copyLength2 = copyLength1;
copyLength1 = temp;
}
else if (copyLength3 > copyLength2)
{
temp = copyLength3;
copyLength3 = copyLength2;
copyLength2 = temp;
}
else
{
break;
}
}
if (copyLength1 < (copyLength2 + copyLength3)) //三角形兩邊和須大於第三邊
{
return true;
}
else
{
return false;
}
}
public double RadiusOfCircumcircle()
{
double lengthA = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double lengthB = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double lengthC = pointArray[2].DistanceTo(pointArray[0]);
double cosAlpha = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC); //由餘弦公式計算出Alpha角之餘弦值
double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha); //由萬用公式計算出Alpha角之正弦值
double radius = 0.5 * lengthA / sinAlpha; //求得外接圓半徑
return radius;
}
}
}
[2015][Quiz][Week07] Quiz4 - 40173041H
程式分為
1.建立點型態
2.建立板子
3.建立三角形
4.視窗
5.主程式
點型態
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class Point
{
public double xCoordinate;
public double yCoordinate;
public double DistanceTo(Point pin) //計算該點和某點之間的距離
{
double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
return Distance;
}
}
}
板子
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class PinBoard
{
public int rows;
public int columns;
public Double rowInterval;
public Double columnInterval;
public Point[,] pinArray = null;
public void CreatePins() //產生矩陣各點之位置
{
pinArray = new Point[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
pinArray[i, j] = new Point();
}
}
}
public void SetPinsPosition() //依行列間距計算出各點x.y之座標
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
pinArray[i, j].xCoordinate = (i * rowInterval);
pinArray[i, j].yCoordinate = (j * columnInterval);
}
}
}
}
}
三角形
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz4
{
class TriangleFunction
{
public Point[] pointArray = new Point[3]; //建立三角形的三個點
public double Perimeter() //計算三角形的周長
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double Perimeter = length1 + length2 + length3; //三邊和為周長
return Perimeter;
}
public double Area()
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double Perimeter = length1 + length2 + length3; //已求得三邊長度以海龍公式計算三角形面積
double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - length1) * (0.5 * Perimeter - length2) * (0.5 * Perimeter - length3));
return Area;
}
public bool IsRight()
{
double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double length3 = pointArray[2].DistanceTo(pointArray[0]);
double temp = 0;
double copyLength1 = length1; //複製三邊邊長以便依大小排序
double copyLength2 = length2;
double copyLength3 = length3;
while (true)
{
if (copyLength2 > copyLength1) //將copyLength1變為最長邊
{
temp = copyLength2;
copyLength2 = copyLength1;
copyLength1 = temp;
}
else if (copyLength3 > copyLength2)
{
temp = copyLength3;
copyLength3 = copyLength2;
copyLength2 = temp;
}
else
{
break;
}
}
if (copyLength1 < (copyLength2 + copyLength3)) //三角形兩邊和須大於第三邊
{
return true;
}
else
{
return false;
}
}
public double RadiusOfCircumcircle()
{
double lengthA = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
double lengthB = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
double lengthC = pointArray[2].DistanceTo(pointArray[0]);
double cosAlpha = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC); //由餘弦公式計算出Alpha角之餘弦值
double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha); //由萬用公式計算出Alpha角之正弦值
double radius = 0.5 * lengthA / sinAlpha; //求得外接圓半徑
return radius;
}
}
}
視窗
using System;
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 Quiz4
{
public partial class Form1 : Form
{
PinBoard board = new PinBoard();
public Form1()
{
InitializeComponent();
}
private void btnGenerateTriengle_Click(object sender, EventArgs e)
{
TriangleFunction tri = new TriangleFunction();
Random rand = new Random();
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = Double.Parse(txtXInterval.Text);
board.columnInterval = Double.Parse(txtYInterval.Text);
int[] rowRand = new int[3];
int[] columnRand = new int[3];
board.CreatePins();
board.SetPinsPosition();
TrianglePin:
for (int i = 0; i < 3; i++)
{
rowRand[i] = rand.Next(board.rows);
columnRand[i] = rand.Next(board.columns);
tri.pointArray[i] = board.pinArray[rowRand[i], columnRand[i]];
}
if (!tri.IsRight())
{
goto TrianglePin;
}
txtPoint1Row.Text = rowRand[0].ToString();
txtPoint1Column.Text = columnRand[0].ToString();
txtPoint2Row.Text = rowRand[1].ToString();
txtPoint2Column.Text = columnRand[1].ToString();
txtPoint3Row.Text = rowRand[2].ToString();
txtPoint3Column.Text = columnRand[2].ToString();
txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
txtTriangleSolution.Text += Environment.NewLine;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
TriangleFunction tri = new TriangleFunction();
for (int i = 0; i < 3;i++ )
{
tri.pointArray[i]=new Point();
}
board.rows = int.Parse(txtRow.Text);
board.columns = int.Parse(txtColumn.Text);
board.rowInterval = Double.Parse(txtXInterval.Text);
board.columnInterval = Double.Parse(txtYInterval.Text);
txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
tri.pointArray[0].xCoordinate = double.Parse(txtPoint1X.Text);
tri.pointArray[0].yCoordinate = double.Parse(txtPoint1Y.Text);
tri.pointArray[1].xCoordinate = double.Parse(txtPoint2X.Text);
tri.pointArray[1].yCoordinate = double.Parse(txtPoint2Y.Text);
tri.pointArray[2].xCoordinate = double.Parse(txtPoint3X.Text);
tri.pointArray[2].yCoordinate = double.Parse(txtPoint3Y.Text);
if (!tri.IsRight())
{
txtTriangleSolution.Text = "不是三角形";
}
else
{
txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
txtTriangleSolution.Text += Environment.NewLine;
txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
txtTriangleSolution.Text += Environment.NewLine;
}
}
}
}
主程式
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quiz4
{
static class Program
{
///
/// 應用程式的主要進入點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
訂閱:
意見 (Atom)