using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mid103_1
{
interface IRollable
{
double RollDistance();
string GetDistance();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mid103_1
{
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 (value < 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();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mid103_1
{
class Ball : Shape3D,IRollable
{
private double radius;
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
public override double Volume()
{
return 4.0/3.0*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;
}
public static Ball Creat(double r, double density)
{
if (r < 0)
return null;
Ball ball=new Ball(r,density);
return ball;
}
public double RollDistance()
{
return radius * radius;
}
public string GetDistance()
{
string str_Output = "";
str_Output += ShapeProperty() + "\t" + Density.ToString("#.##") + "\t";
//滾動距離=0時,ToString("#.##")會return "". 所以要另外判斷
//也可改用string.Format("format", data);
if (RollDistance() == 0)
str_Output += 0.ToString() + "\t";
else str_Output += RollDistance().ToString("#.##") + "\t";
return str_Output;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mid103_1
{
class Cylinder : Shape3D, IRollable
{
private double radius;
private double height;
public Cylinder(double radius, double height,double density)
: base(density)
{
Radius = radius;
Height = height;
}
public double Radius
{
get { return radius; }
set
{
if (value < 0)
radius = 0;
else
radius = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 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;
}
public double RollDistance()
{
return 5.0 * radius;
}
public string GetDistance()
{
string str_Output = "";
str_Output += ShapeProperty() + "\t" + Density.ToString("#.##") + "\t";
//滾動距離=0時,ToString("#.##")會return "". 所以要另外判斷
//也可改用string.Format("format", data);
if (RollDistance() == 0)
str_Output += 0.ToString() + "\t";
else str_Output += RollDistance().ToString("#.##") + "\t";
return str_Output;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mid103_1
{
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 (value < 0)
side = 0;
else
side = value;
}
}
public double Height
{
get { return height; }
set
{
if (value < 0)
height = 0;
else
height = value;
}
}
public override double Volume()
{
return 1.0/3.0*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;
}
}
}
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 Mid103_1
{
public partial class Form1 : Form
{
List shapeArr = new List();
List DistanceArr = new List();
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.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
case 2:
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(Convert.ToDouble(txt_Para1.Text), density);
shapeArr.Add(ball);
DistanceArr.Add(ball);
break;
case 1:
Cylinder cylinder = new Cylinder(Convert.ToDouble(txt_Para1.Text), Convert.ToDouble(txt_Para2.Text), density);
shapeArr.Add(cylinder);
DistanceArr.Add(cylinder);
break;
case 2:
Pyramid pyramid = new Pyramid(Convert.ToDouble(txt_Para1.Text), Convert.ToDouble(txt_Para2.Text), density);
shapeArr.Add(pyramid);
break;
default:
break;
}
txt_Message.AppendText(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 = (shapeArr[i].ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
private void btn_RollDistance_Click(object sender, EventArgs e)
{
txt_distance.Clear();
for (int i = 0; i < DistanceArr.Count ; i++)
{
string str = (DistanceArr[i].GetDistance() + "\n");
txt_distance.AppendText(str);
}
}
}
}
沒有留言:
張貼留言