2015年4月11日 星期六

[2015][Quiz][Week07]Quiz04-40173008H

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 Quiz4
{
    public partial class pinboard : Form
    {
        PinBoard board = new PinBoard(); //建立板子
        
        public pinboard()
        {

            InitializeComponent();
        }

        private void btn_calculate_Click(object sender, EventArgs e)
        {


            Triangle tri = new Triangle();
            int[] row = new int[3];
            int[] col = new int[3];
            col[0] = int.Parse(txtCol1.Text);
            row[0] = int.Parse(txtRow1.Text);
            col[1] = int.Parse(txtCol2.Text);
            row[1] = int.Parse(txtRow2.Text);
            col[2] = int.Parse(txtCol3.Text);
            row[2] = int.Parse(txtRow3.Text);
            for (int i = 0; i <= 2; i++)
            {
                tri.pointArray[i] = board.pinArray[row[i], col[i]];
            }
            board.SetPinsPosition();
            txtX1.Text = tri.pointArray[0].xCoordinate.ToString();
            txtY1.Text = tri.pointArray[0].yCoordinate.ToString();
            txtX2.Text = tri.pointArray[1].xCoordinate.ToString();
            txtY2.Text = tri.pointArray[1].yCoordinate.ToString();
            txtX3.Text = tri.pointArray[2].xCoordinate.ToString();
            txtY3.Text = tri.pointArray[2].yCoordinate.ToString();
            if (!tri.ConditionsOfTriangle())
                txt_Display.AppendText("以上三點無法構成三角形,重新產生三點\n");
            double Perimeter=tri.Perimeter();
            txt_Display.AppendText(DisplayStringPerimeter(Perimeter));

            double Area = tri.Area();
            txt_Display.AppendText(DisplayStringArea(Area));


            double radius = tri.RadiusOfCircumcircle();
            txt_Display.AppendText(DisplayStringradius(radius));

        }

        private string DisplayStringValue(double Value)
        {
            string StrDisplay = "";
            StrDisplay += Value;
            StrDisplay += "\n";
            return StrDisplay;
        }
        private string DisplayStringPerimeter(double Perimeter) 
        {
            string StrDisplay = "";
            StrDisplay += "周長";
            StrDisplay += Perimeter;
            StrDisplay += "\n";
            return StrDisplay;
        }
        private string DisplayStringArea(double Area)
        {
            string StrDisplay = "";
            StrDisplay += "面積";
            StrDisplay += Area;
            StrDisplay += "\n";
            return StrDisplay;
        }
        private string DisplayStringradius(double radius)
        {
            string StrDisplay = "";
            StrDisplay += "外接圓半徑";
            StrDisplay += radius;
            StrDisplay += "\n";
            return StrDisplay;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            board.rows = int.Parse(txt_rows.Text);
            board.cols = int.Parse(txt_cols.Text);
            board.rowInterval = double.Parse(txt_rowInterval.Text);
            board.colInterval = double.Parse(txt_colInterval.Text);
            Triangle tri = new Triangle();   //建立三角形
            Random rand = new Random();      //產生亂數
            board.CreatePins();

            int[] row = new int[3];
            int[] col = new int[3];
            for(int i=0;i<=2;i++)
            {
                row[i] = rand.Next(board.rows); 
                col[i] = rand.Next(board.cols);
                tri.pointArray[i] = board.pinArray[row[i], col[i]];

            }


            txtCol1.Text = col[0].ToString();
            txtRow1.Text = row[0].ToString();
            txtCol2.Text = col[1].ToString();
            txtRow2.Text = row[1].ToString();
            txtCol3.Text = col[2].ToString();
            txtRow3.Text = row[2].ToString(); 

            board.SetPinsPosition();
            txtX1.Text=tri.pointArray[0].xCoordinate.ToString();
            txtY1.Text=tri.pointArray[0].yCoordinate.ToString();
            txtX2.Text=tri.pointArray[1].xCoordinate.ToString();
            txtY2.Text=tri.pointArray[1].yCoordinate.ToString();
            txtX3.Text=tri.pointArray[2].xCoordinate.ToString();
            txtY3.Text=tri.pointArray[2].yCoordinate.ToString();
            if (!tri.ConditionsOfTriangle())
                txt_Display.AppendText("以上三點無法構成三角形,重新產生三點\n");
            


        }





 



    }
}

Point

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

namespace Quiz4
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;

        public double DistanceBetweenTwoPoints(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - this.xCoordinate), 2) + Math.Pow((pin.yCoordinate - this.yCoordinate), 2));
            return Distance;
        }
    }
}


PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class PinBoard
    {
        public int rows = 0;
        public int cols = 0;
        public Double rowInterval = 0;
        public Double colInterval = 0;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置
        {
            pinArray = new Point[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    pinArray[i, j] = new Point();

                }
            }
        }

        public void SetPinsPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    pinArray[i, j].yCoordinate = (i * rowInterval);
                    pinArray[i, j].xCoordinate = (j * colInterval);
                }
            }
        }
    }
}

Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class Triangle
    {
        public Point[] pointArray = new Point[3]; //建立三角形的三個點

        public bool ConditionsOfTriangle()//判斷是否符合構成三角形的條件
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
            double temp = 0;


            while (true)
            {
                if (lengthB > lengthA) //將lengthA變為最長邊
                {
                    temp = lengthB;
                    lengthB = lengthA;
                    lengthA = temp;
                }
                else if (lengthC > lengthB)//lengthC變為最短邊
                {
                    temp = lengthC;
                    lengthC = lengthB;
                    lengthB = temp;
                }
                else
                {
                    break;
                }
            }
            if (lengthA < (lengthB + lengthC)) //三角形兩邊和大於第三邊
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public double Perimeter() //計算三角形的周長
        {
            //由DistanceTo()計算兩點間的距離算出三角形三邊之邊長
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            return Perimeter;
        }

        public double Area()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            //以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - lengthA) * (0.5 * Perimeter - lengthB) * (0.5 * Perimeter - lengthC));
            return Area;
        }

               public double RadiusOfCircumcircle()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            //由餘弦公式計算出角A之餘弦值
            double cosA = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC);
            double sinA = Math.Sqrt(1 - cosA * cosA);
            //求得外接圓半徑
            double radius = 0.5 * lengthA / sinA; 
            return radius;
        }
    }
}

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quiz4
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new pinboard());
        }
    }
}

2015年4月9日 星期四

[2015][Quiz][Week07]Quiz04-60373006H

namespace Quiz_4_9
{
    public class Point//Point 類別
    {
        public double PointX;
        public double PointY;

