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.Windows.Forms;
namespace HW7
{
public partial class Form1 : Form
{
private Shape3D[] shapeArr = new Shape3D[100];
private static double[] densityArr = { 2.7, 7.87, 11.3 };
public Form1()
{
InitializeComponent();
cbox_ShapeSelect.SelectedIndex = 0;
cbox_MeterialSelect.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.Visible = false;
txt_Para2.Visible = false;
break;
case 2:
lbl_Para1.Text = "半徑";
lbl_Para2.Text = "高";
lbl_Para2.Visible = true;
txt_Para2.Visible = true;
break;
case 3:
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_MeterialSelect.SelectedIndex];
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
shapeArr[amount] = new Ball(double.Parse(txt_Para1.Text), density);
break;
case 1:
shapeArr[amount] = new Cube(double.Parse(txt_Para1.Text), density);
break;
case 2:
shapeArr[amount] = new Cylinder(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density);
break;
case 3:
shapeArr[amount] = new Pyramid(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density);
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)
{
for (int i = 0; i < Shape3D.Amount; i++)
{
string str = (shapeArr[i].ShowVolumeWeight() + Environment.NewLine);
txt_Display.AppendText(str);
}
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class Shape3D
{
//Fields
protected double _density;
private static int _amount = 0;
//Constructors
public Shape3D()
{
_density = 0;
_amount++;
}
public Shape3D(double d)
{
_density = d;
_amount++;
}
//Attributes
public double Density
{
get { return _density; }
set
{
if (_density < 0)
{
_density = 0;
}
else
{
_density = value;
}
}
}
public static int Amount
{
get { return _amount; }
}
//Methods
public double Weight()
{
return Density * 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;
}
//Virtual Methods
public virtual double Volume()
{
return 0;
}
public virtual string ShapeProperty()
{
return "";
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class Ball : Shape3D
{
//Fields
private double _radius;
//Constructors
public Ball(double radius, double density)
: base(density)
{
Radius = radius;
}
//Attributes
public double Radius
{
get { return _radius; }
set
{
if (_radius < 0)
{
_radius = 0;
}
else
{
_radius = value;
}
}
}
//Methods
public override double Volume()
{
return 4.0 / 3 * GeoConstant.pi * _radius * _radius * _radius;
}
public override string ToString()
{
string str = string.Format("{0,8}", "球");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Ball");
str += '\t';
str += string.Format("{0,8:F2}", Radius);
str += "\t\t";
str += string.Format("{0,8}", "");
return str;
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class Cube : Shape3D
{
//Fields
private double _side;
//Constructors
public Cube(double side, double density)
: base(density)
{
Side = side;
}
//Attributes
public double Side
{
get { return _side; }
set
{
if (_side < 0)
{
_side = 0;
}
else
{
_side = value;
}
}
}
//Methods
public override double Volume()
{
return _side * _side * _side;
}
public override string ToString()
{
string str = string.Format("{0,8}", "正方體");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
public override string ShapeProperty()
{
string str = string.Format("{0,8}", "Cube");
str += '\t';
str += string.Format("{0,8:F2}", Side);
str += "\t\t";
str += string.Format("{0,8}", "");
return str;
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class Cylinder : Shape3D
{
//Fields
private double _radius;
private double _height;
//Constructors
public Cylinder(double radius, double height, double density)
: base(density)
{
Radius = radius;
Height = height;
}
//Attributes
public double Height
{
get { return _height; }
set
{
if (_height < 0)
{
_height = 0;
}
else
{
_height = value;
}
}
}
public double Radius
{
get { return _radius; }
set
{
if (_radius < 0)
{
_radius = 0;
}
else
{
_radius = value;
}
}
}
//Methods
public override double Volume()
{
return GeoConstant.pi * _radius * _radius * _height;
}
public override string ToString()
{
string str = string.Format("{0,8}", "圓柱體");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
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);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class Pyramid : Shape3D
{
//Fields
private double _side;
private double _height;
//Constructors
public Pyramid(double side, double height, double density)
: base(density)
{
Side = side;
Height = height;
}
//Attributes
public double Height
{
get { return _height; }
set
{
if (_height < 0)
{
_height = 0;
}
else
{
_height = value;
}
}
}
public double Side
{
get { return _side; }
set
{
if (_side < 0)
{
_side = 0;
}
else
{
_side = value;
}
}
}
//Methods
public override double Volume()
{
return 1.0 / 3 * _side * _side * _height;
}
public override string ToString()
{
string str = string.Format("{0,8}", "金字塔");
str += '\t';
str += string.Format("{0,10:F2}", Volume());
str += '\t';
str += string.Format("{0,8:F2}", Weight());
return str;
}
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);
str += '\t';
str += string.Format("{0,8}", "");
return str;
}
}
}
GeoConstant.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HW7
{
class GeoConstant
{
public static double pi = 3.1415926;
}
}
Form1.Designer.cs(按我顯示)
namespace HW7
{
partial class Form1
{
///
/// 設計工具所需的變數。
///
private System.ComponentModel.IContainer components = null;
///
/// 清除任何使用中的資源。
///
/// 如果應該處置 Managed 資源則為 true,否則為 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 設計工具產生的程式碼
///
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
/// 修改這個方法的內容。
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.cbox_MeterialSelect = new System.Windows.Forms.ComboBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txt_Para2 = new System.Windows.Forms.TextBox();
this.txt_Para1 = new System.Windows.Forms.TextBox();
this.lbl_Para2 = new System.Windows.Forms.Label();
this.lbl_Para1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.cbox_ShapeSelect = new System.Windows.Forms.ComboBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.txt_AmountOfShape = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.btn_Add = new System.Windows.Forms.Button();
this.btn_Calculate = new System.Windows.Forms.Button();
this.txt_Message = new System.Windows.Forms.TextBox();
this.txt_Display = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.cbox_MeterialSelect);
this.groupBox1.Location = new System.Drawing.Point(30, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(336, 50);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "材質設定";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(36, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 2;
this.label1.Text = "材質類別";
//
// cbox_MeterialSelect
//
this.cbox_MeterialSelect.FormattingEnabled = true;
this.cbox_MeterialSelect.Items.AddRange(new object[] {
"鋁",
"鐵",
"鉛"});
this.cbox_MeterialSelect.Location = new System.Drawing.Point(154, 21);
this.cbox_MeterialSelect.Name = "cbox_MeterialSelect";
this.cbox_MeterialSelect.Size = new System.Drawing.Size(121, 20);
this.cbox_MeterialSelect.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txt_Para2);
this.groupBox2.Controls.Add(this.txt_Para1);
this.groupBox2.Controls.Add(this.lbl_Para2);
this.groupBox2.Controls.Add(this.lbl_Para1);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.cbox_ShapeSelect);
this.groupBox2.Location = new System.Drawing.Point(30, 68);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(336, 100);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "形狀設定";
//
// txt_Para2
//
this.txt_Para2.Location = new System.Drawing.Point(154, 69);
this.txt_Para2.Name = "txt_Para2";
this.txt_Para2.Size = new System.Drawing.Size(100, 22);
this.txt_Para2.TabIndex = 7;
//
// txt_Para1
//
this.txt_Para1.Location = new System.Drawing.Point(38, 69);
this.txt_Para1.Name = "txt_Para1";
this.txt_Para1.Size = new System.Drawing.Size(100, 22);
this.txt_Para1.TabIndex = 6;
//
// lbl_Para2
//
this.lbl_Para2.AutoSize = true;
this.lbl_Para2.Location = new System.Drawing.Point(152, 54);
this.lbl_Para2.Name = "lbl_Para2";
this.lbl_Para2.Size = new System.Drawing.Size(11, 12);
this.lbl_Para2.TabIndex = 5;
this.lbl_Para2.Text = "2";
//
// lbl_Para1
//
this.lbl_Para1.AutoSize = true;
this.lbl_Para1.Location = new System.Drawing.Point(36, 54);
this.lbl_Para1.Name = "lbl_Para1";
this.lbl_Para1.Size = new System.Drawing.Size(11, 12);
this.lbl_Para1.TabIndex = 4;
this.lbl_Para1.Text = "1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(36, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 3;
this.label2.Text = "形狀類別";
//
// cbox_ShapeSelect
//
this.cbox_ShapeSelect.AutoCompleteCustomSource.AddRange(new string[] {
"球",
"正方體",
"圓柱體",
"金字塔"});
this.cbox_ShapeSelect.FormattingEnabled = true;
this.cbox_ShapeSelect.Items.AddRange(new object[] {
"球",
"正方體",
"圓柱體",
"金字塔"});
this.cbox_ShapeSelect.Location = new System.Drawing.Point(154, 21);
this.cbox_ShapeSelect.Name = "cbox_ShapeSelect";
this.cbox_ShapeSelect.Size = new System.Drawing.Size(121, 20);
this.cbox_ShapeSelect.TabIndex = 0;
this.cbox_ShapeSelect.SelectedIndexChanged += new System.EventHandler(this.cbox_ShapeSelect_SelectedIndexChanged);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.txt_AmountOfShape);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Controls.Add(this.btn_Add);
this.groupBox3.Location = new System.Drawing.Point(30, 184);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(336, 49);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "增加Shape";
//
// txt_AmountOfShape
//
this.txt_AmountOfShape.Location = new System.Drawing.Point(187, 21);
this.txt_AmountOfShape.Name = "txt_AmountOfShape";
this.txt_AmountOfShape.Size = new System.Drawing.Size(100, 22);
this.txt_AmountOfShape.TabIndex = 5;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(152, 27);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 4;
this.label3.Text = "數量";
//
// btn_Add
//
this.btn_Add.Location = new System.Drawing.Point(38, 19);
this.btn_Add.Name = "btn_Add";
this.btn_Add.Size = new System.Drawing.Size(75, 23);
this.btn_Add.TabIndex = 0;
this.btn_Add.Text = "加入";
this.btn_Add.UseVisualStyleBackColor = true;
this.btn_Add.Click += new System.EventHandler(this.btn_Add_Click);
//
// btn_Calculate
//
this.btn_Calculate.Location = new System.Drawing.Point(394, 25);
this.btn_Calculate.Name = "btn_Calculate";
this.btn_Calculate.Size = new System.Drawing.Size(75, 23);
this.btn_Calculate.TabIndex = 6;
this.btn_Calculate.Text = "計算";
this.btn_Calculate.UseVisualStyleBackColor = true;
this.btn_Calculate.Click += new System.EventHandler(this.btn_Calculate_Click);
//
// txt_Message
//
this.txt_Message.Location = new System.Drawing.Point(30, 239);
this.txt_Message.Multiline = true;
this.txt_Message.Name = "txt_Message";
this.txt_Message.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txt_Message.Size = new System.Drawing.Size(336, 109);
this.txt_Message.TabIndex = 6;
//
// txt_Display
//
this.txt_Display.Location = new System.Drawing.Point(394, 68);
this.txt_Display.Multiline = true;
this.txt_Display.Name = "txt_Display";
this.txt_Display.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txt_Display.Size = new System.Drawing.Size(475, 289);
this.txt_Display.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(902, 369);
this.Controls.Add(this.txt_Display);
this.Controls.Add(this.txt_Message);
this.Controls.Add(this.btn_Calculate);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ComboBox cbox_MeterialSelect;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cbox_ShapeSelect;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox txt_AmountOfShape;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btn_Add;
private System.Windows.Forms.Button btn_Calculate;
private System.Windows.Forms.TextBox txt_Message;
private System.Windows.Forms.TextBox txt_Display;
private System.Windows.Forms.Label lbl_Para2;
private System.Windows.Forms.Label lbl_Para1;
private System.Windows.Forms.TextBox txt_Para2;
private System.Windows.Forms.TextBox txt_Para1;
}
}
沒有留言:
張貼留言