2015年4月16日 星期四

[2015][Quiz]MidExam_40173022H


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 TEST01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        JugGame jug = new JugGame();
        int capacityA;
        int capacityB;
        int goalVolume;

        private void btnSolve_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
            capacityA = int.Parse(txtCapacityA.Text);
            capacityB = int.Parse(txtCapacityB.Text);
            goalVolume = int.Parse(txtGoalVolume.Text);

            if (jug.IsLegal(capacityA, capacityB, goalVolume) != true )
                txtDisplay.Text = "輸入錯誤,請重新嘗試";
            

            else
            {
                int currentA = 0;
                int currentB = 0;
                int diff;

                if (capacityA > capacityB)
                {
                    int d = capacityA;
                    capacityA = capacityB;
                    capacityB = d;
                }
                txtDisplay.AppendText("容量較小者為容器A" + '\r' + '\n' + "A容器之容量為" + capacityA + " , B容器之容量為" + capacityB + '\r' + '\n' + "目標水量為" + goalVolume + " , 開始分水!"+'\r' + '\n'+"============="+'\r' + '\n');

                if ((capacityA + capacityB) == goalVolume)
                    txtDisplay.AppendText("將容器A與容器B裝滿水一起放置於磅秤上");
                else
                {
                    do
                    {
                        if (currentA == 0)
                        {
                            currentA += capacityA;
                            txtDisplay.AppendText("將容器A裝滿" + '\r' + '\n');
                            txtDisplay.AppendText(currentA + " , " + currentB + '\r' + '\n');
                        }
                        else if (currentB == capacityB)
                        {
                            currentB = 0;
                            txtDisplay.AppendText("將容器B的水倒掉" + '\r' + '\n');
                            txtDisplay.AppendText(currentA + " , " + currentB + '\r' + '\n');
                        }
                        else if (currentA == capacityA)
                        {
                            currentA -= capacityA;
                            currentB += capacityA;
                            if (currentB > capacityB)
                            {
                                diff = currentB - capacityB;
                                currentB = capacityB;
                                currentA = diff;
                            }
                            txtDisplay.AppendText("容器A往容器B倒水" + '\r' + '\n');
                            txtDisplay.AppendText(currentA + " , " + currentB + '\r' + '\n');
                        }
                        else
                        {
                            currentB += currentA;
                            if (currentB > capacityB)
                            {
                                diff = currentB - capacityB;
                                currentB = capacityB;
                                currentA = diff;
                            }
                            else
                                currentA = 0;
                            txtDisplay.AppendText("容器A往容器B倒水" + '\r' + '\n');
                            txtDisplay.AppendText(currentA + " , " + currentB + '\r' + '\n');
                        }

                    } while (currentA != goalVolume && currentB != goalVolume && currentA+currentB!=goalVolume);
                }
                if (currentA + currentB == goalVolume && goalVolume < capacityA || goalVolume < capacityB )
                {
                    currentB += currentA;
                    currentA = 0;
                    txtDisplay.AppendText("容器A往容器B倒水" + '\r' + '\n');
                    txtDisplay.AppendText(currentA + " , " + currentB + '\r' + '\n');
                    txtDisplay.AppendText("將容器B放置於磅秤上,炸彈解除!");
                }

                else if (currentA + currentB == goalVolume && goalVolume > capacityA || goalVolume > capacityB)  
                {
                    txtDisplay.AppendText("將兩容器一起放置於磅秤上,炸彈解除!");
                }

            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
        }
    }
}

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

namespace TEST01
{
    class JugGame
    {
        public int Gcd(int capacityA, int capacityB)
        {
            int gcdA=capacityA;
            int gcdB=capacityB;
            int temp=0;
            while(gcdA!=0)
            {
                if(gcdA < gcdB)
                {
                    temp = gcdB;
                    gcdB = gcdA;
                    gcdA = temp;
                }
                gcdA = gcdA % gcdB;
            }
            return gcdB;
        }

        public bool IsLegal(int capacityA, int capacityB, int goalVolume)
        {
            if ((goalVolume % Gcd(capacityA, capacityB)) == 0 )
                return true;
            return false;
        }
    }
}

