2015年4月16日 星期四

[2015][Quiz]MidExam_40075040H

視窗程式

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

namespace Midtern
{
    public partial class Form1 : Form
    {
        private Play game;
        public Form1()
        {
            InitializeComponent();
            game = new Play(int.Parse(txtCapcityA.Text), int.Parse(txtCapcityB.Text), int.Parse(txtTarget.Text));
        }

        private void btnSolve_Click(object sender, EventArgs e)
        {
            int capA;
            int capB;
            int goal;
            capA = int.Parse(txtCapcityA.Text);
            capB = int.Parse(txtCapcityB.Text);
            goal = int.Parse(txtTarget.Text);         
            game.compute(capA, capB, goal);
            txtDisplay.Text = game.message;

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            game.clr();
            txtDisplay.Text = game.message;
        }
    }
}

分水遊戲執行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Midtern
{
    class Play
    {
        public Glass A;
        public Glass B;
        public int goal;
        public String message;
        public String temp;
        public Play(int glassA, int glassB, int target)
        {
            temp = message;
            A = new Glass(glassA);
            B = new Glass(glassB);
            goal = target;
        }
        public  void clr()
        {
            message.Remove(1);
        }
        public void compute(int glassA, int glassB, int target)
        {
            int temp1;
            int temp2;
            int hcf;
            A.capcity=glassA;
            B.capcity=glassB;
            goal = target;
            hcf = gcd(glassA, glassB);
            if (goal % hcf != 0) message += "無解!!!";
            else
            if ((glassA >= goal) || (glassB >= goal))
            {


                while ((A.value != target) && (B.value != target))
                {
                    if (A.value == 0)
                    {
                        A.push(A.capcity);
                        message += "將容器A裝滿" + System.Environment.NewLine;
                        message += "(A,B)=" + "(" + A.value.ToString() + "," + B.value.ToString() + ")" + System.Environment.NewLine;
                        message += "=====================================================================" + System.Environment.NewLine;
                    }
                    else if (A.value == A.capcity)
                    {
                        temp1 = A.pop();
                        temp2 = B.push(temp1);
                        A.value = temp2;
                        message += "將容器A往容器B倒水" + System.Environment.NewLine;
                        message += "(A,B)=" + "(" + A.value.ToString() + "," + B.value.ToString() + ")" + System.Environment.NewLine;
                        message += "=====================================================================" + System.Environment.NewLine;
                    }
                    else if (B.value == B.capcity)
                    {
                        B.value = 0;
                        message += "將容器B的水倒掉" + System.Environment.NewLine;
                        message += "(A,B)=" + "(" + A.value.ToString() + "," + B.value.ToString() + ")" + System.Environment.NewLine;
                        message += "=====================================================================" + System.Environment.NewLine;
                    }
                    else
                    {
                        temp1 = A.pop();
                        temp2 = B.push(temp1);
                        A.value = temp2;
                        message += "將容器A往容器B倒水" + System.Environment.NewLine;
                        message += "(A,B)=" + "(" + A.value.ToString() + "," + B.value.ToString() + ")" + System.Environment.NewLine;
                        message += "=====================================================================" + System.Environment.NewLine;
                    }
                }
            }
            else
            {
                message += "無解!!!";
            }
            message += "*******************************************************" + System.Environment.NewLine + "*******************************************************" + System.Environment.NewLine;
        }
        int gcd(int x,int y)
        {
            int max = Math.Max(x,y);
            int min = Math.Min(x,y);
            if (max % min != 0)
                return gcd(max % min, min);
            else
                return min;
        }
    }
}

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

namespace Midtern
{
    public class Glass
    {
        public int capcity=0;
        public int value = 0;
        public Glass(int cap)
        {
            capcity = cap;
        }
        public int pop()
        {
            int temp = value;
            value=0;
            return temp;
        }
        public int push(int num)
        {
            int temp = value;
            if (num + value > capcity)
            {
                temp = value;
                value = capcity;
                return num + temp - capcity;
            }
            else
            {
                value = num;
                return 0;
            }
        }
    }
}
第二題(萬年曆) MyDateTime class

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

namespace Midterm_2
{
    class MyDateTime
    {
        //field
        private int _year;
        private int _month;
        private int _day;
        //attribute
        public int year
        {
            set
            {
                _year = value;
            }
            get
            {
                return _year;
            }
        }
        public int month
        {
            set
            {
                _month = value;
            }
            get
            {
                return _month;
            }
        }
        public int day
        {
            set
            {
                _day = value;
            }
            get
            {
                return _day;
            }
        }
        //constructor
        public MyDateTime()
        {
            _year = 0;
            _month = 0;
            _day = 0;
        }
        public MyDateTime(int year,int month,int day)
        {
            _year = year;
            _month = month;
            _day = day;
        }