        //計算輸入點與成員變數的距離
        public double DistanceTo(Point Input)
        {
            return Math.Sqrt((Input.PointX - PointX) * (Input.PointX - PointX) + (Input.PointY - PointY) * (Input.PointY - PointY));
        }
    }
}
 namespace Quiz_4_9
{
    class PinBoard //釘版類別
    {

        public int Row;//列
        public int Column;//行
        public double RowInterval;
        public double ColumnInterval;
        public Point[,] PinArray;

      
        public void CreatPinArray()
        {
            PinArray = new Point[Row, Column];
            for (int i = 0; i < Row; i++)
            {
                for (int j = 0; j < Column; j++)
                    PinArray[i, j] = new Point();
            }
        }
        public void SetPointPostion()
        {
            for (int i = 0; i < Row; )
            {
                for (double ValueRow = 0; i < Row; i++, ValueRow += RowInterval)
                {
                    for (int j = 0; j < Column; )
                    {
                        for (double ValueCol = 0; j < Column; j++, ValueCol += ColumnInterval)
                        {
                            PinArray[i, j].PointX = ValueRow;
                            PinArray[i, j].PointY = ValueCol;
                        }
                    }
                }
            }
        }
    }
}
namespace Quiz_4_9
{
    class Triangle //三角形類別
    {
        //成員三點
        public Point[] PointofTriangle = new Point[3];

        public bool IsRight()
        {
            double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
            double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
            double s3 = PointofTriangle[0].DistanceTo(PointofTriangle[2]);
            if ((s1 + s2 > s3) && (s2 + s3 > s1) && (s1 + s3 > s2))
                return true;
            else
                return false;
        }//判斷三角形是否合法
        public double Preimeter()
        {
            double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
            double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
            double s3 = PointofTriangle[0].DistanceTo(PointofTriangle[2]);
            return (s1 + s2 + s3);
        }//周長
        public double Area()
        {
            double s1 = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
            double s2 = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
            double s3 = PointofTriangle[2].DistanceTo(PointofTriangle[0]);
            double s = (s1 + s2 + s3) / 2;
            return Math.Sqrt(s * (s - s1) * (s - s2) * (s - s3));
        }//面積
        public double RadiusOfCircumcircle()
        {
            double a = PointofTriangle[0].DistanceTo(PointofTriangle[1]);
            double b = PointofTriangle[1].DistanceTo(PointofTriangle[2]);
            double c = PointofTriangle[2].DistanceTo(PointofTriangle[0]);
            double cosAlpha = (b * b + c * c - a * a) / (2 * b * c);
            double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha);
            return (0.5 * a / sinAlpha);
        }//外接圓半徑
    }
}
namespace Quiz_4_9
{
    public partial class Form1 : Form
    {
        private PinBoard board;
        Triangle triangle;
        Random rand;

        public Form1()
        {
            InitializeComponent();
            board = new PinBoard();
            triangle = new Triangle();
            rand = new Random();
        }

        //btn_GenerateTriangle功能創造PinArray
        private void btn_GenerateTriangle_Click(object sender, EventArgs e)
        {
            if (txt_RowKeyin.Text != "" && txt_ColumnKeyin.Text != "" && txt_RowIntervalKeyin.Text != "" && txt_ColumnIntervalKeyin.Text != "")
            {
                //讀取txt
                board.Row = Convert.ToInt16(txt_RowKeyin.Text);
                board.Column = Convert.ToInt16(txt_ColumnKeyin.Text);
                board.RowInterval = Convert.ToDouble(txt_RowIntervalKeyin.Text);
                board.ColumnInterval = Convert.ToDouble(txt_ColumnIntervalKeyin.Text);
                //創造PinBoard
                board.CreatPinArray();
                board.SetPointPostion();
                PrintPoint();
            }
            else
                MessageBox.Show("缺參數"); 
        }

        //亂數選擇三點作為三角形,並將三點塞進Triangle類別中,同時print出來
        private void PrintPoint()
        {
            //亂數選點 && 塞進txt
            txt_Point1Row.Text = rand.Next(board.Row).ToString();
            txt_Point1Column.Text = rand.Next(board.Column).ToString();
            txt_Point2Row.Text = rand.Next(board.Row).ToString();
            txt_Point2Column.Text = rand.Next(board.Column).ToString();
            txt_Point3Row.Text = rand.Next(board.Row).ToString();
            txt_Point3Column.Text = rand.Next(board.Column).ToString();
            //用選點結果,提出PinBoard點,並塞進Triangle類別
            triangle.PointofTriangle[0] = board.PinArray[Convert.ToInt16(txt_Point1Row.Text), Convert.ToInt16(txt_Point1Column.Text)];
            triangle.PointofTriangle[1] = board.PinArray[Convert.ToInt16(txt_Point2Row.Text), Convert.ToInt16(txt_Point2Column.Text)];
            triangle.PointofTriangle[2] = board.PinArray[Convert.ToInt16(txt_Point3Row.Text), Convert.ToInt16(txt_Point3Column.Text)];
            //顯示三角形的三點
            txt_Point1X.Text = triangle.PointofTriangle[0].PointX.ToString();
            txt_Point1Y.Text = triangle.PointofTriangle[0].PointY.ToString();
            txt_Point2X.Text = triangle.PointofTriangle[1].PointX.ToString();
            txt_Point2Y.Text = triangle.PointofTriangle[1].PointY.ToString();
            txt_Point3X.Text = triangle.PointofTriangle[2].PointX.ToString();
            txt_Point3Y.Text = triangle.PointofTriangle[2].PointY.ToString();
        }

        //將Triangle類別中的數字提出來做運算,並放進txtbox裡。
        private void btn_Calulate_Click(object sender, EventArgs e)
        {
            //判斷三點是否為空
            if (txt_Point1X.Text != "" && txt_Point1Y.Text != "" && txt_Point2X.Text != "" &&
                txt_Point2Y.Text != "" && txt_Point3X.Text != "" && txt_Point3Y.Text != "")
            {//判斷手動輸入數字是否超過板子大小
                if (Convert.ToInt16(txt_Point1Row.Text) < board.Row && Convert.ToInt16(txt_Point1Column.Text) < board.Column &&
                    Convert.ToInt16(txt_Point2Row.Text) < board.Row && Convert.ToInt16(txt_Point2Column.Text) < board.Column &&
                    Convert.ToInt16(txt_Point3Row.Text) < board.Row && Convert.ToInt16(txt_Point3Column.Text) < board.Column)
                {
                    txt_ResultPrint.Text = "";
                    //為手動輸入所以必須再寫一次
                    triangle.PointofTriangle[0] = board.PinArray[Convert.ToInt16(txt_Point1Row.Text), Convert.ToInt16(txt_Point1Column.Text)];
                    triangle.PointofTriangle[1] = board.PinArray[Convert.ToInt16(txt_Point2Row.Text), Convert.ToInt16(txt_Point2Column.Text)];
                    triangle.PointofTriangle[2] = board.PinArray[Convert.ToInt16(txt_Point3Row.Text), Convert.ToInt16(txt_Point3Column.Text)];
                    if (triangle.IsRight() == true)
                    {
                        txt_ResultPrint.Text += "周長:";
                        txt_ResultPrint.Text += triangle.Preimeter().ToString() + Environment.NewLine;
                        txt_ResultPrint.Text += "面積:";
                        txt_ResultPrint.Text += triangle.Area().ToString() + Environment.NewLine;
                        txt_ResultPrint.Text += "外接圓半徑:";
                        txt_ResultPrint.Text += triangle.RadiusOfCircumcircle().ToString() + Environment.NewLine;
                    }
                    else
                        txt_ResultPrint.Text = "the triangle is illegal";
                }
                else
                    MessageBox.Show("輸入數字超過板子大小");
            }
            else
                MessageBox.Show("缺參數");
        }

    }
}

