2015年5月14日 星期四

[2015][Quiz][Week12]Quiz06 - Team04

From1.cs
using quiz5;
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;
using System.Collections;

namespace _2015OOP_Quiz5_win
{
    public partial class Form1 : Form
    {
        private ArrayList shapeArr=new ArrayList();
        private static double[] densityArr = { 2.7, 7.87, 11.3};

        public Form1()
        {
            InitializeComponent();     
            lblPara1.Visible = false;
            txtPara1.Visible = false;  
            lblPara2.Visible = false;
            txtPara2.Visible = false;  
        }

        private void cboxShapeSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboxShapeSet.SelectedIndex)
            { 
                case 0:
                    lblPara1.Visible = true;
                    txtPara1.Visible = true;  
                    lblPara1.Text = "半徑";
                    lblPara2.Visible = false;
                    txtPara2.Visible = false;                      
                    break;
                case 1:
                    lblPara1.Visible = true;
                    txtPara1.Visible = true; 
                    lblPara1.Text = "邊長";
                    lblPara2.Visible = false;
                    txtPara2.Visible = false;
                    break;
                case 2:
                    lblPara1.Visible = true;
                    txtPara1.Visible = true; 
                    lblPara1.Text = "半徑";
                    lblPara2.Visible = true;
                    lblPara2.Text = "高";
                    txtPara2.Visible = true;
                    break;
                case 3:
                    lblPara1.Visible = true;
                    txtPara1.Visible = true; 
                    lblPara1.Text = "邊長";
                    lblPara2.Visible = true;
                    lblPara2.Text = "高";
                    txtPara2.Visible = true;
                    break;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int amount = shapeArr.Count;
            double density = densityArr[cboxMaterialSet.SelectedIndex];
            switch (cboxShapeSet.SelectedIndex)
            {
                case 0:
                    Ball ball = new Ball(double.Parse(txtPara1.Text),density);
                    shapeArr.Add(ball);
                    break;
                case 1:
                    Cube cube = new Cube(double.Parse(txtPara1.Text), density);
                    shapeArr.Add(cube);
                    break;
                case 2:
                    Cylinder cylinder = new Cylinder(double.Parse(txtPara1.Text), double.Parse(txtPara2.Text), density);
                    shapeArr.Add(cylinder);
                    break;
                case 3:
                    Pyramid pyramid = new Pyramid(double.Parse(txtPara1.Text), double.Parse(txtPara2.Text), density);
                    shapeArr.Add(pyramid);
                    break;
                default:
                    break;
            }
            txtShapeNum.Text = shapeArr.Count.ToString();
            txtAdd.AppendText(((Shape3D)shapeArr[amount]).ShowProperty() + Environment.NewLine);
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            txtShowSolution.Clear();
            for (int i = 0; i < shapeArr.Count; i++)
            {
                txtShowSolution.AppendText(((Shape3D)shapeArr[i]).ShowResults() + Environment.NewLine);
            }
        }

        private bool CompareByVolumeAscent(Shape3D shapeA, Shape3D shapeB)
        {
            if (shapeA.Volume() > shapeB.Volume())
                return true;
            else
                return false;
        }
        private bool CompareByVolumeDescent(Shape3D shapeA, Shape3D shapeB)
        {
            if (shapeA.Volume() < shapeB.Volume())
                return true;
            else
                return false;
        }
        private bool CompareByWeightAscent(Shape3D shapeA, Shape3D shapeB)
        {
            if (shapeA.Weight() > shapeB.Weight())
                return true;
            else
                return false;
        }
        private bool CompareByWeightDescent(Shape3D shapeA, Shape3D shapeB)
        {
            if (shapeA.Weight() < shapeB.Weight())
                return true;
            else
                return false;
        }

        private delegate bool CompareFun(Shape3D shapeA, Shape3D shapeB);

