C#中通过三边长判断三角形类型

对于《编程之美》P292上关于三角形测试用例的问题,题目是这样的:

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

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

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

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

我的解答

1. 我不知道如何用一个byte表示各种输出情况,谁能指点一下?

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

UI:

Code:

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。

时间: 2024-08-01 10:41:10

C#中通过三边长判断三角形类型的相关文章

九度oj 题目1048:判断三角形类型

题目1048:判断三角形类型 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8240 解决:3992 题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输出: 对于每组输入,输出直角三角形.锐角三角形.或是钝角三角形. 样例输入: 3 4 5 样例输出: 直角三角形 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 i

「C语言」「算法」根据三边判断三角形类型

1 #include <stdio.h> 2 3 //根据三边长判断是否能组成三角形,分别以边长和角度看能组成什么三角形 4 5 int main(){ 6 int a,b,c; 7 printf("请输入三边的长,并以逗号隔开:\n"); 8 scanf("%d,%d,%d",&a,&b,&c); 9 if(a<=0 || b<=0 || c<=0){ 10 printf("请输入正数");

判断三角形类型-2009年哈尔滨工业大学计算机研究生机试真题

题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输出: 对于每组输入,输出直角三角形.锐角三角形.或是钝角三角形. 样例输入: 3 4 5 样例输出: 直角三角形 解题代码: #include <stdio.h> int main(){ int arr[3]; int temp; while (scanf("%d%d%d",&arr[0],&arr[1],&arr[2] ) != EO

【C语言】判断三角形类型

根据输入的三角形的三边判断三角形的类型,并输出其面积和类型. #include<stdio.h> #include<stdlib.h> #include<math.h> int main() { float a, b, c; float s, area; printf("please input three line:\n"); scanf("%f%f%f", &a, &b, &c); //判断是否满足三角

九度OJ—题目1048:判断三角形类型

题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输出: 对于每组输入,输出直角三角形.锐角三角形.或是钝角三角形. 样例输入: 3 4 5 样例输出: 直角三角形 来源: 2009年哈尔滨工业大学计算机研究生机试真题 答疑: 解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7772-1-1.html #include <iostream> using namespace std;

1048.判断三角形类型

题目描述: 给定三角形的三条边,a,b,c.判断该三角形类型. 输入: 测试数据有多组,每组输入三角形的三条边. 输出: 对于每组输入,输出直角三角形.锐角三角形.或是钝角三角形. 样例输入: 3 4 5 样例输出: 直角三角形 #include<iostream> using namespace std; int sanjiaoxing(int a,int b,int c){ int temp; if(a<b){ temp=a; a=b; b=temp; } if(a<c){ t

django中三种判断请求类型的方法

1.面向对象方法 在views.py中编写 引入模块 from django import views 函数编写,创建类文件 class View(views.View): def get(self, request): print('GET方法') return HttpResponse('GET方法') def post(self, request): print('POST方法') return HttpResponse('POST方法') 配置路由 urls.py中编写 引入模块 fro

题目1048:判断三角形类型----------有规律的

此题的判断思路:两条较短边的平方和大于最长边的平方,此三角形就是锐角三角形: 两条较短边的平方和小于最长边的平方,此三角形就是钝角三角形: 两条边短边的平方和等于最长边的平方,此三角形就是直角三角形. 由此AC: #include<iostream> using namespace std; int main() { int a,b,c; while(cin>>a>>b>>c) { if ((c*c+a*a-b*b==0)||(b*b+a*a-c*c==0)

strstr() strpos() 获取db报错,判断报错中是否包含字符串,判断错误类型

model中直接获取添加公司的错误.(公司名称不能重复) $enterprise_id = $this->add($enterprisedata ); $err = $this->getDbError(); $err =="1062:Duplicate entry 'aaa' for key 'enterprise_name'\n [ SQL\u8bed\u53e5 ] : INSERT INTO `t_enterprise` (`enterprise_name`,`enterpri