[2015][Homework]Team03 - Hw03

namespace practice
{
    class Test
    {
        static void KeyInPara(MyDateTime date)
        {
            int year;
            int month;
            int day;
            do
            {
                Console.WriteLine("輸入一個西元年份");
                year = Convert.ToInt16(Console.ReadLine());
                if (year > 0)
                    date.Year = year;
                else
                    Console.WriteLine("重新輸入");
            } while (date.Year < 0);
            do
            {
                Console.WriteLine("輸入一個西元月份");
                month = Convert.ToInt16(Console.ReadLine());
                if (month > 0)
                    date.Month = month;
                else
                    Console.WriteLine("重新輸入");
            } while (date.Month < 0);
            do
            {
                Console.WriteLine("輸入一個西元日期");
                day = Convert.ToInt16(Console.ReadLine());
                if (month > 0)
                    date.Day = day;
                else
                    Console.WriteLine("\n重新輸入");
            } while (date.Day < 0);
        }
        static void DetermineWeeks(MyDateTime date,int year, int month, int day)
        {
            Console.WriteLine("{0}/{1}/{2} : ", year,month,day);
            switch(date.DetermineWeek(year,month,day))
            {
                case 0:
                    Console.WriteLine("星期日");
                    break;
                case 1:
                    Console.WriteLine("星期一");
                    break;
                case 2:
                    Console.WriteLine("星期二");
                    break;
                case 3:
                    Console.WriteLine("星期三");
                    break;
                case 4:
                    Console.WriteLine("星期四");
                    break;
                case 5:
                    Console.WriteLine("星期五");
                    break;
                case 6:
                    Console.WriteLine("星期六");
                    break;
            }
        }
        static void Main(string[] args)
        {
            MyDateTime date = new MyDateTime();
            string iscontinue="y";
            do
            {
                KeyInPara(date);
                Console.WriteLine("====================我是分隔線====================");
                Console.WriteLine("{0}/{1}/{2}", date.Year, date.Month, date.Day);
                if (date.DetermineLeapYear(date.Year) == true)
                {
                    Console.WriteLine("{0}是閏年", date.Year);
                    Console.WriteLine("{0}年有366天", date.Year);
                }
                else
                {
                    Console.WriteLine("{0}是平年", date.Year);
                    Console.WriteLine("{0}年有365天", date.Year);
                }
                Console.WriteLine("{0}/{1}月 有{2}天", date.Year, date.Month, date.CountDayofMonth(date.Year, date.Month));
                DetermineWeeks(date, date.Year, date.Month, date.Day);
                Console.WriteLine("====================我是分隔線====================");
                Console.WriteLine("\n是否繼續(y)");
                iscontinue = Console.ReadLine();
            } while (iscontinue == "y");
            Console.WriteLine("按任意鍵結束");
            Console.ReadKey();
        }
    }
}
namespace practice
{
    class MyDateTime
    {
        public int Year;
        public int Month;
        public int Day;

        public bool DetermineLeapYear(int year)
        {
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //可被4整除 && 不可被100整除 ||可被400整除
                return true;
            return false;
        }
        public int CountDayofMonth(int year, int month)
        {
            if ((1 <= month) && (month <= 12) && month != 2)
            {
                if (month <= 7)
                {
                    if ((month % 2) == 1)
                        return 31;
                    else
                        return 30;
                }
                else
                {
                    if ((month % 2) == 1)
                        return 30;
                    else
                        return 31;
                }
            }
            else
                if (DetermineLeapYear(year) == true)
                    return 29;
                else
                    return 28;
        }
        public int DetermineWeek(int year, int month, int day)
        {
            double frontofyears, behindofyear;
            double week;
            frontofyears = year / 100;
            behindofyear = year % 100;
            if (month == 1 || month == 2)
            {
                if (month == 1)
                {
                    month = 13;
                    year--;
                }
                else
                {
                    month = 14;
                    year--;
                }
            }
            week = behindofyear + Math.Floor(behindofyear / 4) + Math.Floor(frontofyears / 4) - 2 * frontofyears +
                Math.Floor((26 * ((double)month + 1)) / 10) + day - 1;
            if (week < 0)
                week = (week % 7 + 7) % 7;
            else
                week %= 7;

            if (month == 13)
            {
                month = 1;
                year++;
            }
            else 
            {
                month = 2;
                year++;
            }
            return (int)week;
        }
        public bool DetermineDayIslegal(int year,int month,int day)
        {
            if (year < 0)
                return false;
            else
                if (month < 1 || month > 12)
                    return false;
                else
                    if (day > CountDayofMonth(year, month))
                        return false;
                    else
                        return true;
        }
        public int CountDayBetweenOther(int year, int month, int day, int year2, int month2, int day2)
        {
            int sumOfDay = 0;
            for (int i = year2; year < i; i--)
            {
                if (DetermineLeapYear(year++) == true)
                    sumOfDay += 366;
                else
                    sumOfDay += 365;
            }
            for (int i = month2 - 1; i >= 1; i--)
                sumOfDay += CountDayofMonth(year2, i);
            for (int i = month - 1; i >= 1; i--)
                sumOfDay -= CountDayofMonth(year2, i);
            return sumOfDay + day2 - day - 1;
            
        }
    }
}

[2015][Quiz][Week07]Quiz04-R02945022

Program為主程式
Form1為視窗程式,包含輸入與輸出
Point為類別,計算兩點距離
PinBoard為類別,產生格點與格點座標
Triangle為類別,產生triangle三點座標,計算周長、面積與外接圓半徑

[2015][Quiz][Week07] Quiz4 - 40173027H

