執行檔的連結
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 HW7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//因形狀不同而改變出現的參數
private void cbox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_shape.SelectedIndex)
{
case 0:
lbl_para1.Text = "半徑";
txt_para2.Visible = false;
lbl_para2.Visible = false;
break;
case 1:
lbl_para1.Text = "邊長";
txt_para2.Visible = false;
lbl_para2.Visible = false;
break;
case 2:
lbl_para1.Text = "半徑";
txt_para2.Visible = true;
lbl_para2.Visible = true;
break;
case 3:
lbl_para1.Text = "邊長";
txt_para2.Visible = true;
lbl_para2.Visible = true;
break;
default:
break;
}
}
static double[] densityarr={2.7,7.87,11.3};
//用Shape3D與IRollable兩個不同參考的泛型來存取
List shapeArr = new List();
List rollArr = new List();
//加入物件
private void btn_import_Click(object sender, EventArgs e)
{
if (CheckChoise())
{
double density = densityarr[cbox_material.SelectedIndex];
switch (cbox_shape.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_amount.Text = Convert.ToString(Shape3D.Amount);
txt_objdata.AppendText(shapeArr[shapeArr.Count - 1].material() + '\n');
}
}
//計算體積
private void btn_calculateV_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < shapeArr.Count; i++)
txt_showVolume.AppendText(shapeArr[i].datadisplay());
}
//計算滑動距離
private void btn_calculateScroll_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i = 2 && txt_para2.Text == "")
{
MessageBox.Show("仍有參數未輸入,請輸入完畢!!");
return false;
}
else
return true;
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
abstract class Shape3D
{
//Fields
protected double density;
protected static int amount = 0;
//Contructor
public Shape3D()
{
Density = 0;
}
public Shape3D(double density)
{
Density = density;
amount++;
}
//Atributes
public double Density
{
get { return density;}
set { density =value;}
}
//readonly Atributes
public static int Amount
{
get { return amount; }
}
//abstract method
public abstract double volume();
public abstract string material();
//method
public double weight()
{
return volume()*Density;
}
public string datadisplay()
{
string str=null;
str += material();
str += '\t';
str += String.Format("{0:F2}",weight());
str += '\n';
return str;
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Ball:Shape3D,IRollable
{
//Fields
private static double pi = 3.1415926;
private double radius;
private string shapename = "Ball";
//contructor
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
}
//Atributes
public double Radius
{
get{return radius;}
set{radius=value;}
}
//override method
public override double volume()
{
return 4.0 / 3 * pi * Math.Pow(Radius, 3);
}
public override string material()
{
string str = null;
str += shapename;
str += '\t';
str += Convert.ToString(Radius);
str += '\t';
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
public double Rolldistance()
{
return Radius * Radius;
}
public string ShowRolldistance()
{
return material() + '\t' + String.Format("{0:F2}", Rolldistance()) + '\n';
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Cube : Shape3D
{
//Fields
private double side;
private string shapename = "Cube";
//contructor
public Cube(double side, double density)
: base(density)
{
Side = side;
}
//Atributes
public double Side
{
get { return side; }
set { side = value; }
}
//override method
public override double volume()
{
return Math.Pow(Side, 3);
}
public override string material()
{
string str = null;
str += shapename;
str += '\t';
str += Convert.ToString(Side);
str += '\t';
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Cylinder : Shape3D,IRollable
{
//Fields
private static double pi = 3.1415926;
private double radius;
private double height;
private string shapename = "Cylinder";
//contructor
public Cylinder(double radius, double height, double density)
: base(density)
{
Radius = radius;
Height = height;
}
//Atributes
public double Radius
{
get{return radius;}
set{radius=value;}
}
public double Height
{
get { return height; }
set { height = value; }
}
//override method
public override double volume()
{
return Height * pi * Math.Pow(Radius, 2);
}
public override string material()
{
string str = null;
str += shapename;
str += '\t';
str += Convert.ToString(Radius);
str += '\t';
str += Convert.ToString(Height);
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
public double Rolldistance()
{
return Radius*5;
}
public string ShowRolldistance()
{
return material()+'\t' + String.Format("{0:F2}", Rolldistance())+'\n';
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
class Pyramid : Shape3D
{
//Fields
private double side;
private double height;
private string shapename = "Pyramid";
//contructor
public Pyramid(double side, double height, double density)
: base(density)
{
Side = side;
Height = height;
}
//Atributes
public double Side
{
get { return side; }
set { side = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
//override method
public override double volume()
{
return 1.0 / 3 * Math.Pow(Side, 2) * Height;
}
public override string material()
{
string str = null;
str += shapename;
str += '\t';
str += Convert.ToString(Side);
str += '\t';
str += Convert.ToString(Height);
str += '\t';
str += String.Format("{0:F2}", Density);
return str;
}
}
}
IRollable.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW7
{
interface IRollable
{
double Rolldistance();
string ShowRolldistance();
}
}

沒有留言:
張貼留言