c# 图形周长、面积计算

namespace 实验1
{
     public partial class Form1:Form
     {
          public Form1()
          {
               InitializeComponent();
          }
           private void button1_Click(object sender, EventArgs e)
        {          
            double a = double.Parse(txtSanA .Text);
            double b = double.Parse(txtSanB.Text);
            double c = double.Parse(txtSanC .Text);
            Triangle s3 = new Triangle(a,b,c);
            txtC.Text = string.Format("{0}", s3.GetPerimeter());
            txtS .Text = string.Format("{0}", s3.GetArea());
        }
......
        private void button1_Click_1(object sender, EventArgs e)
        {
            double x = double.Parse(txtInputy2.Text);
            double y = double.Parse(txtInputY1.Text);
            Rectangle s2 = new Rectangle(x,y);
            txtC.Text = string.Format("{0}", s2.GetPerimeter());
            txtS.Text = string.Format("{0}", s2.GetArea());
        }
        private void button2_Click(object sender, EventArgs e)
        {
            double r = double.Parse(txtYuan.Text);
            Circle s1 = new Circle(r);
            txtC.Text = string.Format("{0}", s1.GetPerimeter());
            txtS.Text = string.Format("{0}", s1.GetArea());
        }
    }
}
public abstract class Shape
{
     public abstract double GetPerimeter();//周长 抽象方法体没有{}
     public abstract double GetArea();  //面积
     public Shape ()
     { }
}
public class Circle : Shape                 //圆形
{
    public double r;
    public Circle (double r)
    {
        this.r = r;
    }
      public override double GetPerimeter()
    {
        return 2 * Math.PI * r;
    }
    public override double GetArea()
    {
        return Math.PI * r * r;
    }
}
public class Rectangle : Shape            //矩形
{
    public double x, y;
    public Rectangle (double x,double y)
    {
        this.x = x;
        this.y = y;
    }
    public override double GetPerimeter()
    {
        return 2 * (x + y);
    }
    public override double GetArea()
    {
        return x * y;
    }
}
public class Triangle : Shape            //三角形
{
    public double a, b, c;
    public Triangle (double a,double b,double c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
     public override double GetPerimeter()
    {
        return a + b + c;
    }
    public override double GetArea()
    {
        return System.Math.Sqrt((a + b + c) / 2 * (a + b + c / 2 - a) * (a + b + c / 2 - b) * (a + b + c / 2 - c));
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值