程式分為五個部分
Form1為視窗程式
Program為主程式
Point為類別 點的x.y座標和到另一點的距離
PinBoard為類別 板子的大小.間距.創造點和設定點的位置
Triangle為類別 三個大頭針的位置.計算是否構成三角形.其周長.面積.外接圓半徑

Form1
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 Quiz4
{
    public partial class Form1 : Form
    {
        PinBoard board = new PinBoard(); //建立板子

        public Form1()
        {  
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnTriangle_Click(object sender, EventArgs e)
        {
            Triangle tri = new Triangle();   //建立三角形
            Random rand = new Random();      //產生亂數

            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColumn.Text);
            board.rowInterval = double.Parse(txtXInterval.Text);
            board.columnInterval = double.Parse(txtYInterval.Text);

            board.CreatePins();
            board.SetPinsPosition();

            for (int i = 0; i < 3; i++)
            {
                tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)];         //以亂數產生三角形的三個點
            }
            txtP1Row.Text = (Convert.ToString(tri.pointArray[0].xCoordinate / board.rowInterval));
            txtP1Col.Text = (Convert.ToString(tri.pointArray[0].yCoordinate / board.columnInterval));
            txtP1XInt.Text = (Convert.ToString(tri.pointArray[0].xCoordinate));
            txtP1YInt.Text = (Convert.ToString(tri.pointArray[0].yCoordinate));
            txtP2Row.Text = (Convert.ToString(tri.pointArray[1].xCoordinate / board.rowInterval));
            txtP2Col.Text = (Convert.ToString(tri.pointArray[1].yCoordinate / board.columnInterval));
            txtP2XInt.Text = (Convert.ToString(tri.pointArray[1].xCoordinate));
            txtP2YInt.Text = (Convert.ToString(tri.pointArray[1].yCoordinate));
            txtP3Row.Text = (Convert.ToString(tri.pointArray[2].xCoordinate / board.rowInterval));
            txtP3Col.Text = (Convert.ToString(tri.pointArray[2].yCoordinate / board.columnInterval));
            txtP3XInt.Text = (Convert.ToString(tri.pointArray[2].xCoordinate));
            txtP3YInt.Text = (Convert.ToString(tri.pointArray[2].yCoordinate));
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            Triangle tri = new Triangle();   //建立三角形

            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColumn.Text);
            board.rowInterval = double.Parse(txtXInterval.Text);
            board.columnInterval = double.Parse(txtYInterval.Text);

            board.CreatePins();
            board.SetPinsPosition();

            for (int i = 0; i < 3; i++)
            {
                tri.pointArray[i] = new Point(); 
            }

            tri.pointArray[0].xCoordinate = (int.Parse(txtP1Row.Text)) * board.rowInterval;
            tri.pointArray[0].yCoordinate = (int.Parse(txtP1Col.Text)) * board.columnInterval;
            tri.pointArray[1].xCoordinate = (int.Parse(txtP2Row.Text)) * board.rowInterval;
            tri.pointArray[1].yCoordinate = (int.Parse(txtP2Col.Text)) * board.columnInterval;
            tri.pointArray[2].xCoordinate = (int.Parse(txtP3Row.Text)) * board.rowInterval;
            tri.pointArray[2].yCoordinate = (int.Parse(txtP3Col.Text)) * board.columnInterval;
            txtP1XInt.Text = (Convert.ToString(tri.pointArray[0].xCoordinate));
            txtP1YInt.Text = (Convert.ToString(tri.pointArray[0].yCoordinate));
            txtP2XInt.Text = (Convert.ToString(tri.pointArray[1].xCoordinate));
            txtP2YInt.Text = (Convert.ToString(tri.pointArray[1].yCoordinate));
            txtP3XInt.Text = (Convert.ToString(tri.pointArray[2].xCoordinate));
            txtP3YInt.Text = (Convert.ToString(tri.pointArray[2].yCoordinate));

            if (tri.IsRight())
            {
                txtPrint.Text = null;
                txtPrint.AppendText("三角形的周長是:" + Convert.ToString(tri.Perimeter()) + "\n");
                txtPrint.AppendText("三角形的面積是:" + Convert.ToString(tri.Area()) + "\n");
                txtPrint.AppendText("三角形的外接圓半徑是:" + Convert.ToString(tri.RadiusOfCircumcircle()) + "\n");
            }
            else 
            {
                txtPrint.Text = null;
                txtPrint.AppendText("產生的三點不構成三角形!\n");
            }
        }

        private void lblTriXInterval_Click(object sender, EventArgs e)
        {

        }
    }
}

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quiz4
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Point
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;

        public double DistanceTo(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
            return Distance;
        }
    }
}

PinBoard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class PinBoard
    {
        public int rows = 10;
        public int columns = 10;
        public Double rowInterval = 10;
        public Double columnInterval = 10;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置
        {
            pinArray = new Point[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j] = new Point();
                }
            }
        }

        public void SetPinsPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * columnInterval);
                }
            }
        }
    }
}

Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class Triangle
    {
        public Point[] pointArray = new Point[3]; //建立三角形的三個點

        public double Perimeter() //計算三角形的周長
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);

            double Perimeter = length1 + length2 + length3; //三邊和為周長
            return Perimeter;
        }

        public double Area()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);

            double Perimeter = length1 + length2 + length3; //已求得三邊長度以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - length1) * (0.5 * Perimeter - length2) * (0.5 * Perimeter - length3));
            return Area;
        }

        public bool IsRight()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double temp = 0;
            double copyLength1 = length1; //複製三邊邊長以便依大小排序
            double copyLength2 = length2;
            double copyLength3 = length3;

            while (true)
            {
                if (copyLength2 > copyLength1) //將copyLength1變為最長邊
                {
                    temp = copyLength2;
                    copyLength2 = copyLength1;
                    copyLength1 = temp;
                }
                else if (copyLength3 > copyLength2)
                {
                    temp = copyLength3;
                    copyLength3 = copyLength2;
                    copyLength2 = temp;
                }
                else
                {
                    break;
                }
            }
            if (copyLength1 < (copyLength2 + copyLength3)) //三角形兩邊和須大於第三邊
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public double RadiusOfCircumcircle()
        {
            double lengthA = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double lengthB = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double lengthC = pointArray[2].DistanceTo(pointArray[0]);

            double cosAlpha = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC); //由餘弦公式計算出Alpha角之餘弦值
            double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha); //由萬用公式計算出Alpha角之正弦值
            double radius = 0.5 * lengthA / sinAlpha; //求得外接圓半徑

            return radius;
        }
    }
}

[2015][Quiz][Week07] Quiz4 - 40173041H

