软件测试 三角形判定

程序流程图:


代码:

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 = "";
        }
    }
}

测试结果:


猜你喜欢

转载自blog.csdn.net/qq_36109528/article/details/80224289
今日推荐