作业帮 > 综合 > 作业

用C#做一个三角形判定的程序

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/14 08:21:13
用C#做一个三角形判定的程序
假设某段程序要求完成下述功能:从三个文本输入对话框中读取三个数值.这三个数值代表了三角形三边的长度.该程序可以用于分类三角形,即通过显示提示信息,指出该三角形是不规则三角形、等腰三角形还是等边三角形.
根据这段哈uxieyige测试用例,并根据测试用例编写一个程序,要求有前台界面.用visual studio 做,
用C#做
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 :Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e) {
var aStr = this.textBox1.Text.Trim();
var bStr = this.textBox2.Text.Trim();
var cStr = this.textBox3.Text.Trim();
//不为空
if (string.IsNullOrEmpty(aStr) && string.IsNullOrEmpty(bStr) && string.IsNullOrEmpty(cStr)) {
this.label4.Text = "A,B,C都不可以为空";
return;
}
//都是数字
float a,b,c;
if (!float.TryParse(aStr,out a)) {
this.label4.Text = "输入的A不是数字";
return;
}
if (!float.TryParse(bStr,out b)) {
this.label4.Text = "输入的B不是数字";
return;
}
if (!float.TryParse(cStr,out c)) {
this.label4.Text = "输入的C不是数字";
return;
}
//三遍从大到小排序 依次为a,b,c
if (a < b) {
float d = a;
a = b;
b = d;
}
if (a < c) {
float d = a;
a = c;
c = d;
}
if (b < c) {
float d = b;
b = c;
c = d;
}
//判断是否能构成三角形
if (!((a + b > c) && (a - b < c) && (b + c > a) && (b - c < a) && (a + c > b) && (a - c < b))) {
this.label4.Text = "此三边不能构成三角形";
return;
}
//判断是否等边
if (a == b&&b == c) {
this.label4.Text = "此三角形为等边三角形";
return;
}
//判断是否等腰
if (a == b || a == c || b == c) {
this.label4.Text = "此三角形为等腰三角形";
return;
}
this.label4.Text = "此三角形为不规则三角形";
}
}
}