HW 07 - exe
HW 07
Form1.cs
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 HW_07
{
public partial class Form1 : Form
{
List ShapeList = new List();
List RollList = new List();
bool IsClean = false;
bool calculated_mav = false;
bool calculated_roll = false;
public Form1()
{
InitializeComponent();
}
public void ShapeShow(string text)
{
string picPath = text + ".png";
picBox_3DShape.Image = Image.FromFile(picPath);
}
public void InputShow(string text)
{
pnl_length.Hide(); pnl_height.Hide(); pnl_width.Hide();
switch (text)
{
case "Cube":
pnl_length.Show();
lbl_length.Text = "長度";
break;
case "Cuboid":
pnl_length.Show(); pnl_height.Show(); pnl_width.Show();
lbl_length.Text = "長度";
break;
case "Sphere":
pnl_length.Show();
lbl_length.Text = "半徑";
break;
case "Cylinder":
pnl_length.Show(); pnl_height.Show();
lbl_length.Text = "半徑";
break;
case"Pyramid":
pnl_length.Show(); pnl_height.Show();
lbl_length.Text = "長度";
break;
case "Corn":
pnl_length.Show(); pnl_height.Show();
lbl_length.Text = "半徑";
break;
default:
break;
}
}
private void cBox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
ShapeShow(cmb_shape.Text);
InputShow(cmb_shape.Text);
}
private void cBox_material_SelectedIndexChanged(object sender, EventArgs e)
{
lbl_materialShow.Text = cmb_material.Text.Substring(0,2);
}
private void btn_selected_Click(object sender, EventArgs e)
{
if (IsClean == false)
{
rtx_result.Text = ("種類" + "\t" + "參數 1" + "\t" + "參數 2" + "\t" + "參數 3"
+ "\n" + "========================================================\n");
Shape3D.Amount = 0;
ShapeList.Clear();
RollList.Clear();
IsClean = true;
calculated_mav = false;
calculated_roll = false;
}
int amount = Shape3D.Amount;
switch (cmb_shape.SelectedIndex)
{
case 0: //Cube
if (txt_length.Text == "")
{
ShowResult("Error");
return;
}
ShapeList.Add(Cube.Create(double.Parse(txt_length.Text)));
break;
case 1: //Cuboid
if (txt_length.Text == "" || txt_width.Text == "" || txt_height.Text == "")
{
ShowResult("Error");
return;
}
ShapeList.Add(Cuboid.Create(double.Parse(txt_length.Text),
double.Parse(txt_width.Text), double.Parse(txt_height.Text)));
break;
case 2: //Sphere
if (txt_length.Text == "")
{
ShowResult("Error");
return;
}
Sphere sphere = Sphere.Create(double.Parse(txt_length.Text));
ShapeList.Add(sphere);
RollList.Add(sphere);
break;
case 3: //Cylinder
if (txt_length.Text == "" || txt_height.Text == "")
{
ShowResult("Error");
return;
}
Cylinder cylinder = Cylinder.Create(double.Parse(txt_length.Text),
double.Parse(txt_height.Text));
ShapeList.Add(cylinder);
RollList.Add(cylinder);
break;
case 4: //Pyramid
if (txt_length.Text == "" || txt_height.Text == "")
{
ShowResult("Error");
return;
}
ShapeList.Add(Pyramid.Create(double.Parse(txt_length.Text),
double.Parse(txt_height.Text)));
break;
case 5: //Corn
if (txt_length.Text == "" || txt_height.Text == "")
{
ShowResult("Error");
return;
}
ShapeList.Add(Corn.Create(double.Parse(txt_length.Text),
double.Parse(txt_height.Text)));
break;
default:
MessageBox.Show("請選擇形狀!");
return;
}
lbl_ashow.Text = Shape3D.Amount.ToString();
ShapeList[amount].Density(cmb_material.SelectedIndex);
txt_vShow.Text = ShapeList[amount].Volume().ToString("F");
txt_wSHow.Text = ShapeList[amount].Mass().ToString("F");
rtx_result.AppendText(ShapeList[amount].Name() + "\n");
}
private void btn_calaulate_Click(object sender, EventArgs e)
{
ShowResult("Vol/Mass");
IsClean = false;
}
private void btn_roll_Click(object sender, EventArgs e)
{
ShowResult("RollDis");
IsClean = false;
}
private void ShowResult(string DoWhat)
{
string str = "";
string paraline = "種類" + "\t" + "參數 1" + "\t" + "參數 2" + "\t" + "參數 3" + "\t" + "密度" + "\t";
string newline = "\n========================================================\n";
switch (DoWhat)
{
case "Error": // 建立失敗
MessageBox.Show("建立失敗,請重新確認參數!");
break;
case "Vol/Mass": // 顯示質量與體積
if (calculated_mav != true)
{
rtx_result.Text = ""; //textBox Clear
rtx_result.AppendText(paraline + "體積" + "\t" + "質量" + newline);
foreach (var element in ShapeList)
{
str += element.Name() + "\t"
+ element.Density() + "\t"
+ element.Volume().ToString("F") + "\t"
+ element.Mass().ToString("F") + "\n";
}
str += newline;
rtx_result.AppendText(str);
calculated_mav = true;
}
break;
case "RollDis": // 顯示滾動距離
if (calculated_roll != true)
{
rtx_result.AppendText("\n" + paraline + "滾動距離" + newline);
foreach (var element in RollList)
{
str += ((Shape3D)element).Name() + "\t"
+ ((Shape3D)element).Density() + "\t"
+ element.RollingDistance().ToString("F") + "\n";
}
str += newline;
rtx_result.AppendText(str);
calculated_roll = true;
}
break;
default:
break;
}
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
abstract class Shape3D
{
//Field
private double density = 1;
protected double length;
protected double height;
protected double width;
protected static double Pi = 3.14159;
public static double[] Material_density = { 19.3, 10.49, 8.96, 7.9, 2.7, 4.5, -1 };
//
protected static int amount = 0;
//Constructors
public Shape3D()
{
this.length = 0;
this.width = 0;
this.height = 0;
}
public Shape3D(double length)
{
this.length = length;
amount++;
}
public Shape3D(double length, double height)
{
this.length = length;
this.height = height;
amount++;
}
public Shape3D(double length, double width, double height)
{
this.length = length;
this.width = width;
this.height = height;
amount++;
}
//ReadOnly Attribute
public static int Amount
{
get { return amount; }
set { amount = value; }
}
//Virtual Methods
public abstract double Volume();
public abstract string Name();
//Methods
public void Density(int select)
{
if (select < 0) //都沒選擇材料的情況
this.density = 1;
else
density = Material_density[select];
}
public double Density()
{
return density;
}
public double Mass()
{
return Volume() * density;
}
}
}
IRollable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
interface IRollable
{
double RollingDistance();
}
}
Sphere.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Sphere : Shape3D, IRollable
{
//Field
new private static int amount = 0;
private double radius = 0;
//Constructors
public Sphere(double length)
: base(length)
{
radius = length;
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
// 4/3*pi*(R^3)
return 4.0 / 3 * Pi * radius * radius * radius;
}
public override string Name()
{
return "Sphere" + "\t" + length.ToString("F") + "\t" + "\t";
}
public double RollingDistance() // 計算滾動距離
{
return radius * radius;
}
public static Sphere Create(double radius)
{
Sphere sphere = null;
if (radius < 0)
return sphere;
else
{
sphere = new Sphere(radius);
return sphere;
}
}
}
}
Corn.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Corn : Shape3D
{
//Fields
new private static int amount = 0;
private double radius = 0;
//Constructors
public Corn(double length, double height)
: base(length, height)
{
radius = length;
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
return 1.0 / 3 * radius * radius * Pi * height;
}
public override string Name()
{
return "Corn" + "\t" + radius.ToString("F") + "\t" + height.ToString("F") + "\t";
}
public static Corn Create(double radius, double height)
{
Corn corn = null;
if (radius < 0 || height < 0)
return corn;
else
{
corn = new Corn(radius, height);
return corn;
}
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Cube : Shape3D
{
//Fields
new private static int amount = 0;
//Constructors
public Cube(double length)
: base(length)
{
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
return length * length * length;
}
public override string Name()
{
return "Cube" + "\t" + length.ToString("F") + "\t" + "\t";
}
public static Cube Create(double length)
{
Cube cube = null;
if (length < 0)
return cube;
else
{
cube = new Cube(length);
return cube;
}
}
}
}
Cuboid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Cuboid : Shape3D
{
//Fields
new private static int amount = 0;
//Constructors
public Cuboid(double length, double width, double height)
: base(length, width, height)
{
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
return length * width * height;
}
public override string Name()
{
return "Cuboid" + "\t" + length.ToString("F") + "\t" + height.ToString("F") + "\t" + width.ToString("F");
}
public static Cuboid Create(double length, double width, double height)
{
Cuboid cuboid = null;
if (length < 0 || width < 0 || height < 0)
return cuboid;
else
{
cuboid = new Cuboid(length, width, height);
return cuboid;
}
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Cylinder : Shape3D, IRollable
{
//Fields
new private static int amount = 0;
private double radius = 0;
//Constructors
public Cylinder(double length, double height)
: base(length, height)
{
radius = length;
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
return Pi * radius * radius * height;//pi*(r^2)*h
}
public override string Name()
{
return "Cylinder" + "\t" + radius.ToString("F") + "\t" + height.ToString("F") + "\t";
}
public double RollingDistance() // 計算滾動距離
{
return 5.0 * length;
}
public static Cylinder Create(double radius, double height)
{
Cylinder cylinder = null;
if (radius < 0 || height < 0)
return cylinder;
else
{
cylinder = new Cylinder(radius, height);
return cylinder;
}
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_07
{
class Pyramid : Shape3D
{
//Fields
new private static int amount = 0;
//Constructors
public Pyramid(double length, double height)
: base(length, height)
{
amount++;
}
//繼承隱藏 hide inherited attribute
new public static int Amount
{
get { return amount; }
}
//override Methods
public override double Volume()
{
return 1.0 / 3 * length * length * height;
}
public override string Name()
{
return "Pyramid" + "\t" + length.ToString("F") + "\t" + height.ToString("F") + "\t";
}
public static Pyramid Create(double length, double height)
{
Pyramid pyramid = null;
if (length < 0 || height < 0)
return pyramid;
else
{
pyramid = new Pyramid(length, height);
return pyramid;
}
}
}
}
沒有留言:
張貼留言