程式分為 1.建立點型態 2.建立板子 3.建立三角形 4.視窗 5.主程式 點型態
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;

        public double DistanceTo(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
            return Distance;
        }
    }
}
板子
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class PinBoard
    {
        public int rows;
        public int columns;
        public Double rowInterval;
        public Double columnInterval;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置
        {
            pinArray = new Point[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j] = new Point();
                }
            }
        }

        public void SetPinsPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * columnInterval);
                }
            }
        }
    }
}

三角形
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quiz4
{
    class TriangleFunction
    {
        public Point[] pointArray = new Point[3]; //建立三角形的三個點

        public double Perimeter() //計算三角形的周長
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);

            double Perimeter = length1 + length2 + length3; //三邊和為周長
            return Perimeter;
        }

        public double Area()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);

            double Perimeter = length1 + length2 + length3; //已求得三邊長度以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - length1) * (0.5 * Perimeter - length2) * (0.5 * Perimeter - length3));
            return Area;
        }

        public bool IsRight()
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double length2 = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double temp = 0;
            double copyLength1 = length1; //複製三邊邊長以便依大小排序
            double copyLength2 = length2;
            double copyLength3 = length3;

            while (true)
            {
                if (copyLength2 > copyLength1) //將copyLength1變為最長邊
                {
                    temp = copyLength2;
                    copyLength2 = copyLength1;
                    copyLength1 = temp;
                }
                else if (copyLength3 > copyLength2)
                {
                    temp = copyLength3;
                    copyLength3 = copyLength2;
                    copyLength2 = temp;
                }
                else
                {
                    break;
                }
            }
            if (copyLength1 < (copyLength2 + copyLength3)) //三角形兩邊和須大於第三邊
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public double RadiusOfCircumcircle()
        {
            double lengthA = pointArray[0].DistanceTo(pointArray[1]); //由DistanceTo()計算兩點間的距離
            double lengthB = pointArray[1].DistanceTo(pointArray[2]); //算出三角形三邊之邊長
            double lengthC = pointArray[2].DistanceTo(pointArray[0]);

            double cosAlpha = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC); //由餘弦公式計算出Alpha角之餘弦值
            double sinAlpha = Math.Sqrt(1 - cosAlpha * cosAlpha); //由萬用公式計算出Alpha角之正弦值
            double radius = 0.5 * lengthA / sinAlpha; //求得外接圓半徑

            return radius;
        }
    }
}

