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 Hw6_40273032H
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LengthFieldInvisible();
}
protected double[] materialDensity={ 2.7 , 7.9 , 11.34 };//材料密度
protected static int amoung = 0;//計算物件數用的
public int Amoung
{
get
{ return amoung; }
set
{
amoung = value;
}
}//amoung的屬性
private Sheap3D[] sheapArr=new Sheap3D[100];
public void cbox_sheap_SelectedIndexChanged(object sender, EventArgs e)//隨形狀改變的邊長條件
{
LengthFieldInvisible();
txt_length1.Text = "";
txt_length2.Text = "";
switch (cbox_sheap.SelectedIndex)
{
case 0://球
lbl_length1.Text = "半徑";
lbl_length1.Visible = true;
txt_length1.Visible = true;
break;
case 1://正方體
lbl_length1.Text = "邊長";
lbl_length1.Visible = true;
txt_length1.Visible = true;
break;
case 2://圓柱體
lbl_length1.Text = "半徑";
lbl_length1.Visible = true;
txt_length1.Visible = true;
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
break;
case 3://金字塔
lbl_length1.Text = "邊長";
lbl_length1.Visible = true;
txt_length1.Visible = true;
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
break;
}
}
public void btn_add_Click(object sender, EventArgs e)//建立物件
{
switch (cbox_sheap.SelectedIndex)
{
case 0://球
sheapArr[Amoung] = new Ball(txt_length1.Text,materialDensity[cbox_material.SelectedIndex]);
break;
case 1://正方體
sheapArr[Amoung] = new Cube(txt_length1.Text, materialDensity[cbox_material.SelectedIndex]);
break;
case 2://圓柱體
sheapArr[Amoung] = new Cylinder(txt_length1.Text, txt_length2.Text, materialDensity[cbox_material.SelectedIndex]);
break;
case 3://金字塔
sheapArr[Amoung] = new Pyramid(txt_length1.Text, txt_length2.Text, materialDensity[cbox_material.SelectedIndex]);
break;
}
txt_objNum.Text = Amoung.ToString();
txt_pile.AppendText(MName[cbox_material.SelectedIndex] + "\t" + OName[cbox_sheap.SelectedIndex] + "\t" + txt_length1.Text + "\t" + txt_length2.Text + "\t" + "\n");
}
public void LengthFieldInvisible()
{
lbl_length1.Visible = false;
lbl_length2.Visible = false;
txt_length1.Visible = false;
txt_length2.Visible = false;
}
private void btn_compute_Click(object sender, EventArgs e)
{
for (int i = 0; i < Amoung; i++)
{
txt_display.AppendText(sheapArr[i].ShowResault());
}
}
}
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw6_40273032H
{
abstract class Sheap3D : Form1
{
protected string length1, length2;
protected double material,pi=3.14;
public Sheap3D(string length1,double material)
{
this.length1 = length1;
this.material = material;
Amoung++;
}
public Sheap3D(string length1, string length2, double material)
{
this.length1 = length1;
this.length2 = length2;
this.material = material;
Amoung++;
}
public string MaterialName()
{
if (material == materialDensity[0])
return "鋁";
else if (material == materialDensity[1])
return "鐵";
else if (material == materialDensity[2])
return "鉛";
else return "";
}
public abstract double CalculateVolume();
public abstract double CalculateWeight();
public abstract string ShowResault();
// public abstract void LengthFieldState();
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw6_40273032H
{
class Pyramid : Sheap3D
{
public Pyramid(string length, string high, double material): base(length, high, material)
{ }
public override double CalculateVolume()
{
return 1.0/3*Convert.ToDouble(length1) * Convert.ToDouble(length1) * Convert.ToDouble(length2);
}
public override double CalculateWeight()
{
return CalculateVolume() * material;
}
public override string ShowResault()
{
string str="";
str += MaterialName()+" ";
str += " 金字塔";
str += length1 + " " + length2 + " ";
str += CalculateVolume() + " " + CalculateWeight() + '\n';
return str;
}
/* public override void LengthFieldState()
{
lbl_length1.Text = "邊長";
lbl_length1.Visible = true;
txt_length1.Visible = true;
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
} */
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw6_40273032H
{
class Cylinder : Sheap3D
{
public Cylinder(string radius, string high, double material): base(radius, high, material)
{ }
public override double CalculateVolume()
{
return Convert.ToDouble(length1) * Convert.ToDouble(length1) * pi * Convert.ToDouble(length2);
}
public override double CalculateWeight()
{
return CalculateVolume() * material;
}
public override string ShowResault()
{
string str = "";
str += MaterialName() + " ";
str += " 圓柱體";
str += length1 + " " + length2 + " ";
str += CalculateVolume() + " " + CalculateWeight() + '\n';
return str;
}
/* public override void LengthFieldState()
{
lbl_length1.Text = "半徑";
lbl_length1.Visible = true;
txt_length1.Visible = true;
lbl_length2.Text = "高";
lbl_length2.Visible = true;
txt_length2.Visible = true;
} */
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw6_40273032H
{
class Cube : Sheap3D
{
public Cube(string length, double material): base(length, material)
{ }
public override double CalculateVolume()
{
return Convert.ToDouble(length1) * Convert.ToDouble(length1) * Convert.ToDouble(length1);
}
public override double CalculateWeight()
{
return CalculateVolume() * material;
}
public override string ShowResault()
{
string str = "";
str += MaterialName() + " ";
str += " 正立方體";
str += length1 + " " + "0" + " ";
str += CalculateVolume() + " " + CalculateWeight()+'\n';
return str;
}
/* public override void LengthFieldState()
{
lbl_length1.Text = "邊長";
lbl_length1.Visible = true;
txt_length1.Visible = true;
} */
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hw6_40273032H
{
class Ball : Sheap3D
{
public Ball(string radius, double material): base(radius, material)
{ }
public override double CalculateVolume()
{
return 3.0/4*pi*Convert.ToDouble(length1) * Convert.ToDouble(length1) * Convert.ToDouble(length1);
}
public override double CalculateWeight()
{
return CalculateVolume() * material;
}
public override string ShowResault()
{
string str = "";
str += MaterialName() + " ";
str += " 球";
str += length1 + " " + "0" + " ";
str += CalculateVolume() + " " + CalculateWeight() + '\n';
return str;
}
/* public override void LengthFieldState()
{
lbl_length1.Text = "半徑";
lbl_length1.Visible = true;
txt_length1.Visible = true;
} */
}
}
第8組:
回覆刪除amount是使用Form1的成員然後Shape3D再去繼承,如果下次換了一個form那Shape3D就會少了amout這個成員。
而且繼承Form1也代表Shape3D具有Form1裡面所有的函式..
還是處理一下例外狀況吧,這個程式如果一開始直接按「加入」,就可以看到程式當掉了。
回覆刪除==第七組==
回覆刪除建構式裡的屬性,建議都使用double就好,在撰寫子類別時就可不必一直轉換成double
因為可透用double.parse將Text資料轉換成浮點數,會比較方便。