对于《编程之美》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("等腰", );
result.Add("等边", );
result.Add("直角", );
result.Add("钝角", );
result.Add("锐角", );
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, ) + Math.Pow(e2, ) + Math.Pow(e3, );
double max = Numbers.Max();
if (CheckTriangle(e1, e2, e3))
{
//三角形。
result["等腰"] = CheckEquicrural(e1, e2, e3) ? : ;
result["等边"] = CheckEquilateral(e1, e2, e3) ? : ;
result["直角"] = CheckRightAngle(powSum, max) ? : ;
result["钝角"] = CheckObtuseAngle(powSum, max) ? : ;
result["锐角"] = CheckAcuteAngle(powSum, max) ? : ;
string resultTip = result["等腰"] == ? "等腰" : "";
resultTip += result["等边"] == ? "等边" : "";
resultTip += result["直角"] == ? "直角" : "";
resultTip += result["钝角"] == ? "钝角" : "";
resultTip += result["锐角"] == ? "锐角" : "";
resultTip += "三角形";
MessageBox.Show(resultTip);
}
else
{
//不是三角形。
MessageBox.Show("您输入的三边构不成三角形!");
}
}
else
{
//输入非法。
MessageBox.Show("您输入的信息有问题!");
}
} private bool CheckAcuteAngle(double powSum, double max)
{
return (Math.Pow(max, ) < powSum - Math.Pow(max, )) ? true : false;
} private bool CheckObtuseAngle(double powSum, double max)
{
return (Math.Pow(max, ) > powSum - Math.Pow(max, )) ? true : false;
} private bool CheckRightAngle(double powSum, double max)
{
return (Math.Pow(max, ) == powSum - Math.Pow(max, )) ? 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[] + edges[] + edges[];
int succFlag = ;
for (int i = ; i < edges.Count(); i++)
{
if (edges[i] < sum - edges[i])
{
succFlag++;
}
}
if (succFlag == )
{
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) > && Int32.Parse(edge2) > && Int32.Parse(edge3) > )
{
result = true;
}
}
return result;
}
}
}

Run:

2. 对于功能测试而言:

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

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

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

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

在学习二进制和按位“|”的用法之后:

经过学习二进制和按位或“|”的用法,我将程序做了修改,终于满足了要求(但是由于三边都是int类型是无法表示直角等腰三角形的,所以我把int改成了double类型):

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)
{
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);
byte result = GetTriangleType(e1, e2, e3);
if (result == )
{
MessageBox.Show("您输入的三边构不成三角形!");
}
else
{
//十进制转换为二进制。
string tip = Convert.ToString(result, ) + "\n锐角 钝角 直角 等边 等腰(1代表是)";
MessageBox.Show(tip);
/*
1 1 1 1 1
锐角 钝角 直角 等边 等腰
*/
}
}
else
{
//输入非法。
MessageBox.Show("您输入的信息有问题!");
}
}
byte GetTriangleType(double e1, double e2, double e3)
{
//等腰00001=1,等边00010=2,直角00100=4,钝角01000=8,锐角10000=16。
byte result = ;
double[] Numbers = new double[] { e1, e2, e3 };
double powSum = Math.Pow(e1, ) + Math.Pow(e2, ) + Math.Pow(e3, );
double max = Numbers.Max();
if (CheckTriangle(e1, e2, e3))
{
//三角形。
if (CheckEquicrural(e1, e2, e3))
{
result |= ;//等腰。
}
if (CheckEquilateral(e1, e2, e3))
{
result |= ;//等边。
}
if (CheckRightAngle(powSum, max))
{
result |= ;//直角。
}
if (CheckObtuseAngle(powSum, max))
{
result |= ;//钝角。
}
if (CheckAcuteAngle(powSum, max))
{
result |= ;//锐角。
}
return result;
}
else
{
//不是三角形。
return ;
}
}
private bool CheckAcuteAngle(double powSum, double max)
{
return (Math.Pow(max, ) < powSum - Math.Pow(max, )) ? true : false;
} private bool CheckObtuseAngle(double powSum, double max)
{
return (Math.Pow(max, ) > powSum - Math.Pow(max, )) ? true : false;
} private bool CheckRightAngle(double powSum, double max)
{
return (Math.Pow(max, ) == powSum - Math.Pow(max, )) ? 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[] + edges[] + edges[];
int succFlag = ;
for (int i = ; i < edges.Count(); i++)
{
if (edges[i] < sum - edges[i])
{
succFlag++;
}
}
if (succFlag == )
{
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) > && Int32.Parse(edge2) > && Int32.Parse(edge3) > )
{
result = true;
}
}
return result;
}
}
}

C#中通过三边长判断三角形类型(三角形测试用例)的更多相关文章

  1. Python中请使用isinstance()判断变量类型

    一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...

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

    model中直接获取添加公司的错误.(公司名称不能重复) $enterprise_id = $this->add($enterprisedata ); $err = $this->getD ...

  3. shell中if的可判断的类型

    -d :判断制定的是否为目录-z:判断制定的变量是否存在值-f:判断制定的是否为文件-L:判断制定的是否为符号链接-r:判断制定的是否可读-w:判断制定的是否可写-x:判断存在的对象是否可以执行!:测 ...

  4. 判断String类型字符串是否为空的方法

    在项目中经常遇到要判断String类型的字段是否为空操作 我们可以用Apache提供的StringUtils这个工具类,不用自己去判断,也不用自己封装判断空的方法 它有两个版本,一个是org.apac ...

  5. js 判断浏览器类型

    前言 工作中需要用到判断浏览器类型,网上找到的内容不怎么全,故在此进行一下总结. 一.不同浏览器及版本下User-Agent信息 待续.....欢迎补充 二.根据User-Agent信息进行判断 参考 ...

  6. Java开发中经典的小实例-(输入三个数字判断三角形类型)

    import java.util.Scanner;public class threeTest {    public static void main(String[] args) {       ...

  7. 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;

    考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...

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

    根据输入的三角形的三边判断三角形的类型,并输出其面积和类型. #include<stdio.h> #include<stdlib.h> #include<math.h&g ...

  9. PHP判断变量类型和类型转换的三种方式

    前言: PHP 在变量定义中不需要(不支持)明确的类型定义.变量类型是根据使用该变量的上下文所决定的.所以,在面对页码跳转.数值计算等严格的格式需求时,就要对变量进行类型转换. 举例如下: $foo ...

随机推荐

  1. 将url转化成对象

    <script> var url = "baidu.com?"; //var url = window.location.search; //获取url functio ...

  2. springday03-go1

    springday02项目下新建包annotation11.复制xml文件到包annotation1下,并添加组件扫描方式代码2.Waiter类实现序列化接口,构造函数,并使用特定注解标记waiter ...

  3. 反射认识_06_ArrayList_HashSet区别

    包01: package ReflectionCollection; public class ReflectionConstructorPoint { private int x; public i ...

  4. .net 网站预编译命令

    aspnet_compiler -v /Aspnet  -p "C:\inetpub\wwwroot\a"  C:\inetpub\wwwroot\a2 /Aspnet   iis ...

  5. xsd与xsl文件的区别

    请问xsd与xsl文件的区别在哪里呢?   我刚学xml,一开始用xmlspy学的,然后生成了一个xsd的文件,但在网上下的源程序是有一个xsl文件的,很不明之间有什么区别的,为什么别人的程序不用xs ...

  6. zw版_zw中文增强版Halcon官方Delphi例程

    [<zw版·delphi与halcon系列原创教程>zw版_zw中文增强版Halcon官方Delphi例程 源码下载:http://files.cnblogs.com/files/ziwa ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON Cast 使用方式

    zw版[转发·台湾nvp系列Delphi例程]HALCON Cast 使用方式 procedure TForm1.Button1Click(Sender: TObject);var img, img1 ...

  8. SVN使用(二)

    TortoiseSVN是windows平台下Subversion的免费开源客户端. 一般我们都是先讲讲服务器的配置,然后再讲客户端的使用,但是在TortoiseSVN上,却可以反过来.因为,如果你的要 ...

  9. Elasticsearch DSL语句之连接查询

    传统数据库支持的full join(全连接)查询方式. 这种方式在Elasticsearch中使用时非常昂贵的.因此,Elasticsearch提供两种操作可以支持水平扩展 更多内容请参考Elasti ...

  10. WM_SETFOCUS和WM_KILLFOCUS、WM_GETDLGCODE、CM_ENTER...

    procedure WMSetFocus (var Message: TWMSetFocus); message WM_SETFOCUS; //获得焦点 procedure WMKillFocus ( ...