728x90 AdSpace

Latest News
Friday, May 18, 2018

Xây Dựng Lớp - Đối Tượng Bài 2:Xây Dựng một class tính chu vi,diện tích hình tam giác

Đề:  Xây Dựng một class tính chu vi,diện tích hình tam giác và xây dựng form như hình sau




Code Xử lý:

xây dựng class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tamgiac
{
    class tamgiac
    {
        private double a1;
        private double b1;
        private double c1;
        public tamgiac()
        {
            a1 = 0;
            b1 = 0;
            c1 = 0;
        }
        public tamgiac(double canha, double canhb, double canhc)
        {
            a1 = canha;
            b1 = canhb;
            c1 = canhc;
        }
        public double tinhchuvi()
        {
            double cv;
            cv = (a1 + b1 + c1) / 2;
            return cv;
        }
        public double tinhdientich()
        {
            double dt;
            double p = (a1 + b1 + c1) / 2;
            dt = Math.Sqrt(p * (p - a1) * (p - b1) * (p - c1));
            return dt;
        }

    }
}

Xây Dựng form 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tamgiac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (a.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh a", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (b.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh b", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
           else if (c.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh c", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                double a1 = Convert.ToInt32(a.Text);
                double b1 = Convert.ToInt32(b.Text);
                double c1 = Convert.ToInt32(c.Text);
                tamgiac tinh = new tamgiac(a1, b1, c1);
                chuvi.Text = tinh.tinhchuvi().ToString();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)

        {
             if (a.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh a", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
          else  if (b.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh b", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
             else
                  if (c.Text == "")
            {
                MessageBox.Show("bạn chưa nhập cạnh c", "chú ý", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                double a1 = Convert.ToInt32(a.Text);
                double b1 = Convert.ToInt32(b.Text);
                double c1 = Convert.ToInt32(c.Text);
                tamgiac tinh = new tamgiac(a1, b1, c1);
                dientich.Text = tinh.tinhdientich().ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            a.Text = "";
            b.Text = "";
            c.Text = "";
            chuvi.Text = "";
            dientich.Text = "";

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}




  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Xây Dựng Lớp - Đối Tượng Bài 2:Xây Dựng một class tính chu vi,diện tích hình tam giác Rating: 5 Reviewed By: Genm