視窗
using System;
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 Quiz4
{
    public partial class Form1 : Form
    {
        PinBoard board = new PinBoard();
        public Form1()
        {
            InitializeComponent();
        }
        private void btnGenerateTriengle_Click(object sender, EventArgs e)
        {

            TriangleFunction tri = new TriangleFunction();
            Random rand = new Random();
            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColumn.Text);
            board.rowInterval = Double.Parse(txtXInterval.Text);
            board.columnInterval = Double.Parse(txtYInterval.Text);
            int[] rowRand = new int[3];
            int[] columnRand = new int[3];
            board.CreatePins();
            board.SetPinsPosition();
        TrianglePin:
            for (int i = 0; i < 3; i++)
            {
                rowRand[i] = rand.Next(board.rows);
                columnRand[i] = rand.Next(board.columns);
                tri.pointArray[i] = board.pinArray[rowRand[i], columnRand[i]];
            }
            if (!tri.IsRight())
            {
                goto TrianglePin;
            }

            txtPoint1Row.Text = rowRand[0].ToString();
            txtPoint1Column.Text = columnRand[0].ToString();
            txtPoint2Row.Text = rowRand[1].ToString();
            txtPoint2Column.Text = columnRand[1].ToString();
            txtPoint3Row.Text = rowRand[2].ToString();
            txtPoint3Column.Text = columnRand[2].ToString();
            txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
            txtTriangleSolution.Text += Environment.NewLine;
            txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
            txtTriangleSolution.Text += Environment.NewLine;
            txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
            txtTriangleSolution.Text += Environment.NewLine;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            TriangleFunction tri = new TriangleFunction();
            for (int i = 0; i < 3;i++ )
            {
                tri.pointArray[i]=new Point();
            }
            board.rows = int.Parse(txtRow.Text);
            board.columns = int.Parse(txtColumn.Text);
            board.rowInterval = Double.Parse(txtXInterval.Text);
            board.columnInterval = Double.Parse(txtYInterval.Text);
            txtPoint1X.Text = (Double.Parse(txtPoint1Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint1Y.Text = (Double.Parse(txtPoint1Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            txtPoint2X.Text = (Double.Parse(txtPoint2Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint2Y.Text = (Double.Parse(txtPoint2Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            txtPoint3X.Text = (Double.Parse(txtPoint3Column.Text) * Double.Parse(txtXInterval.Text)).ToString();
            txtPoint3Y.Text = (Double.Parse(txtPoint3Row.Text) * Double.Parse(txtYInterval.Text)).ToString();
            tri.pointArray[0].xCoordinate = double.Parse(txtPoint1X.Text);
            tri.pointArray[0].yCoordinate = double.Parse(txtPoint1Y.Text);
            tri.pointArray[1].xCoordinate = double.Parse(txtPoint2X.Text);
            tri.pointArray[1].yCoordinate = double.Parse(txtPoint2Y.Text);
            tri.pointArray[2].xCoordinate = double.Parse(txtPoint3X.Text);
            tri.pointArray[2].yCoordinate = double.Parse(txtPoint3Y.Text);
            if (!tri.IsRight())
            {
                txtTriangleSolution.Text = "不是三角形";
            }
            else
            {
                txtTriangleSolution.Text = "周長:" + tri.Perimeter().ToString();
                txtTriangleSolution.Text += Environment.NewLine;
                txtTriangleSolution.Text += "面積:" + tri.Area().ToString();
                txtTriangleSolution.Text += Environment.NewLine;
                txtTriangleSolution.Text += "外接圓:" + tri.RadiusOfCircumcircle().ToString();
                txtTriangleSolution.Text += Environment.NewLine;
            }
        }

    }
}

主程式
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quiz4
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

[2015][Homework]Team03 - Hw05

Program為進入程式點,輸入與輸出 Point為類別,計算兩點距離 PinBoard為類別,產生PinBoard格點座標 Triangle為類別,產生triangle並計算周長、面積、外接圓半徑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Board
{
    class Program
    {
        static void Main(string[] args)
        {
            PinBoard board = new PinBoard(); //建立板子
            Triangle tri = new Triangle();   //建立三角形
            Random rand = new Random();      //產生亂數
 
        Columns:
            Console.WriteLine("請輸入板子有幾行:");
            board.columns = int.Parse(Console.ReadLine());
            if (board.columns < 1)
            {
                Console.WriteLine("行數不得小於1");
                goto Columns;
            }
 
        Rows:
            Console.WriteLine("請輸入板子有幾列:");
            board.rows = int.Parse(Console.ReadLine());
            if (board.rows < 1)
            {
                Console.WriteLine("列數不得小於1");
                goto Rows;
            }
 
        RowInterval: 
            Console.WriteLine("請輸入板子行之間的距離:");
            board.rowInterval = Double.Parse(Console.ReadLine());
            if (board.rowInterval < 0)
            {
                Console.WriteLine("距離不得小於0");
                goto RowInterval;
            }
 
            Console.WriteLine("請輸入板子列之間的距離:");
 
        ColumnInterval:
            board.columnInterval = Double.Parse(Console.ReadLine());
            if (board.columnInterval < 0)
            {
                Console.WriteLine("距離不得小於0");
                goto ColumnInterval;
            }
 
            board.CreatePins();
            board.SetPinsPosition();
 
        TrianglePin:
            Console.WriteLine("三角形的3個點是:");
            for (int i = 0; i < 3; i++)
            {
                tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.columns)];         //以亂數產生三角形的三個點
                Console.WriteLine("{0},{1}", tri.pointArray[i].xCoordinate, tri.pointArray[i].yCoordinate);
            }
            if (!tri.IsRight())
            {
                Console.WriteLine("產生的三點不構成三角形! 再試一次!");
                goto TrianglePin;
            }
            Console.WriteLine("\n三角形的周長是:{0}",tri.Perimeter());
            Console.WriteLine("三角形的面積是:{0}", tri.Area());
            Console.WriteLine("三角形的外接圓半徑是:{0}", tri.RadiusOfCircumcircle());
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Board
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;
 
        public double DistanceTo(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - xCoordinate), 2) + Math.Pow((pin.yCoordinate - yCoordinate), 2));
            return Distance;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Board
{
    class PinBoard
    {
        public int rows = 10;
        public int columns = 10;
        public Double rowInterval = 10;
        public Double columnInterval = 10;
        public Point[,] pinArray = null;
 
        public void CreatePins() //產生矩陣各點之位置
        {
            pinArray = new Point[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j] = new Point();
                }
            }
        }
 
        public void SetPinsPosition() //依行列間距計算出各點x.y之座標
        {
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * columnInterval);
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Board
{
    class Triangle
    {
        public Point[] pointArray = new Point[3];
 
        public double Perimeter()      //計算周長
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            return (length1 + length2 + length3);
        }
 
        public double Area()      //計算面積
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double length = (length1 + length2 + length3) / 2;
            return Math.Sqrt(length * (length - length1) * (length - length2) * (length - length3));
        }
 
        public double RadiusOfCircumcircle()      //計算外接圓半徑
        {
            double length1 = pointArray[0].DistanceTo(pointArray[1]);
            double length2 = pointArray[1].DistanceTo(pointArray[2]);
            double length3 = pointArray[2].DistanceTo(pointArray[0]);
            double cosAngle = (length2 * length2 + length3 * length3 - length1 * length1) / (2 * length2 * length3);
            double sinAngle = Math.Sqrt(1 - cosAngle * cosAngle);
            return 0.5 * length1 / sinAngle;
        }
    }
}

[2015][Homework]Team05 - Hw06

[2015][Homework]Team05-Hw05

read more

[2015][Homework]Team02 - Hw06

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        CardDeck Poker = new CardDeck();
        public Form1()
        {
            InitializeComponent();
        }

        private void message_show()
        {
            string display = "";
            for (int i = 0; i < 52; i++)
            {
                display += Poker.Cards[i].SuitToUniCode() + Poker.Cards[i].RankToStr() + "  ";
                if (((i + 1) % 13) == 0)
                {
                    display += "\n";
                }
            }
            MessageBox.Show(display);
        }

        private void btnCboxTest_Click(object sender, EventArgs e)
        {
            Poker.CreateDeck();
            Poker.ResetDeck();

            switch(cbox_Select.SelectedIndex)
            {
                case 0:
                    txt_Display.AppendText("選擇了按花色排列" + "\n");
                    Poker.Reset();
                    message_show();
                    break;
                case 1:
                    txt_Display.AppendText("選擇了亂數洗牌" + "\n");
                    Poker.Shuffle();
                    message_show();
                    break;
                case 2:
                    txt_Display.AppendText("選擇了按點數大小排列" + "\n");
                    Poker.ResetDeck();
                    message_show();
                    break;
                default:
                    MessageBox.Show("請選擇!");
                    break;
            }
        }

        private void btn_ChkboxTest_Click(object sender, EventArgs e)
        {
            if (chkbox_Select1.Checked)
            {
                txt_Display.AppendText("選擇了按花色排列" + "\n");
                Poker.ResetDeck();
                message_show();
            }
            if(chkbox_Select2.Checked)
            {
                txt_Display.AppendText("選擇了亂數洗牌" + "\n");
                Poker.Shuffle();
                message_show();
            }
            if(chkbox_Select3.Checked)
            {
                txt_Display.AppendText("選擇了按點數大小排列" + "\n");
                Poker.Reset();
                message_show();
            }

        }

    }
}

class CardDeck


namespace WindowsFormsApplication2
{
    class CardDeck
    {
        //產生52張牌
        public PokerCard[] Cards = new PokerCard[52];
        public void CreateDeck()
        {
            for (int i = 0; i < 52; i++)
                Cards[i] = new PokerCard();
        }
        
        //將撲克排回復初始照點數大小排列
        public void ResetDeck()
        {
            for (int i = 0; i <52; i++)
            {
                Cards[i].Rank = i % 13 + 1;
                Cards[i].Suit = i / 13 + 3;
            }
        }

        //洗牌以作亂數排列
        public void Shuffle()
        {
            PokerCard Change = new PokerCard();
            for (int i = 0; i <52; i++)
            {
                Random rand = new Random();
                Change = Cards[i];
                int another = rand.Next(52);
                Cards[i] = Cards[another];
                Cards[another] = Change;
            }
        }

        //將撲克牌依照花色排列
        public void Reset()
        {
            for (int i = 0; i <52; i++)
            {
                Cards[i].Rank = (i / 4) + 1;
                Cards[i].Suit = (i % 4) + 3;
            }
        }
    }
}

Class PokerCard
namespace WindowsFormsApplication2
{
    class PokerCard
    {
        public int Suit;
        public int Rank;
        public string RankToStr()
        {
            switch (Rank)
            {
                case 1:
                    return "A";
                case 11:
                    return "J";
                case 12:
                    return "Q";
                case 13:
                    return "K";
                default:
                    return Convert.ToString(Rank);
            }
        }
        public string SuitToUniCode()
        {
            string Str_Suit;
            switch (Suit)
            {
                case 3:
                    Str_Suit = "\u2665";
                    break;
                case 4:
                    Str_Suit = "\u2666";
                    break;
                case 5:
                    Str_Suit = "\u2663";
                    break;
                default:
                    Str_Suit = "\u2660";
                    break;
            }
            return Str_Suit;
        }
    }
}

2015年4月8日 星期三

[2015][Homework]Team02 - Hw04

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

namespace Hw2
{
    class Program
    {
        static bool HasSolution(int a,int b,int c)
        {
            int GCD;
            //演算步驟一     計算 a, b 的最大公因數 
            int aCopy = a;
            int bCopy = b;
            int temp;
            if (a > b)
            {
                while (aCopy % bCopy != 0)
                {
                    temp = aCopy % bCopy;
                    aCopy = bCopy;
                    bCopy = temp;
                }
                GCD = bCopy;
            }
            else
            {
                while (bCopy % aCopy != 0)
                {
                    temp = bCopy % aCopy;
                    bCopy = aCopy;
                    aCopy = temp;
                }
                GCD = aCopy;
            }
            //演算步驟二     設(a, b) = k, 若 c 不為 k 的倍數, 則無解。 
            if (c % GCD != 0)
            {
                return false;
            }
            else
                return true;
            
        }
        static void Main(string[] args)
        {
            bool flag=true;
            int temp;
            while (flag)
            {
                Console.WriteLine("第一桶容量=");
                int containerA = int.Parse(Console.ReadLine());
                Console.WriteLine("第二桶容量=");
                int containerB = int.Parse(Console.ReadLine());
                Console.WriteLine("所需水量=");
                int waterRequired = int.Parse(Console.ReadLine());
                Console.WriteLine("A桶水為較小桶水,B桶為較大桶水");
                if (containerA > containerB)
                {
                    temp = containerB;
                    containerB = containerA;
                    containerA = temp;
                    Console.WriteLine("A桶容量{0}公升,B桶容量{1}公升", containerA, containerB);
                }
                else
                {
                    Console.WriteLine("A桶容量{0}公升,B桶容量{1}公升", containerA, containerB);
                }
                if (HasSolution(containerA, containerB, waterRequired) == true)
                {
                    int currentCapacityA = 0;
                    int currentCapacityB = 0;
                    int step = 0;
                    Console.WriteLine("一開始,A桶有水{0}公升,B桶有水{1}公升\n", currentCapacityA, currentCapacityB);
                    while (currentCapacityA + currentCapacityB != waterRequired)
                    {
                        step++;
                        if (containerA + containerB < waterRequired)
                        {
                            Console.WriteLine("被唬了");
                            break;
                        }
                        if (containerA + containerB == waterRequired)
                        {
                            currentCapacityA =containerA;
                            currentCapacityB = containerB;
                            Console.WriteLine("step{0}:兩桶皆滿水\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, currentCapacityA, currentCapacityB);
                        }
                        else if (currentCapacityB == 0)
                        {
                            currentCapacityB = containerB;
                            Console.WriteLine("step{0}:把B桶裝滿水\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, currentCapacityA, currentCapacityB);

                        }
                        else if (currentCapacityA >= containerA)
                        {
                            currentCapacityA = 0;
                            Console.WriteLine("step{0}:A桶水滿,把A桶的水全部倒掉\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, currentCapacityA, currentCapacityB);
                        }
                        else if (currentCapacityA + currentCapacityB >= containerA)
                        {
                            currentCapacityB = currentCapacityA + currentCapacityB - containerA;
                            currentCapacityA = containerA;
                            Console.WriteLine("step{0}:將B桶的水倒滿A桶\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, currentCapacityA, currentCapacityB);
                        }
                        else if (currentCapacityA + currentCapacityB != waterRequired)
                        {
                            currentCapacityA = currentCapacityA + currentCapacityB;
                            currentCapacityB = 0;
                            Console.WriteLine("step{0}:將B桶的水倒滿A桶\n此時,A桶有水{1}公升,B桶有水{2}公升\n", step, currentCapacityA, currentCapacityB);
                        }
                    }
                    if (containerA + containerB >= waterRequired)
                    {
                        Console.WriteLine("最後,A桶有水{0}公升,B桶有水{1}公升\n", currentCapacityA, currentCapacityB);
                        Console.WriteLine("將水{0}公升放在啟動開關上,解除炸彈", currentCapacityA + currentCapacityB);
                    }
                    Console.ReadLine();
                    Console.Clear();
                    while (true)
                    {
                        Console.WriteLine("還有其他犯人嗎?沒有請按0,有按1");
                        int response = int.Parse(Console.ReadLine());
                        if (response == 0)
                        {
                            flag = false;
                            break;
                        }
                        else if (response == 1)
                        {
                            flag = true;
                            break;
                        }
                        else
                            Console.Clear();
                    }
                }
                else
                { 
                    Console.WriteLine("解除炸彈是幌子,蹦!!!!!!!!!!!");
                    break;
                }
                }
            Console.Read();
            }
        }
    }

[2015][Homework]Team01 - Hw06

[2015][Homework]Team01 - Hw05

[2015][Homework]Team05 - Hw04

[2015][Homework]Team02-Hw05

 
Program 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Program
    {
        static void Main(string[] args)
        {
            PinBoard board = new PinBoard(); //建立板子
            Triangle tri = new Triangle();   //建立三角形
            Random rand = new Random();      //產生亂數

            while (true)
            {
                //輸入板子的設定參數
                Console.WriteLine("請輸入板子有幾行:");
                board.cols = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子有幾列:");
                board.rows = int.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子行之間的距離:");
                board.rowInterval = Double.Parse(Console.ReadLine());
                Console.WriteLine("請輸入板子列之間的距離:");
                board.colInterval = Double.Parse(Console.ReadLine());
                //建立板子
                board.CreatePins();

            TrianglePin:
                Console.WriteLine("\n三角形的3個點是:");
                for (int i = 0; i < 3; i++)
                {
                    //以亂數產生三角形的三個點
                    tri.pointArray[i] = board.pinArray[rand.Next(board.rows), rand.Next(board.cols)];
                    Console.WriteLine("{0},{1}", tri.pointArray[i].xCoordinate, tri.pointArray[i].yCoordinate);
                }
                if (!tri.ConditionsOfTriangle())
                {
                    Console.WriteLine("產生的三點不構成三角形,重新產生三個點\n");
                    goto TrianglePin;
                }
                Console.WriteLine("\n三角形的周長是:{0}", tri.Perimeter());
                Console.WriteLine("\n三角形的面積是:{0}", tri.Area());
                Console.WriteLine("\n三角形的外接圓半徑是:{0}", tri.RadiusOfCircumcircle());

                Console.WriteLine("\n是否繼續程式?是請輸入Y,否則輸入N");
                char select = Convert.ToChar(Console.ReadLine());
                if (select != ('y') && select != ('Y'))
                {
                    break;
                }
                Console.Clear();
            }

        }
    }
}

Point 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Point
    {
        public double xCoordinate;
        public double yCoordinate;

        public double DistanceBetweenTwoPoints(Point pin) //計算該點和某點之間的距離
        {
            double Distance = Math.Sqrt(Math.Pow((pin.xCoordinate - this.xCoordinate), 2) + Math.Pow((pin.yCoordinate - this.yCoordinate), 2));
            return Distance;
        }
    }
}

PinBoard  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class PinBoard
    {
        public int rows = 0;
        public int cols = 0;
        public Double rowInterval = 0;
        public Double colInterval = 0;
        public Point[,] pinArray = null;

        public void CreatePins() //產生矩陣各點之位置,並依行列間距計算出各點x.y之座標,
        {
            pinArray = new Point[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    pinArray[i, j] = new Point();
                    pinArray[i, j].xCoordinate = (i * rowInterval);
                    pinArray[i, j].yCoordinate = (j * colInterval);
                }
            }
        }

    }
}

Triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW5
{
    class Triangle
    {
        public Point[] pointArray = new Point[3]; //建立三角形的三個點

        public bool ConditionsOfTriangle()//判斷是否符合構成三角形的條件
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]);
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);
            double temp = 0;


            while (true)
            {
                if (lengthB > lengthA) //將lengthA變為最長邊
                {
                    temp = lengthB;
                    lengthB = lengthA;
                    lengthA = temp;
                }
                else if (lengthC > lengthB)//lengthC變為最短邊
                {
                    temp = lengthC;
                    lengthC = lengthB;
                    lengthB = temp;
                }
                else
                {
                    break;
                }
            }
            if (lengthA < (lengthB + lengthC)) //三角形兩邊和大於第三邊
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public double Perimeter() //計算三角形的周長
        {
            //由DistanceTo()計算兩點間的距離算出三角形三邊之邊長
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]);
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            return Perimeter;
        }

        public double Area()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            double Perimeter = lengthA + lengthB + lengthC; 
            //以海龍公式計算三角形面積
            double Area = Math.Sqrt((0.5 * Perimeter) * (0.5 * Perimeter - lengthA) * (0.5 * Perimeter - lengthB) * (0.5 * Perimeter - lengthC));
            return Area;
        }

               public double RadiusOfCircumcircle()
        {
            double lengthA = pointArray[0].DistanceBetweenTwoPoints(pointArray[1]); 
            double lengthB = pointArray[1].DistanceBetweenTwoPoints(pointArray[2]); 
            double lengthC = pointArray[2].DistanceBetweenTwoPoints(pointArray[0]);

            //由餘弦公式計算出角A之餘弦值
            double cosA = (lengthB * lengthB + lengthC * lengthC - lengthA * lengthA) / (2 * lengthB * lengthC);
            double sinA = Math.Sqrt(1 - cosA * cosA);
            //求得外接圓半徑
            double radius = 0.5 * lengthA / sinA; 
            return radius;
        }
    }
}

[2015][Homework]Team04-Hw06

Program為進入程式點
Poker為類別,視窗設計
PokerCard為類別,轉換點數和花色
CardDeck為類別,產生Poker cards,依花色排列、亂數洗牌、依點數大小排列

[2015][Homework]Team04-Hw05

Program為進入程式點,輸入與輸出
Point為類別,計算兩點距離
PinBoard為類別,產生pinboard格點座標
Triangle為類別,產生triangle並計算周長、面積、外接圓半徑

2015年4月6日 星期一

[2015][Homework]Team01 - Hw04

[2015][Homework]Team06 - Hw06

程式分為四個部分
Program 為程式進入點
PokerForm 為視窗主程式
CardDeck 為類別 建立一整副牌.進行重置.洗牌.依點數和花色大小排列
PokerCard 為類別 設定每張牌的點數.花色 以及轉換輸出

2015年4月5日 星期日

[2015][Homework]Team03 - Hw04

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

namespace JugPuzzle
{
    class Program
    {
        static void Main(string[] args)
        {

        start:
            Console.WriteLine("請輸入水杯A");
            int settingbottleA = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請輸入水杯B");
            int settingbottleB = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("請輸入要幾公升");
            int settingtarget = Convert.ToInt32(Console.ReadLine());
            int gcd = Program.gcd(settingbottleA, settingbottleB);


            if ((settingtarget > settingbottleB) && (settingtarget > settingbottleA))
            {
                Console.WriteLine("要設定的公升比大水杯還大,永遠達不到,結束程式");
            }
            else
            {
                if (settingtarget % gcd == 0)
                {
                    Program.jugPuzzle(settingbottleA, settingbottleB, settingtarget);
                }
                else
                {
                    Console.WriteLine("此題無解");
                }
            }
            Console.Read();
        }