        //method
        public bool IsLeap()
        {
            return((_year%400==0)||((_year%4==0)&&(_year%100!=0)));
        }
        public int DaysOfMonth()
        {
            if (_month == 2)
                return 28;
            else
            {
                if(_month<8)
                {
                    if (_month % 2 == 0)
                        return 30;
                    else
                        return 31;
                }
                else
                {
                    if (_month % 2 == 0)
                        return 31;
                    else return 30;
                }
            }
        }
        public bool IsValid()
        {
            return (((_year > 0)&&(_month < 13)&&(_month > 0)&&(_day > 0)&&(_day < DaysOfMonth())));
        }
        public int weekDay()
        {
            int Y=_year;
            int d = _day;
            int m = ((_month + 9) % 12) + 1;
            if ((_month == 1) || (_month == 2))
                Y = Y - 1;
            int y = Y % 100;
            int c = Y / 100;
            int w;
            
            w = (int)((int)(d + Math.Floor(2.6 *  m - 0.2) + y + Math.Floor(y / 4.0) + Math.Floor(c / 4.0) - 2 * c)) % 7;
            if (w < 0)
                return w + 7;
            else
                return w;
        }        
        public string Print()
        {
            return(_year.ToString()+"/"+_month.ToString()+"/"+_day.ToString());
        }
    }
}

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;

namespace Midterm_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboDay.SelectedIndex = 22;
            comboMonth.SelectedIndex = 3;
            rbtnLeap.Checked = true;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            MyDateTime date;
            date = new MyDateTime(int.Parse(txtYear.Text), comboMonth.SelectedIndex + 1, comboDay.SelectedIndex + 1);
            if(rbtnLeap.Checked==true)
            {
                if (date.IsLeap())
                    txtDisplay.AppendText(date.year.ToString() + "是閏年!\n");
                else
                    txtDisplay.AppendText(date.year.ToString() + "是平年!\n");
            }
            else if(rbtnDate.Checked==true)
            {
                if(date.IsValid())
                {
                    txtDisplay.AppendText(date.Print()+"是合法的!\n");
                }
                else
                {
                    txtDisplay.AppendText(date.Print() + "是不合法的!\n");
                }
            }
            else if(rbtnTrans.Checked==true)
            {
                txtDisplay.AppendText(date.Print());
                if(date.weekDay()==0)
                {
                    txtDisplay.AppendText("是星期天\n");
                }
                else if(date.weekDay()==1)
                {
                    txtDisplay.AppendText("是星期一\n");
                }
                else if (date.weekDay() == 2)
                {
                    txtDisplay.AppendText("是星期二\n");
                }
                else if (date.weekDay() == 3)
                {
                    txtDisplay.AppendText("是星期三\n");
                }
                else if (date.weekDay() == 4)
                {
                    txtDisplay.AppendText("是星期四\n");
                }
                else if (date.weekDay() == 5)
                {
                    txtDisplay.AppendText("是星期五\n");
                }
                else if (date.weekDay() == 6)
                {
                    txtDisplay.AppendText("是星期六\n");
                }
            }
            else
            {
                txtDisplay.AppendText("\n\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
                int ptr = 0;
                int ptr2=0;
                int row = 0;
                MyDateTime box = new MyDateTime(int.Parse(txtYear.Text),comboMonth.SelectedIndex+1,1);
                ptr2 = box.weekDay();
                for(int i=1;i<=date.DaysOfMonth();i++)
                {
             check: if (ptr == 7)
                    {
                        ptr = 0;
                        ptr2 = 0;
                        row++;
                        txtDisplay.AppendText("\n");
                    }
                 
                    if (ptr == 0)
                        txtDisplay.AppendText("\t");
                    
                    if (ptr2 == ptr)
                    {
                        txtDisplay.AppendText(i.ToString());
                        if (i < 10)
                            txtDisplay.AppendText("\t");
                        else
                            txtDisplay.AppendText("\t");
                        ptr++;
                        ptr2++;
                        continue;
                    }
                    else
                    {
                        txtDisplay.AppendText("\t");
                        ptr++;
                        goto check;
                    }
                }
            }
        }

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

沒有留言:

張貼留言