執行檔.exe
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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
private int amount = 0;
private double density;
private string[] str = null;
Modle[] M;
public Form1()
{
InitializeComponent();
str = new string[10];
M = new Modle[10];
TxtBox_2.Visible = false;
TxtBox_3.Visible = false;
Lbl_1.Visible = false;
Lbl_2.Visible = false;
}
private void Btn_1_Click(object sender, EventArgs e)
{
while (Modle.Amount == 10)
{
MessageBox.Show("無法再新增囉!");
return;
}
if (CmbBox_1.SelectedIndex == -1 || CmbBox_2.SelectedIndex == -1)
{
MessageBox.Show("請輸入正確參數!");
return;
}
switch (CmbBox_1.SelectedIndex)
{
case 0:
density = 8.92;
str[Modle.Amount] +="(銅) ";
break;
case 1:
density = 7.87;
str[Modle.Amount] +="(鐵) ";
break;
case 2:
density = 11.34;
str[Modle.Amount] +="(鉛) ";
break;
}
//************將創造出來之不同外型物件,放至於所屬形狀之陣列*****************
switch (CmbBox_2.SelectedIndex)
{
case 0:
if (TxtBox_2.Text == "0")
{
MessageBox.Show("參數輸入錯誤!");
return;
}
M[Modle.Amount] = Sphere.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
str[Modle.Amount - 1] += " 球 \t" + TxtBox_2.Text + "cm\t" + "\t";
break;
case 1:
if (TxtBox_2.Text == "0")
{
MessageBox.Show("參數輸入錯誤!");
return;
}
M[Modle.Amount] = Cube.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
str[Modle.Amount - 1] += "正方體\t" + TxtBox_2.Text + "cm\t" + "\t";
break;
case 2:
if (TxtBox_2.Text == "0" || TxtBox_3.Text == "0")
{
MessageBox.Show("參數輸入錯誤!");
return;
}
M[Modle.Amount] = Pyramid.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
str[Modle.Amount - 1] += "金字塔" + "\t" + TxtBox_2.Text + "cm\t" + TxtBox_3.Text + "cm\t";
break;
case 3:
if (TxtBox_2.Text == "0" || TxtBox_3.Text == "0")
{
MessageBox.Show("參數輸入錯誤!");
return;
}
M[Modle.Amount] = Cylinder.Create(double.Parse(TxtBox_2.Text), double.Parse(TxtBox_3.Text), density);
str[Modle.Amount - 1] += "圓柱體" + "\t" + TxtBox_2.Text + "cm\t" + TxtBox_3.Text + "cm\t";
break;
}
TxtBox_1.AppendText(str[Modle.Amount-1] + "\n");
TxtBox_4.Text = Modle.Amount.ToString();
TxtBox_6.Text = Sphere.Amount.ToString();
TxtBox_7.Text = Cube.Amount.ToString();
TxtBox_8.Text = Pyramid.Amount.ToString();
TxtBox_9.Text = Cylinder.Amount.ToString();
}
private void Btn_2_Click(object sender, EventArgs e)
{
//***********各外型類別物件創造出來時,都已具有專屬屬性,再經由本身Mass()方法計算出質量;當掃描至某矩陣位置具有存放記憶體時,再輸出其資料***************
for (int i = amount; i < Modle.Amount; i++)
{
TxtBox_5.AppendText(str[i]+ M[i].Density + "g/cm^3\t" + M[i].Mass().ToString("0.00") + "g\n");
M[i] = null;
str[i] = "";
amount = Modle.Amount;
}
}
private void CmbBox_2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (CmbBox_2.SelectedIndex)
{
case 0:
Lbl_1.Text = "半徑:";
TxtBox_2.Visible = true;
TxtBox_3.Visible = false;
Lbl_1.Visible = true;
Lbl_2.Visible = false;
break;
case 1:
Lbl_1.Text = "邊長:";
TxtBox_2.Visible = true;
TxtBox_3.Visible = false;
Lbl_1.Visible = true;
Lbl_2.Visible = false;
break;
case 2:
Lbl_1.Text = "邊長:";
Lbl_2.Text = "高:";
TxtBox_2.Visible = true;
TxtBox_3.Visible = true;
Lbl_1.Visible = true;
Lbl_2.Visible = true;
break;
case 3:
Lbl_1.Text = "邊長:";
Lbl_2.Text = "高:";
TxtBox_2.Visible = true;
TxtBox_3.Visible = true;
Lbl_1.Visible = true;
Lbl_2.Visible = true;
break;
}
}
private void Btn_3_Click(object sender, EventArgs e)
{
TxtBox_1.Clear();
TxtBox_5.Clear();
Modle.Amount = 0;
Sphere.Amount = 0;
Cube.Amount = 0;
Pyramid.Amount = 0;
Cylinder.Amount = 0;
TxtBox_4.Text = Modle.Amount.ToString();
TxtBox_6.Text = Sphere.Amount.ToString();
TxtBox_7.Text = Cube.Amount.ToString();
TxtBox_8.Text = Pyramid.Amount.ToString();
TxtBox_9.Text = Cylinder.Amount.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication6
{
public class Modle
{
//**********************Modle建構子*******************
public double pi = 3.14159; //圓周率
protected double length_1;
protected double length_2;
protected double density;
private static int amount = 0;
public Modle(double L1, double L2, double density)
{
this.length_1 = L1;
this.length_2 = L2;
this.density = density;
amount++;
}
//**********************Modle_Amount屬性*******************
public static int Amount
{
get
{
return amount;
}
set
{
amount = value;
}
}
//**********************Modle_Length_1屬性*******************
public double Length_1
{
get
{ return length_1; }
set
{
if (value < 0)
length_1 = 0;
else
{
length_1 = value;
}
}
}
//**********************Modle_Length_2屬性*******************
public double Length_2
{
get
{ return length_2; }
set
{
if (value > 0)
length_2 = value;
else
{
length_2 = 0;
}
}
}
public virtual double Volume()
{
return 0.0;
}
public virtual double Mass()
{
return Volume() * Density;
}
//**********************Modle_Density屬性*******************
public double Density
{
get
{ return density; }
set
{
if (value > 0)
density = value;
else
{
density = 0;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication6
{
//**********************繼承Modle建構子*******************
public class Sphere : Modle
{
private static int amount = 0;
public Sphere(double radius, double high, double density)
: base(radius, high, density)
{
amount++;
}
//**********************建造Sphere實體********************
public static Sphere Create(double radius, double high, double density)
{
Sphere S = null;
if (radius <= 0 || density <= 0)
{
return S;
}
else
{
S = new Sphere(radius, high, density);
return S;
}
}
//********************
public override double Volume()
{
return Length_1 * Length_1 * Length_1 * pi * 4 / 3;
}
//********************
public new static int Amount // 數量
{
get
{
return amount;
}
set
{
amount = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication6
{
public class Cube : Modle
{
private static int amount = 0;
//**********************繼承Modle建構子*******************
public Cube(double radius, double high, double density)
: base(radius, high, density)
{
amount++;
}
//**********************建造Cube實體********************
public static Cube Create(double radius, double high, double density)
{
Cube C = null;
if (radius <= 0 || density <= 0)
{
return C;
}
else
{
C = new Cube(radius, high, density);
return C;
}
}
//*********************************
public override double Volume()
{
return Length_1 * Length_1 * Length_1;
}
//********************
public new static int Amount // 數量
{
get
{
return amount;
}
set
{
amount = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication6
{
public class Pyramid : Modle
{
private static int amount = 0;
//**********************繼承Modle建構子*******************
public Pyramid(double radius, double high, double density)
: base(radius, high, density)
{
amount++;
}
//**********************建造Pyramid實體********************
public static Pyramid Create(double radius, double high, double density)
{
Pyramid P = null;
if (radius <= 0 || density <= 0)
{
return P;
}
else
{
P = new Pyramid(radius, high, density);
return P;
}
}
//*********************************
public override double Volume()
{
return Length_1 * Length_1 * Length_2 / 3;
}
//********************
public new static int Amount // 數量
{
get
{
return amount;
}
set
{
amount = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication6
{
public class Cylinder : Modle
{
private static int amount = 0;
//**********************繼承Modle建構子*******************
public Cylinder(double radius, double high, double density)
: base(radius, high, density)
{
amount++;
}
//**********************建造Cylinder實體********************
public static Cylinder Create(double radius, double high, double density)
{
Cylinder CL = null;
if (radius <= 0 || density <= 0)
{
return CL;
}
else
{
CL = new Cylinder(radius, high, density);
return CL;
}
}
//*********************************
public override double Volume()
{
return Length_1 * Length_1 * Length_2;
}
//********************
public new static int Amount // 數量
{
get
{
return amount;
}
set
{
amount = value;
}
}
}
}
沒有留言:
張貼留言