Form
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;
using shape;
namespace prac1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cBShape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cBShape.SelectedIndex)
{
case 0://ball
label_radius.Text = "半徑";
label_height.Visible = false;
textBox_height.Visible = false;
break;
case 1://cube
label_radius.Text = "邊長";
label_height.Visible = false;
textBox_height.Visible = false;
break;
case 2://cylinder
label_radius.Text = "半徑";
label_height.Text = "高";
label_height.Visible = true;
textBox_height.Visible = true;
break;
default://pyramid
label_radius.Text = "邊長";
label_height.Visible = true;
textBox_height.Visible = true;
break;
}
}
ObjectType[] objectType =new ObjectType[10];
double[] densityArr = { 2.70, 7.87, 11.35 };
private void btnAdd_Click(object sender, EventArgs e)
{
int amount=ObjectType.Amount;
if (amount >= 10)
return;
if (cBoxMaterial.Text == "")
{
MessageBox.Show( "Please choose the material.");
return;
}
switch (cBShape.SelectedIndex)
{
case 0:
objectType[amount] = new ball(Convert.ToDouble(txtBoxRadius.Text));
objectType[amount].density = densityArr[cBoxMaterial.SelectedIndex];
objectType[amount].StringMaterial = cBoxMaterial.Text;
break;
case 1:
objectType[amount] = new Cube(Convert.ToDouble(txtBoxRadius.Text));
objectType[amount].density = densityArr[cBoxMaterial.SelectedIndex];
objectType[amount].StringMaterial = cBoxMaterial.Text;
break;
case 2:
objectType[amount] = new Cylinder(Convert.ToDouble(txtBoxRadius.Text), Convert.ToDouble(textBox_height.Text));
objectType[amount].density = densityArr[cBoxMaterial.SelectedIndex];
objectType[amount].StringMaterial = cBoxMaterial.Text;
break;
case 3:
objectType[amount] = new Pyramid(Convert.ToDouble(txtBoxRadius.Text), Convert.ToDouble(textBox_height.Text));
objectType[amount].density = densityArr[cBoxMaterial.SelectedIndex];
objectType[amount].StringMaterial = cBoxMaterial.Text;
break;
default:
MessageBox.Show("Please choose the shape.");
return;
}
textBox_shape.AppendText(objectType[amount].StringShape()+"\r\n");
textBox_amount.Text = ObjectType.Amount.ToString();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
for (int i = 0; i < ObjectType.Amount; i++)
{
textBox_Properties.AppendText((i+1).ToString()+"\t"+objectType[i].StringProperties());
}
}
}
}
Class Object
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 shape
{
class ObjectType
{
public double density;
public string StringMaterial;
private static int amount=0;
public static int Amount
{
get { return amount; }
set
{
amount = value;
}
}
public ObjectType()
{
amount++;
}
public virtual double Volume()
{
return 0;
}
public double Mass()
{
return Volume() * density;
}
public virtual string StringShape()
{
return "";
}
public string StringProperties()
{
string str = "";
str += string.Format("{0,8}",StringMaterial);
str += "\t";
str += StringShape();
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}",Mass())+"\r\n";
return str;
}
}
}
class ball
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 shape
{
class ball:ObjectType
{
private double radius;
public ball(double radius)
{
this.radius = radius;
}
public double Radius
{
get { return radius; }
set
{
if (radius < 0)
radius = 0;
else
radius = value;
}
}
private static double pi = 3.1415926;
public override double Volume()
{
return 4 / 3 * pi * Math.Pow(radius, 3);
}
//public double Mass()
//{
// return Volume() * density;
//}
public override string StringShape()
{
string str = "";
str += string.Format("{0,10}","ball");
str += "\t";
str += string.Format("{0,8:F2}",radius);
str += "\t";
return str;
}
//public string StringProperties()
//{
// string str = "";
// str += StringShape();
// str += "\t";
// str += Volume().ToString();
// str += "\t";
// str += Mass().ToString();
// return str;
//}
}
}
class cube
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 shape
{
class Cube:ObjectType
{
private double side;
public Cube(double s)
{
this.side = s;
}
public double Side
{
get { return side; }
set
{
if (side < 0)
side = 0;
else
side = value;
}
}
public override double Volume()
{
return side*side*side;
}
//public double Mass()
//{
// return Volume() * density;
//}
public override string StringShape()
{
string str = "";
str += string.Format("{0,8}", "cube");
str += "\t";
str += string.Format("{0,8:F2}", side);
str += "\t";
return str;
}
//public string StringProperties()
//{
// string str = "";
// str += StringShape();
// str += "\t";
// str += Volume().ToString();
// str += "\t";
// str += Mass().ToString();
// return str;
//}
}
}
class cylinder
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 shape
{
class Cylinder:ObjectType
{
private double radius;
public double height;
public Cylinder(double r,double h)
{
this.radius = r;
height = h;
}
private static double pi = 3.1415926;
public double Radius
{
get { return radius; }
set
{
if (radius < 0)
radius = 0;
else
radius = value;
}
}
public override double Volume()
{
return radius*radius*pi*height;
}
//public double Mass()
//{
// return Volume() * density;
//}
public override string StringShape()
{
string str = "";
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 string StringProperties()
//{
// string str = "";
// str += StringShape();
// str += "\t";
// str += Volume().ToString();
// str += "\t";
// str += Mass().ToString();
// return str;
//}
}
}
class pyramid
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 shape
{
class Pyramid:ObjectType
{
private double side;
public double height;
public Pyramid(double s,double h)
{
this.side = s;
height = h;
}
public double Side
{
get { return side; }
set
{
if (side < 0)
side = 0;
else
side = value;
}
}
public override double Volume()
{
return side * side* height/3;
}
//public double Mass()
//{
// return Volume() * density;
//}
public override string StringShape()
{
string str = "";
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;
}
//public string StringProperties()
//{
// string str = "";
// str += StringShape();
// str += "\t";
// str += Volume().ToString();
// str += "\t";
// str += Mass().ToString();
// return str;
//}
}
}
沒有留言:
張貼留言