2015年4月16日 星期四

[2015][Quiz]MidExam_40173037H


Form:
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 Exam1;

namespace WinjugPuzzle
{
    public partial class OOP_Exam1_40173037H : Form
    {
        JugFunc jugPuzzle = new JugFunc();
        public OOP_Exam1_40173037H()
        {
            InitializeComponent();
        }

        private void Solve_Click(object sender, EventArgs e)
        {
            jugPuzzle.capacityA = int.Parse(textA.Text);
            jugPuzzle.capacityB = int.Parse(textB.Text);
            jugPuzzle.jugTarget = int.Parse(textTarget.Text);
            jugPuzzle.TextMessage = textMessage;
            jugPuzzle.JugSolve();
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            jugPuzzle.TextMessage.Clear();
        }
    }
}
Fuction Class
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 Exam1
{
    class JugFunc
    {
        public int capacityA;
        public int capacityB;
        public int jugTarget;
        public TextBox TextMessage;

        public bool IfSolExist()
        {
            if (capacityA + capacityB < jugTarget)
                return false;
            else if (capacityA == 0 && capacityB == 0 && jugTarget == 0)
                return true;
            else if (capacityA == 0 && capacityB == 0 && jugTarget != 0)
                return false;
            else if (capacityA != jugTarget && capacityB == 0 && jugTarget != 0)
                return false;
            else if (capacityA == 0 && capacityB != jugTarget && jugTarget != 0)
                return false;
            int copyA=capacityA;
            int copyB=capacityB;
            int temp;
            int GCD;
            if (copyB > copyA)
            {
                temp = copyB;
                copyB = copyA;
                copyA = temp;
            }
            while (copyA % copyB != 0)
            {
                temp = copyA % copyB;
                copyA = copyB;
                copyB = temp;
            }
            GCD = copyB;
            if (jugTarget % GCD != 0)
            {
                return false;
            }
            else
                return true;
        }
        public string Message(int step, int currentA, int currentB)
        {
            string str = "step" + step.ToString() + "\r\n";
            str += "{A,B}={" + currentA.ToString() + "," + currentB.ToString() + "}\r\n";
            return str;
        }
        public void JugSolve()
        {
            int step = 1;
            int currentA=0;
            int currentB = 0;
            string message="開始進行分水步驟!\r\n*******************************\r\n";
            if (capacityA == 0 && capacityB == 0 && jugTarget == 0)
            {
                message += "耍人嗎!?\r\n";
            }
            else if (capacityA == jugTarget)
            {
                message += "裝滿A杯的水\r\n{A,B}={" + jugTarget.ToString() + "," + "0" + "}\r\n";
                message += "把A杯放到磅秤上,即為所求\r\n";
            }
            else if(capacityB == jugTarget)
            {
                message += "裝滿B杯的水\r\n{A,B}={ " + "0" + "," + jugTarget.ToString() + "}\r\n";
                message+="把B杯放到磅秤上,即為所求\r\n";
            }
            else if (IfSolExist() == false)
            {
                TextMessage.Text = "Everyone will die!!\r\n";
                return;
            }  
            else
            {
                while ((currentA + currentB) != jugTarget)
                {
                    if (currentA == jugTarget)
                    {
                        message += "{A,B}={"  + jugTarget.ToString()+"," +currentB.ToString()+ "}\r\n";
                        break;
                    }
                    else if (currentB == jugTarget)
                    {
                        message += "{A,B}={ " +currentA.ToString()+","+ jugTarget.ToString() + "}\r\n";
                        break;
                    }
                    else if (currentB == 0)
                    {
                        currentB = capacityB;
                        message += Message(step, currentA, currentB);
                        message += "把B杯的水裝滿\r\n";
                    }
                    else if (currentA == capacityA)
                    {
                        currentA = 0;
                        message += Message(step, currentA, currentB);
                        message += "把A杯的水倒掉\r\n";
                    }
                    else if (currentA + currentB > capacityA)
                    {
                        currentB = currentB + currentA - capacityA;
                        currentA = capacityA;   
                        message += Message(step, currentA, currentB);
                        message += "把B杯的水倒進A中\r\n";
                    }
                    else
                    {
                        
                        currentA = currentA+currentB;
                        currentB = 0;
                        message += Message(step, currentA, currentB);
                        message += "把B杯的水倒進A中\r\n";
                    }
                    step++;
                }

            }
            message += "************************\r\n";
            if (currentA + currentB == jugTarget)
                message += "把兩杯放到磅秤上,即為所求\r\n";
            else if (currentA == jugTarget)
                message += "把A杯放到磅秤上,即為所求\r\n";
            else if (currentB == jugTarget)
                message += "把B杯放到磅秤上,即為所求\r\n";
            message += "***************************\r\n炸彈解除!\r\n";
            TextMessage.Text = message;

        }
    }
}