        static int gcd(int m, int n)      //最大公因數
        {
            int temp = 0;
            while (n != 0)
            {
                temp = m % n;
                m = n;
                n = temp;
            }
            return m;
        }



        static void jugPuzzle(int settingbottleA, int settingbottleB, int settingbottletarget)
        {

            int bottleA = 0;
            int bottleB = 0;
            int delta;
            do
            {
                if (bottleA == 0)
                {
                    bottleA += settingbottleA;
                    Console.WriteLine("A杯子裝滿{0}公升", settingbottleA);
                    Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
                }
                else if (bottleB == settingbottleB)
                {
                    bottleB = 0;
                    Console.WriteLine("B杯子把水倒掉");
                    Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
                }
                else if (bottleA == settingbottleA)
                {
                    bottleA -= settingbottleA;
                    bottleB += settingbottleA;
                    if (bottleB > settingbottleB)
                    {
                        delta = bottleB - settingbottleB;
                        bottleB = settingbottleB;
                        bottleA = delta;
                    }
                    Console.WriteLine("A杯子往B杯倒水");
                    Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
                }
                else
                {
                    bottleB += bottleA;
                    if (bottleB > settingbottleB)
                    {
                        delta = bottleB - settingbottleB;
                        bottleB = settingbottleB;
                        bottleA = delta;
                    }
                    else
                    {
                        bottleA = 0;
                    }
                    Console.WriteLine("A杯子往B杯倒水");
                    Console.WriteLine("目前狀態:(A杯子為{0}公升,B杯子為{1}公升)", bottleA, bottleB);
                }
            } while ((bottleB != settingbottletarget)&&(bottleA != settingbottletarget));

        }
    }
}