大家都知道,三角形有好几种计算面积的方式,比如我们最常见的【面积=底*高/2】,我们还可以海伦公式和边角边面积计算出我们的面积,这样我们就能够根据已知条件选择对应的方式来计算三角形面积:
方式一:S=dh/2
case "方法1":
di = Convert.ToDouble(tb_di.Text);
gao=Convert.ToDouble(tb_gao.Text);
S1 = (di * gao) / 2;
lb_mianji.Text=Convert.ToString(S1);
break;
方式二:海伦公式
case "方法2":
a = Convert.ToDouble(tb_a.Text);
b = Convert.ToDouble(tb_b.Text);
c= Convert.ToDouble(tb_c.Text);
p = (a + b + c) / 2;
S2 = Math.Sqrt(p*(p-a)*(p-b)*(p-c));
lb_mianji.Text=Convert.ToString(S2);
break;
方式三:边角边公式
case "方法3":
a1= Convert.ToDouble(tb_a1.Text);
b1 = Convert.ToDouble(tb_b1.Text);
x = Convert.ToDouble(tb_x.Text);
S3 = a1 * b1 * Math.Sin(x * Math.PI / 180)/2;
lb_mianji.Text = Convert.ToString(S3);
break;
然后我们需要做的就是根据已知条件,选择对应的方法,这里就借助了switch函数,所以我们的小案例可以这样写:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sanjiaoxinmianji
{
public partial class Form1 : Form
{
public double di;
public double gao;
public double a;
public double b;
public double c;
public double p;
public double a1;
public double b1;
public double x;
public static double S1;
public static double S2;
public static double S3;
public Form1()
{
InitializeComponent();
}
string fanfa;
private void btn_1_Click(object sender, EventArgs e)
{
fanfa = cb_xuanze.Text;
switch (fanfa)
{
case "方法1":
di = Convert.ToDouble(tb_di.Text);
gao=Convert.ToDouble(tb_gao.Text);
S1 = (di * gao) / 2;
lb_mianji.Text=Convert.ToString(S1);
break;
case "方法2":
a = Convert.ToDouble(tb_a.Text);
b = Convert.ToDouble(tb_b.Text);
c= Convert.ToDouble(tb_c.Text);
p = (a + b + c) / 2;
S2 = Math.Sqrt(p*(p-a)*(p-b)*(p-c));
lb_mianji.Text=Convert.ToString(S2);
break;
case "方法3":
a1= Convert.ToDouble(tb_a1.Text);
b1 = Convert.ToDouble(tb_b1.Text);
x = Convert.ToDouble(tb_x.Text);
S3 = a1 * b1 * Math.Sin(x * Math.PI / 180)/2;
lb_mianji.Text = Convert.ToString(S3);
break;
default:
break;
}
}
}
}
大家也可以通过这个给孩子做一个数学题验算小工具