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 winJugPuzzle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSolve_Click(object sender, EventArgs e)
{
JugPuzzle jug = new JugPuzzle();
jug.message = txtShowAction;
jug.jugA = Convert.ToInt16(txtCapacityOfJugA.Text);
jug.jugB = Convert.ToInt16(txtCapacityOfJugB.Text);
jug.goal = Convert.ToInt16(txtGoal.Text);
jug.solve();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtShowAction.Clear();
}
}
}
第一題 JugPuzzle
JugPuzzle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace winJugPuzzle
{
class JugPuzzle
{
public int jugA;
public int jugB;
public int goal;
public TextBox message;
public void solve()
{
if (!IsSolvable())
{
message.AppendText("無解,你被唬了\n");
return;
}
int currentWaterInJugA=0;
int currentWaterInJugB=0;
message.AppendText("開始進行分水遊戲\n");
while (true)
{
if (currentWaterInJugA == goal || currentWaterInJugB == goal || currentWaterInJugA + currentWaterInJugB == goal)
break;
message.AppendText("===================\n");
if (currentWaterInJugA == 0)
{
message.AppendText("將容器A裝滿\n");
currentWaterInJugA = jugA;
Action(currentWaterInJugA, currentWaterInJugB);
}
else if (currentWaterInJugB==jugB)
{
message.AppendText("將容器B的水到光\n");
currentWaterInJugB=0;
Action(currentWaterInJugA, currentWaterInJugB);
}
else if (jugB - currentWaterInJugB > currentWaterInJugA)
{
message.AppendText("將容器A的水到入容器B\n");
currentWaterInJugB = currentWaterInJugB + currentWaterInJugA;
currentWaterInJugA = 0;
Action(currentWaterInJugA, currentWaterInJugB);
}
else if (jugB - currentWaterInJugB < currentWaterInJugA)
{
message.AppendText("將容器A的水到入容器B,直到B滿出\n");
currentWaterInJugA = currentWaterInJugA - (jugB - currentWaterInJugB);
currentWaterInJugB = jugB;
Action(currentWaterInJugA, currentWaterInJugB);
}
}
message.AppendText("*************************************\n");
message.AppendText("*************************************\n");
if (currentWaterInJugA + currentWaterInJugB == goal)
{
message.AppendText("將容器A與容器B一起放在磅秤上,即為所求炸彈差除");
}
else if (currentWaterInJugA == goal)
{
message.AppendText("將容器A放在磅秤上,即為所求炸彈差除");
}
else if (currentWaterInJugB == goal)
{
message.AppendText("將容器B放在磅秤上,即為所求炸彈差除");
}
}
public void Action(int waterInJugA, int waterInJugB)
{
message.AppendText("(A,B)=("+waterInJugA.ToString()+','+waterInJugB.ToString()+")\n");
}
public bool IsSolvable()
{
if (jugA <= 0 || jugB <= 0 || goal <= 0)
return false;
if (goal % gcd(jugA, jugB) != 0)
return false;
return true;
}
public int gcd(int num1, int num2)
{
int temp;
while (num2 != 0)
{
temp = num1 % num2;
num1 = num2;
num2 = temp;
}
return num1;
}
}
}
第二題 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 _2015OOP_Midterm_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStartFunction_Click(object sender, EventArgs e)
{
MyDateTime date = new MyDateTime();
date.year = int.Parse(txtYear.Text.ToString());
#region Month
switch (cmbMonth.SelectedIndex)
{
case 0:
date.month = 1;
break;
case 1:
date.month = 2;
break;
case 2:
date.month = 3;
break;
case 3:
date.month = 4;
break;
case 4:
date.month = 5;
break;
case 5:
date.month = 6;
break;
case 6:
date.month = 7;
break;
case 7:
date.month = 8;
break;
case 8:
date.month = 9;
break;
case 9:
date.month = 10;
break;
case 11:
date.month = 11;
break;
case 12:
date.month = 12;
break;
}
#endregion cmb
#region Day
switch (cmbDay.SelectedIndex)
{
case 0:
date.day = 1;
break;
case 1:
date.day = 2;
break;
case 2:
date.day = 3;
break;
case 3:
date.day = 4;
break;
case 4:
date.day = 5;
break;
case 5:
date.day = 6;
break;
case 6:
date.day = 7;
break;
case 7:
date.day = 8;
break;
case 8:
date.day = 9;
break;
case 9:
date.day = 10;
break;
case 10:
date.day = 11;
break;
case 11:
date.day = 12;
break;
case 12:
date.day = 13;
break;
case 13:
date.day = 14;
break;
case 14:
date.day = 15;
break;
case 15:
date.day = 16;
break;
case 16:
date.day = 17;
break;
case 17:
date.day = 18;
break;
case 18:
date.day = 19;
break;
case 19:
date.day = 20;
break;
case 20:
date.day = 21;
break;
case 21:
date.day = 22;
break;
case 22:
date.day = 23;
break;
case 23:
date.day = 24;
break;
case 24:
date.day = 25;
break;
case 25:
date.day = 26;
break;
case 26:
date.day = 27;
break;
case 27:
date.day = 28;
break;
case 28:
date.day = 29;
break;
case 29:
date.day = 30;
break;
case 30:
date.day = 31;
break;
}
#endregion
if (rbtnIsLeap.Checked)
{
if(date.IsLeap())
txtShowSolution.AppendText(date.year.ToString()+"年是閏年\n");
else
txtShowSolution.AppendText(date.year.ToString() + "年是平年\n");
}
if (rbtnDaycheck.Checked)
{
if (date.IsValid())
txtShowSolution.AppendText(date.Print() + "是合法\n");
else
txtShowSolution.AppendText(date.Print() + "是不合法\n");
}
if (rbtnDayToDate.Checked)
{
if (date.IsValid())
txtShowSolution.AppendText(date.Print());
switch (date.WeekDay())
{
case 0:
txtShowSolution.AppendText("是星期日");
break;
case 1:
txtShowSolution.AppendText("是星期一");
break;
case 2:
txtShowSolution.AppendText("是星期二");
break;
case 3:
txtShowSolution.AppendText("是星期三");
break;
case 4:
txtShowSolution.AppendText("是星期四");
break;
case 5:
txtShowSolution.AppendText("是星期五");
break;
case 6:
txtShowSolution.AppendText("是星期六");
break;
}
}
if (rbtnCalender.Checked)
{
txtShowSolution.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\n");
date.day = 1;
int begin = date.WeekDay();
for (int i = 1; i < begin+1;i++ )
{
txtShowSolution.AppendText("\t");
}
for (int i=1; i <= date.DaysOfMonth(); i++)
{
txtShowSolution.AppendText("\t"+i.ToString());
if((begin+i)%7==0)
txtShowSolution.AppendText("\n");
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtShowSolution.Clear();
}
}
}
第二題 MyDateTime
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _2015OOP_Midterm_02 { class MyDateTime { //field private int _year; private int _month; private int _day; //Attribute 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)&&(year%400!=0))) return false; else return true; } public int DaysOfMonth() { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (IsLeap()) return 29; else return 28; default: return 0; } } public bool IsValid() { if (year < 0) return false; if (month < 1 || month > 12) return false; if (day < 1 || day > DaysOfMonth()) return false; else return true; } public string Print() { return year.ToString() + '/' + month.ToString() + '/' + day.ToString(); } public int WeekDay() { int Y; int d; int m; int y; int c; int w; Y = year; d = day; if ((month == 1) || (month == 2)) Y = Y - 1; m = ((month+9)%12)+1; y = Y % 100; c = Y / 100; w = (int)(d + Math.Floor(2.6 * m - 0.2) + y + y / 4 + c / 4 - 2 * c) % 7; if (w < 0) w = w + 7; return w; } } }
gcd這個函數的名稱改大寫GCD會比較好,如果在前面加上動詞表示取得GCD會更符合naming rules.
回覆刪除例如: GetGCD(...)
ref: https://msdn.microsoft.com/en-us/library/4df752aw%28v=vs.71%29.aspx
另外還有JugPuzzle的solve()也記得字首要大寫
刪除