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("等腰", );
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#中通过三边长判断三角形类型(三角形测试用例)的更多相关文章
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- strstr() strpos() 获取db报错,判断报错中是否包含字符串,判断错误类型
model中直接获取添加公司的错误.(公司名称不能重复) $enterprise_id = $this->add($enterprisedata ); $err = $this->getD ...
- shell中if的可判断的类型
-d :判断制定的是否为目录-z:判断制定的变量是否存在值-f:判断制定的是否为文件-L:判断制定的是否为符号链接-r:判断制定的是否可读-w:判断制定的是否可写-x:判断存在的对象是否可以执行!:测 ...
- 判断String类型字符串是否为空的方法
在项目中经常遇到要判断String类型的字段是否为空操作 我们可以用Apache提供的StringUtils这个工具类,不用自己去判断,也不用自己封装判断空的方法 它有两个版本,一个是org.apac ...
- js 判断浏览器类型
前言 工作中需要用到判断浏览器类型,网上找到的内容不怎么全,故在此进行一下总结. 一.不同浏览器及版本下User-Agent信息 待续.....欢迎补充 二.根据User-Agent信息进行判断 参考 ...
- Java开发中经典的小实例-(输入三个数字判断三角形类型)
import java.util.Scanner;public class threeTest { public static void main(String[] args) { ...
- 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;
考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...
- 【C语言】判断三角形类型
根据输入的三角形的三边判断三角形的类型,并输出其面积和类型. #include<stdio.h> #include<stdlib.h> #include<math.h&g ...
- PHP判断变量类型和类型转换的三种方式
前言: PHP 在变量定义中不需要(不支持)明确的类型定义.变量类型是根据使用该变量的上下文所决定的.所以,在面对页码跳转.数值计算等严格的格式需求时,就要对变量进行类型转换. 举例如下: $foo ...
随机推荐
- (转)flexigrid 参数说明
本文为转载 http://simple1024.iteye.com/blog/1171090 项目用到这玩意,像样的API都是英文的,英文不好,所以经过各种搜集,flexigrid就整理了这么多用得上 ...
- 转:python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...
- C++Builder加载Png图片
有两种方法,一是把该对象的Transparent 的属性设为true,图片的白色代表即为父界面的颜色 而是在头文件加上#include <pngimage.hpp> Image1-> ...
- interesting js
[5/3/2016 4:18 PM] Calos Chen: function a(b,e){var a=0;a+=b;if(e){console.log(b+e);return;} retur ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- [转]JVM内幕:Java虚拟机详解
本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...
- oracle,sqlserver,mysql 命令行 开启、关闭所需要的服务
ORACLE需要开启的服务 需要启动的服务: 口令: 启动Oracle 11g服务: (下面的可以作为bat 脚本,直接运行便可以不用去自己去启动和关闭服务了.) @echo off @ EC ...
- 继承 Bean 配置
继承 Bean 配置 Spring 允许继承 bean 的配置, 被继承的 bean 称为父 bean. 继承这个父 Bean 的 Bean 称为子 Bean子 Bean 从父 Bean 中继承配置, ...
- android中在代码中设置margin属性
1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...
- html随笔
<!DOCTYPE HTML> <html> <head> <meta charset = "utf-8"> <script ...