20150423 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;
using Exam;

namespace _40173037H_MidExam2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Function ExamFunc = new Function();
            ExamFunc._year = int.Parse(txtYear.Text);
            ExamFunc._month = int.Parse(cBmonth.Text);
            ExamFunc._day = int.Parse(cBday.Text);
            ExamFunc.message=textMessage;
            if (btnIsLeapYear.Checked)
            {
                if (ExamFunc.IsLeap() == true)
                    ExamFunc.message.Text = ExamFunc._year.ToString() + "年是閏年\r\n";
                else
                    ExamFunc.message.Text = ExamFunc._year.ToString() + "年是平年\r\n";
            }
            else if (btnDateExam.Checked)
            {
                if (ExamFunc.IsValid() == true)
                    ExamFunc.message.Text = ExamFunc.Print() + "合法\r\n";
                else
                    ExamFunc.message.Text = ExamFunc.Print() + "不合法\r\n";
            }
            else if (btnWeekDay.Checked)
            {
                ExamFunc.message.Text = ExamFunc.Print() + "是星期" + ExamFunc.StrWeekDay()+"\r\n";
            }
            else if (btnCalendar.Checked)
            {
                textMessage.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
                ExamFunc._day = 1;
                int begin = ExamFunc.WeekDay();
                for (int i = 0; i < begin; i++)
                {
                    textMessage.AppendText("\t");
                }
                for (int i = 1; i <= ExamFunc.DaysOfMonth(); i++)
                {
                    textMessage.AppendText("\t"+i.ToString());
                    if((i+begin)%7==0)
                        textMessage.AppendText("\n");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textMessage.Clear();
        }


    }
}

examfunc
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 Exam
{
    class Function
    {

        public int _year;
        public int _month;

        public int _day;

        public TextBox message;
        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 == 2)
            {
                if (IsLeap() == true)
                    return 29;
                else
                    return 28;
            }
            else if (_month <= 7 && _month % 2 == 1)
                return 31;
            else if (_month > 7 && _month % 2 == 0)
                return 31;
            else
                return 30;
        }
        public bool IsValid()
        {
            if (_month > 12)
                return false;
            else if (_day > DaysOfMonth())
                return false;
            else
                return true;
        }
        public int WeekDay()
        {
            int w;
            int CopyYear = _year;
            int copyMonth = _month;
            if (_month == 1 || _month == 2)
            {
                CopyYear = _year - 1;
                copyMonth = _month +12;
            }
            w = (_day +2*copyMonth+3*(copyMonth+1)/5+CopyYear+CopyYear/4-CopyYear/100+CopyYear/400+1) % 7;
            return w;
        }
        public string StrWeekDay()
        { 
            string str;
            switch (WeekDay())
            {

                case 1:
                    str = "一";
                    break;
                case 2:
                    str = "二";
                    break;
                case 3:
                    str = "三";
                    break;
                case 4:
                    str = "四";
                    break;
                case 5:
                    str = "五";
                    break;
                case 6:
                    str = "六";
                    break;
                default:
                    str = "日";
                    break;
            }
            return str;

        }
        public string Print()
        {
            string str = "";
            string strMonth;
            string strDay;
            if (_month == 10)
                strMonth = _month.ToString();
            else if (_month % 10 == 0)
                strMonth = "0" + _month.ToString();
            else
                strMonth = _month.ToString();
            if (_day == 10 || _day == 20 || _day == 30)
                strDay = _day.ToString();
            else if (_day % 10 == 0)
                strDay = "0" + _day.ToString();
            else
                strDay = _day.ToString();
            str+=_year.ToString()+"/"+strMonth+"/"+strDay;
            return str;
        }
    }
}

3 則留言:

  1. JugFunc的19行,TextMessage因為是欄位,所以字首小寫比較好喔
    57行的Message因為是方法,所以最好加個動詞作為命名會更好,Ex: ShowMessage(...)

    回覆刪除
  2. 第二次考試的部分:
    Class的名稱最好取得和自身內容有關(像這邊就取成Function,但內容是在處理日期)
    public fields的變數名稱使用PascalCase,若要使用_camelCase,則改用private fields會比較好
    TextBox是UI的物件,所以最好不要宣告在Function.cs裡
    DayOfMonth的寫法不錯,沒有藉由switch case來選擇,而是找規律來判斷一個月份的天數

    回覆刪除