        private void BubbleSort(ArrayList arr, CompareFun cmp)
        {
            for (int pass = 0; pass < arr.Count; pass++)
            {
                for (int i = 0; i < arr.Count - 1; i++)
                {
                    if (cmp((Shape3D)arr[i], (Shape3D)arr[i + 1]))
                    {
                        Shape3D temp;
                        temp = (Shape3D)arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = temp;
                    }
                }

            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            
            switch (cmbSortMethod.SelectedIndex)
            { 
                case 0:
                    if (rdbSortAscent.Checked)
                        BubbleSort(shapeArr, CompareByWeightAscent);
                    else if(rdbSortDescent.Checked)
                        BubbleSort(shapeArr, CompareByWeightDescent);
                    break;
                case 1:
                    if (rdbSortAscent.Checked)
                        BubbleSort(shapeArr, CompareByVolumeAscent);
                    else if (rdbSortDescent.Checked)
                        BubbleSort(shapeArr,CompareByVolumeDescent);
                    break;
            }
            Display(shapeArr);

        }
        public void Display(ArrayList arr)
        {
            txtShowSolution.Clear();
            for (int i = 0; i < arr.Count; i++)
            { 
                txtShowSolution.AppendText(((Shape3D)arr[i]).ShowResults()+Environment.NewLine);               
            }
        }
    }
}

Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace quiz5
{
    abstract class Shape3D
    {
        protected double density;
        private static int _amount = 0;

        protected Shape3D()
        {
            Density = 0;
            _amount++;
        }

        protected Shape3D(double d)
        {
            Density = d;
            _amount++;
        }

        public double Density
        {
            get { return density; }
            set
            {
                if (density<0)
                {
                    density = 0;
                }
                else
                {
                    density = value;
                }
            }
        }
        public static int Amount
        {
            get { return _amount; }
        }

        public abstract double Volume();
        
        public double Weight()
        {
            return density * Volume();
        }

        public abstract string ShowProperty();

        public string ShowResults()
        {
            string results = ShowProperty();
            results+='\t';
            results += string.Format("{0,8:F2}", density);
            results += '\t';
            results += string.Format("{0,8:F2}", Volume());
            results += '\t';
            results += string.Format("{0,8:F2}", Weight());
            return results;

        }
    }
}

Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quiz5
{
    class Ball:Shape3D
    {
        private double _radius;

        public Ball (double _radius, double density)
            :base(density)
        {
            Radius = _radius;
        }

        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius<0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        public override double Volume()
        {
            return 4.0 / 3 * Constant.pi * _radius * _radius * _radius;
        }

        public override string ShowProperty()
        {
            string str = "Ball";
            str += '\t';
            str += string.Format("{0,8:F2}", _radius);
            str += '\t';
            str += "";
            return str;
        }
    }
}

Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quiz5
{
    class Cube:Shape3D
    {
        private double _length;

        public Cube(double _length, double density)
            : base(density)
        {
            Length = _length;
        }

        public double Length
        {
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        public override double Volume()
        {
            return _length * _length * _length;
        }

        public override string ShowProperty()
        {
            string str = "Cube";
            str += '\t';
            str += string.Format("{0,8:F2}", _length);
            str += '\t';
            str += "";
            return str;
        }
    }
}

Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quiz5
{
    class Cylinder:Shape3D
    {
        private double _radius;
        private double _height;

        public Cylinder(double _radius, double _height, double density)
            :base(density)
        {
            Radius = _radius;
            Height = _height;
        }

        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius<0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        public override double Volume()
        {
            return Constant.pi * _radius * _radius * _height;
        }

        public override string ShowProperty()
        {
            string str = "Cylinder";
            str += '\t';
            str += string.Format("{0,8:F2}", _radius);
            str += '\t';
            str += string.Format("{0,8:F2}", _height);
            return str;
        }
    }
}

Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quiz5
{
    class Pyramid:Shape3D
    {
        private double _length;
        private double _height;

        public Pyramid(double _length, double _height, double density)
            :base(density)
        {
            Length = _length;
            Height = _height;
        }

        public double Length
        { 
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        public override double Volume()
        {
            return 1.0/3*_length*_length*_height;
        }

        public override string ShowProperty()
        {
            string str = "Pyramid";
            str += '\t';
            str += string.Format("{0,8:F2}", _length);
            str += '\t';
            str += string.Format("{0,8:F2}", _height);
            return str;
        }
    }
}

Constant.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace quiz5
{
    static class Constant
    {
        public static double pi = 3.1415926;
    }
}

沒有留言:

張貼留言