C#判断三角形的类型 C#判断三角形的类型

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#判断三角形的类型 C#判断三角形的类型

天外归云   2021-03-20 我要评论
想了解C#判断三角形的类型的相关内容吗,天外归云在本文为您仔细讲解C#判断三角形的类型的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#判断三角形类型,C语言判断三角形类型,java判断三角形类型,C语言判断三角形,下面大家一起来学习吧。

题目描述:

输入三角形的三条边长,判断是否能构成一个三角形(不考虑退化三角形,即面积为零的三角形),是什么样的三角形(直角、锐角、钝角、等边、等腰)。

函数声明为:byte GetTriangleType(int,int,int)

  1. 如何用一个byte来表示各种输出情况?

  2. 如果你是一名测试工程师,应该如何写测试用例来完成功能测试呢?

题目解析:

对于如何用一个byte表示各种输出情况,不是很清楚,有待研究。

下面的程序我只是实现了功能,并没有按照给定的函数声明的格式完成,大家可以参考参考

UI:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace TriangleTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Test_Click(object sender, EventArgs e)
    {
      //等腰,等边,直角,钝角,锐角。
      Dictionary<String, int> result = new Dictionary<String, int>();
      result.Add("等腰", 0);
      result.Add("等边", 0);
      result.Add("直角", 0);
      result.Add("钝角", 0);
      result.Add("锐角", 0);
      var t1 = edge1.Text;
      var t2 = edge2.Text;
      var t3 = edge3.Text;
      if (CheckInput(t1, t2, t3))
      {
        var e1 = double.Parse(edge1.Text);
        var e2 = double.Parse(edge2.Text);
        var e3 = double.Parse(edge3.Text);
        double[] Numbers = new double[] { e1, e2, e3 };
        double powSum = Math.Pow(e1, 2) + Math.Pow(e2, 2) + Math.Pow(e3, 2);
        double max = Numbers.Max();
        if (CheckTriangle(e1, e2, e3))
        {
          //三角形。
          result["等腰"] = CheckEquicrural(e1, e2, e3) ? 1 : 0;
          result["等边"] = CheckEquilateral(e1, e2, e3) ? 1 : 0;
          result["直角"] = CheckRightAngle(powSum, max) ? 1 : 0;
          result["钝角"] = CheckObtuseAngle(powSum, max) ? 1 : 0;
          result["锐角"] = CheckAcuteAngle(powSum, max) ? 1 : 0;
          string resultTip = result["等腰"] == 1 ? "等腰" : "";
          resultTip += result["等边"] == 1 ? "等边" : "";
          resultTip += result["直角"] == 1 ? "直角" : "";
          resultTip += result["钝角"] == 1 ? "钝角" : "";
          resultTip += result["锐角"] == 1 ? "锐角" : "";
          resultTip += "三角形";
          MessageBox.Show(resultTip);
        }
        else
        {
          //不是三角形。
          MessageBox.Show("您输入的三边构不成三角形!");
        }
      }
      else
      {
        //输入非法。
        MessageBox.Show("您输入的信息有问题!");
      }
    }

    private bool CheckAcuteAngle(double powSum, double max)
    {
      return (Math.Pow(max, 2) < powSum - Math.Pow(max, 2)) ? true : false;
    }

    private bool CheckObtuseAngle(double powSum, double max)
    {
      return (Math.Pow(max, 2) > powSum - Math.Pow(max, 2)) ? true : false;
    }

    private bool CheckRightAngle(double powSum, double max)
    {
      return (Math.Pow(max, 2) == powSum - Math.Pow(max, 2)) ? true : false;
    }

    private bool CheckEquicrural(double e1, double e2, double e3)
    {
      return (e1 == e2 && e2 == e3) ? true : false;
    }

    private bool CheckEquilateral(double e1, double e2, double e3)
    {
      return (e1 == e2 || e2 == e3 || e3 == e1) ? true : false;
    }

    private bool CheckTriangle(double edge1, double edge2, double edge3)
    {
      double[] edges = new double[] { edge1, edge2, edge3 };
      double sum = edges[0] + edges[1] + edges[2];
      int succFlag = 0;
      for (int i = 0; i < edges.Count(); i++)
      {
        if (edges[i] < sum - edges[i])
        {
          succFlag++;
        }
      }
      if (succFlag == 3)
      {
        return true;
      }
      else
      {
        return false;
      }
    }

    private bool CheckInput(string edge1, string edge2, string edge3)
    {
      bool result = false;
      Regex reg = new Regex("^[0-9]*$");
      if (reg.IsMatch(edge1) && reg.IsMatch(edge2) && reg.IsMatch(edge3))
      {
        if (Int32.Parse(edge1) > 0 && Int32.Parse(edge2) > 0 && Int32.Parse(edge3) > 0)
        {
          result = true;
        }
      }
      return result;
    }
  }
}

Run:

2. 对于功能测试而言:

  1)值的类型测试:注意输入值的种类(整形,浮点型,字符串类型等),检查对于非法值类型是否有控制逻辑;

  2)值的边界测试:注意输入值的范围(只能为非负数),检查超出范围时是否有控制逻辑;

  3)以结果为导向的测试:分别对非三角形,三角形中的等腰、等边、直角、钝角、锐角做出几组符合要求的测试数据,检查Test结果是否正确;

  4)值的长度测试:根据需求检查输入值达到最大值长度时,是否能够正常Test。

C#判断三角形的类型基本功能已经是现实,但是并没有按照给定的函数声明的格式完成,还有待于提高,或者是大家有什么好的建议,可以提出,尝试去解决。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们