Program flow chart:
Code:
Triangle class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace triangle determination { 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; } //Triangle judgment 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; } } // Determine if the value is an equilateral triangle public bool f2() { if (f1() && a == b && b == c && a == c) return true; else return false; } // isosceles triangle public bool f3() { if (f1()) { if (a == b || b == c || a == c)//? return true; else return false; } else return false; } //right triangle 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; } // obtuse triangle 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; } //Acute triangle 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 triangle determination { 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())//Judge the triangle { if(t.f6())//Acute angle { if (t.f3())//isosceles { if (t.f2())//equilateral { MessageBox.Show("equilateral triangle"); } else { MessageBox.Show("Isosceles acute triangle"); } } 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 = ""; } } }
测试结果: