Form1
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 Quiz
{
public partial class Form1 : Form
{
Shape3D[] ShapeArr=new Shape3D[100];
public static double[] DensityArr = { 2.7, 7.87, 11.3 };
public Form1()
{
InitializeComponent();
}
private void cboShapeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cboShapeSelect.SelectedIndex)
{
case 0:
lblInput1.Text = "半徑";
lblInput2.Visible = false;
txtInput2.Visible = false;
break;
case 1:
lblInput1.Text = "邊長";
lblInput2.Visible = false;
txtInput2.Visible = false;
break;
case 2:
lblInput1.Text = "半徑";
lblInput2.Text = "高";
lblInput2.Visible = true;
txtInput2.Visible = true;
break;
case 3:
lblInput1.Text = "邊長";
lblInput2.Text = "高";
lblInput2.Visible = true;
txtInput2.Visible = true;
break;
}
}
private void btnAddShape_Click(object sender, EventArgs e)
{
int amount = Shape3D.Amount;
double density = DensityArr[cboMaterial.SelectedIndex];
switch (cboShapeSelect.SelectedIndex)
{
case 0:
ShapeArr[amount] = new Ball(double.Parse(txtInput1.Text), density);
break;
case 1:
ShapeArr[amount] = new Cube(double.Parse(txtInput1.Text), density);
break;
case 2:
ShapeArr[amount] = new Cylinder(double.Parse(txtInput1.Text), double.Parse(txtInput2.Text), density);
break;
case 3:
ShapeArr[amount] = new Pyramid(double.Parse(txtInput1.Text), double.Parse(txtInput2.Text), density);
break;
default:
break;
}
txtAddShape.AppendText(ShapeArr[amount].PropertyOfShape() + Environment.NewLine);
txtAddNum.Text = Shape3D.Amount.ToString();
}
private void btnCount_Click(object sender, EventArgs e)
{
for (int i = 0; i < Shape3D.Amount; i++)
{
string str = (ShapeArr[i].Showlist() + Environment.NewLine);
txtShow.AppendText(str);
}
}
}
}
Shape3D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quiz
{
class Shape3D
{
protected static double pi = 3.1415;
private double _density;
private static int amount=0;
public double Density
{
get { return _density; }
set
{
if (_density <= 0)
_density = 0;
else
_density = value;
}
}
public virtual double Volume()
{
return 0;
}
public double Weight()
{
return Volume() * _density;
}
public string Showlist()
{
string str = PropertyOfShape();
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());
str += "\r\n";
return str;
}
public virtual string PropertyOfShape()
{
return "";
}
public Shape3D()
{
_density=0;
amount++;
}
public Shape3D(double d)
{
_density = d;
amount++;
}
public static int Amount
{
get { return amount; }
}
}
}
Ball
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quiz
{
class Ball:Shape3D
{
//Field
private double _radius;
//Constructor
public Ball(double radius,double density)
:base(density)
{
Radius = radius;
Density = density;
}
//Attribute
public double Radius
{
get { return _radius; }
set
{
if (_radius < 0)
{
_radius = 0;
}
else
{
_radius = value;
}
}
}
//Method
public override double Volume()
{
return 4.0 / 3 * _radius * _radius * _radius * pi;
}
public override string PropertyOfShape()
{
string str = string.Format("{0:8}", "Ball");
str += '\t';
str += string.Format("{0,8:F2}", _radius);
str += '\t';
str += string.Format("{0,8:F2}", "");
return str;
}
public override string ToString()
{
string str = string.Format("{0,8}", "Ball");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,10:F2}", Weight());
return str;
}
}
}
Cylinder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quiz
{
class Cylinder : Shape3D
{
//Field
private double _radius;
private double _height;
//Constructor
public Cylinder(double radius, double height, double density)
:base(density)
{
Radius = radius;
Height = height;
Density = density;
}
//Attribute
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;
}
}
}
//Method
public override double Volume()
{
return _radius * _radius * pi * _height;
}
public override string PropertyOfShape()
{
string str = string.Format("{0:10}", "Cylinder");
str += '\t';
str += string.Format("{0,8:F2}", _radius);
str += '\t';
str += string.Format("{0,8:F2}", _height);
return str;
}
public override string ToString()
{
string str = string.Format("{0,8}", "Cylinder");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,10:F2}", Weight());
return str;
}
}
}
Pyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quiz
{
class Pyramid : Shape3D
{
//Field
private double _side;
private double _height;
//Constructor
public Pyramid(double side, double height, double density)
: base(density)
{
Side = side;
Height = height;
Density = density;
}
//Attribute
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;
}
}
}
//Method
public override double Volume()
{
return 1.0 / 3 * _side * _side * _height;
}
public override string PropertyOfShape()
{
string str = string.Format("{0,10}", "Pyramid");
str += '\t';
str += string.Format("{0,8:F2}", _side);
str += '\t';
str += string.Format("{0,8:F2}", _height);
return str;
}
public override string ToString()
{
string str = string.Format("{0,8}", "pyramid");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,10:F2}", Weight());
return str;
}
}
}
Cube
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Quiz
{
class Cube : Shape3D
{
//Field
private double _side;
//Constructor
public Cube(double side,double density)
:base(density)
{
Side = side;
Density = density;
}
//Attribute
public double Side
{
get { return _side; }
set
{
if (_side < 0)
{
_side = 0;
}
else
{
_side = value;
}
}
}
//Method
public override double Volume()
{
return _side * _side * _side;
}
public override string PropertyOfShape()
{
string str = string.Format("{0,10}", "Cube");
str += '\t';
str += string.Format("{0,8:F2}", _side);
str += '\t';
str += string.Format("{0,8}", "");
str += '\t';
return str;
}
public override string ToString()
{
string str = string.Format("{0,8}", "Cube");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,10:F2}", Weight());
return str;
}
}
}
沒有留言:
張貼留言