问题描述: C#程序设计 输入三角形三边长,并计算的周长和面积 1个回答 分类:综合 2014-11-05 问题解答: 我来补答 前台有五个TextBox,分别是FirstTxt,SecondTxt,ThridTxt(代表a,b,c三边长),areaTxt,aroundTxt(代表面积,周长),一个计算的Button.对输入进行了控制,对是否可形成三角形进行了控制,最终计算.计算三角形面积用的是海伦公式.下面是源码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;namespace triangle{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } public double Alength; public double Blength; public double Clength; private Regex reg = new Regex("^(0|[1-9][0-9]*)$"); //用于检测的正则表达式 //只能输入0或者非0开头的数字 private void Form1_Load(object sender, EventArgs e) { this.FirstTxt.Text = "1"; this.SecondTxt.Text = "1"; this.ThirdTxt.Text ="1"; } private bool TestTriangle(double a,double b,double c) {//测试是否能形成三角形 double result1 = a + b; double result2 = a + c; double result3 = b + c; if (result1>c&&result2>b&&result3>a) { return true; } else { return false; } } private double CountArea(double a,double b,double c) {//计算面积 double p = (a+b+c)/2; double area = Math.Sqrt(p * (p - a) * (p - b) * (p - c)); return area; } private double CountAround(double a,double b,double c) {//计算边长 return a + b + c; } private void count_Click(object sender, EventArgs e) { if (reg.IsMatch(this.FirstTxt.Text)&®.IsMatch(this.SecondTxt.Text)&®.IsMatch(this.ThirdTxt.Text)) { Alength = Double.Parse(this.FirstTxt.Text); Blength = Double.Parse(this.SecondTxt.Text); Clength = Double.Parse(this.ThirdTxt.Text); if (TestTriangle(Alength, Blength, Clength)) { this.aroundtxt.Text = CountAround(Alength, Blength, Clength).ToString(); this.areatxt.Text = CountArea(Alength, Blength, Clength).ToString(); } else { MessageBox.Show("当前不形成三角形!"); } } else { MessageBox.Show("请输入合法的边长值!"); } } }} 展开全文阅读