軟體測試 三角形判定
阿新 • • 發佈:2019-02-11
程式流程圖:
程式碼:
Triangle類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 三角形判定 { public class Triangle { public double a, b, c; public Triangle() { ; } public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } //三角形判斷 public bool f1() { if (a > 0 && b > 0 && c > 0) { if (a + b > c && a + c > b && b + c > a) { return true; } else return false; } else { return false; } } //判斷值是否等邊三角形 public bool f2() { if (f1() && a == b && b == c && a == c) return true; else return false; } //等腰三角形 public bool f3() { if (f1()) { if (a == b || b == c || a == c)//? return true; else return false; } else return false; } //直角三角形 public bool f4() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) == Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) == Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) == Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } //鈍角三角形 public bool f5() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) > Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) > Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) > Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } //銳角三角形 public bool f6() { double max; max = Math.Max(a, b); max = Math.Max(max, c); if (max == a) { if (Math.Pow(max, 2) < Math.Pow(b, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == b) { if (Math.Pow(max, 2) < Math.Pow(a, 2) + Math.Pow(c, 2)) return true; else return false; } else if (max == c) { if (Math.Pow(max, 2) < Math.Pow(b, 2) + Math.Pow(a, 2)) return true; else return false; } else return false; } } }
Form:
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 三角形判定 { public partial class Main : Form { Triangle t=new Triangle(); public Main() { InitializeComponent(); } private void btnSubmit_Click(object sender, EventArgs e) { t = new Triangle(Convert.ToDouble(la.Text.Trim()), Convert.ToDouble(lb.Text.Trim()), Convert.ToDouble(lc.Text.Trim())); if (t.f1())//判斷三角形 { if(t.f6())//銳角 { if (t.f3())//等腰 { if (t.f2())//等邊 { MessageBox.Show("等邊三角形"); } else { MessageBox.Show("等腰銳角三角形"); } } else { MessageBox.Show("銳角三角形"); } } else if (t.f4())//直角 { if (t.f3())//等腰 { MessageBox.Show("等腰直角三角形"); } else { MessageBox.Show("直角三角形"); } } else if (t.f5())//鈍角 { if (t.f3())//等腰 { MessageBox.Show("等腰鈍角三角形"); } else { MessageBox.Show("鈍角三角形"); } } } else//不能構成三角形 { MessageBox.Show("不能構成三角形"); } } private void btnReset_Click(object sender, EventArgs e) { la.Text = ""; lb.Text = ""; lc.Text = ""; } } }
測試結果: