对于《编程之美》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. for 穷举、迭代 while循环

    1.穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 2.百鸡百钱:公鸡2文钱一只,母鸡1文钱一只,小鸡半文钱一只,总共只有100文钱,如何在凑够100只鸡的情况下刚好花完100 ...

  2. PHP内核探索:哈希碰撞攻击是什么?

    最近哈希表碰撞攻击(Hashtable collisions as DOS attack)的话题不断被提起,各种语言纷纷中招.本文结合PHP内核源码,聊一聊这种攻击的原理及实现. 哈希表碰撞攻击的基本 ...

  3. swt byte[] 与 Image的转换

    1. 从byte[]得到Image private static Image createImage(byte[] imageBytes) { Image image = null; try { By ...

  4. ajax“显示弹窗详情”和“删除”功能练习

    1.查看详细信息,以弹窗的形式显示,使用ajax 2.批量删除 “查询”功能可以参考前面的文章,这里只讲解ajax“显示弹窗详情”和“删除”功能 第一:在body中的代码 <title>a ...

  5. Silverlight ModelView中调用UI进程

    Silverlihgt:  Deployment.Current.Dispatcher.BeginInvoke wpf: App.Current.Dispatcher.Invoke  

  6. DOM4J解析xml案例

  7. TI CC254x BLE教程 4

    TI的CC254x芯片 1. SoC 2. RF收发器+8051MCU 128/256KB Code空间. 3. Master或者Slave 4. 可编程flash 5. 8KB SRAM 6. 全软 ...

  8. 将linux下的rm命令改造成移动文件至回收站【转】

    转自:http://blog.csdn.net/a3470194/article/details/16863803 [-] 将linux下的rm命令改造成移动文件至回收站 将AIX下的rm命令改造成移 ...

  9. Spring的beans标签下可以有其他标签

    以前有对xsd(也就是schema文件)小做研究,有个小困惑,就是我们定义的元素只能使用定义的哪一些标签,比如<beans>下面就只能有自定义的哪一些,那为什么在引入<context ...

  10. Oracle中merge into的使用

    http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...