第二題
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 TEST02
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            MyDateTime mydate = new MyDateTime();
            mydate.year = int.Parse(txtYear.Text);
            mydate.month = cboxMonth.SelectedIndex + 1;
            mydate.day = cboxDay.SelectedIndex + 1;

            if(rbtnIsLeap.Checked)
            {
                if (mydate.IsLeap()==true)
                    txtDisplay.AppendText(mydate.year+"年是閏年");
                else
                    txtDisplay.AppendText(mydate.year + "年是平年");
            }
            else if(rbtnDayCheck.Checked)
            {
                if(mydate.IsValid()==true)
                    txtDisplay.AppendText(mydate.Print()+"合法");
                else
                    txtDisplay.AppendText(mydate.Print()+"不合法");
            }
            else if(rbtnWeekDay.Checked)
            {
                switch(mydate.WeekDay())
                {
                    case 0:
                        txtDisplay.AppendText(mydate.Print() + "是星期日");
                        break;
                    case 1:
                        txtDisplay.AppendText(mydate.Print() + "是星期一");
                        break;
                    case 2:
                        txtDisplay.AppendText(mydate.Print() + "是星期二");
                        break;
                    case 3:
                        txtDisplay.AppendText(mydate.Print() + "是星期三");
                        break;
                    case 4:
                        txtDisplay.AppendText(mydate.Print() + "是星期四");
                        break;
                    case 5:
                        txtDisplay.AppendText(mydate.Print() + "是星期五");
                        break;
                    case 6:
                        txtDisplay.AppendText(mydate.Print() + "是星期六");
                        break;
                }
            }
            else if(rbtnCalendar.Checked)
            {
                txtDisplay.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
                mydate.day=1;
                int begin = mydate.WeekDay();

                for (int j = 1; j <= begin; j++)
                {
                    txtDisplay.AppendText("\t");
                }
                for (int i = 1; i <= mydate.DaysOfMonth(); i++)
                {
                    txtDisplay.AppendText("\t" + i);
                    if ((i + begin) % 7 == 0)
                    {
                        txtDisplay.AppendText("\n");
                    }
                }
            }
            else
                txtDisplay.AppendText("請選擇一項功能");
        }

    }
}

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

namespace TEST02
{
    class MyDateTime
    {
        private int _year;
        private int _month;
        private int _day;

        public int year
        {
            get {return _year;}
            set{ _year=value;}
        }
        public int month
        {
            get { return _month; }
            set{ _month=value; }
        }
        public int day
        {
            get { return _day; }
            set{_day=value;}
        }
        public bool IsLeap()
        {
            if (year % 4 == 0 && year % 100 != 0)
                return true;
            else if (year % 400 == 0)
                return true;
            else
                return false;
        }
        public int DaysOfMonth()
        {
            if (_month == 1 || _month == 3 || _month == 5 || _month == 7 || _month == 8 || _month == 10 || _month == 12)
                return 31;
            else if (_month == 4 || _month == 6 || _month == 9 || _month == 11)
                return 30;
            else
            {
                if (IsLeap() == true)
                    return 29;
                else
                    return 28;
            }
        }
        public bool IsValid()
        {
            if (DaysOfMonth() == _day)
                return true;
            else
                return false;
        }
        public int WeekDay()
        { 
            int Y;
            if(month==1||month==2)
                Y=_year-1;
            else
                Y=_year;            
            int m=((_month+9)%12)+1;
            int y=Y%100;
            int c=Y/100;
            int d=_day;

            int w = (d + Convert.ToInt16(Math.Floor(2.6 * m - 0.2)) + y + (y / 4) + (c / 4) - 2 * c) % 7;

            if (w<0)
                w+=7;
            return w;
        }
        public string Print()
        {
            string str= "";
            str += _year.ToString();
            str += "/";
            str += _month.ToString();
            str += "/";
            str += _day.ToString();
            return str;
        }
    }
}

1 則留言:

  1. MyDateTime.cs裡面屬性的名稱第一個字記得要大寫(PascalCase)
    set存取運算子可以再搭配判斷式來檢查輸入參數,合法的數值才指派給欄位,不合法的就設為null

    回覆刪除