倒水程式 JugPuzzle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ted
{
class JugPuzzle
{
public int SettingBottleA;
public int SettingBottleB;
public int SettingTarget;
public TextBox Message;
public void Solve()
{
int bottleA = 0;
int bottleB = 0;
int delta;
if (SettingTarget <= (SettingBottleA + SettingBottleB))
{
if (SettingTarget % gcd(SettingBottleA, SettingBottleB) == 0)
{
Message.AppendText("開始進行分水步驟 \n" );
do
{
if (bottleA == 0)
{
bottleA = SettingBottleA;
Message.AppendText("A水杯裝滿\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else if (bottleB == SettingBottleB)
{
bottleB = 0;
Message.AppendText("B水杯清空");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else if (bottleA == SettingBottleA)
{
bottleA -= SettingBottleA;
bottleB += SettingBottleA;
Message.AppendText("A水杯倒進B水杯");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
if (bottleB > SettingBottleB)
{
delta = bottleB - SettingBottleB;
bottleB = SettingBottleB;
bottleA = delta;
Message.AppendText("B水杯裝滿,A水杯有剩下的水");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
}
else
{
bottleB += bottleA;
Message.AppendText("A水杯的水倒進B水杯");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
if (bottleB > SettingBottleB)
{
delta = bottleB - SettingBottleA;
bottleB = SettingBottleB;
bottleA = delta;
Message.AppendText("B水杯裝滿,A水杯有剩下的水");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
else
{
bottleA = 0;
Message.AppendText("A水杯清空");
Message.AppendText("\n");
Message.AppendText("(A,B) = (" + bottleA.ToString() + ',' + bottleB.ToString() + ')');
Message.AppendText("\n");
}
}
} while ((bottleA != SettingTarget) && (bottleB != SettingTarget) && ((bottleA + bottleB) != SettingTarget));
}
else
{
Message.AppendText("此題無解 \n" );
}
}
else
{
Message.AppendText("超過最大水杯,永遠達不到 \n");
}
}
int gcd(int m, int n)
{
int temp = 0;
while (n != 0)
{
temp = m % n;
m = n;
n = temp;
}
return m;
}
}
}
倒水程式視窗介面
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
JugPuzzle jug = new JugPuzzle();
jug.Message = OutPutText;
jug.SettingBottleA = Convert.ToInt32(InputBoxA.Text);
jug.SettingBottleB = Convert.ToInt32(InputBoxB.Text);
jug.SettingTarget = Convert.ToInt32(SettingTargetBox.Text);
jug.Solve()
}
}
}
課堂上寫的月曆視窗程式
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
MyDateTime MyDateTime = new MyDateTime();
MyDateTime._year = Convert.ToInt16(YearOfInput.Text);
MyDateTime._month = Convert.ToInt16(MonthOfInput.Text);
MyDateTime._day = Convert.ToInt16(DayOfInput.Text);
MyDateTime.Message=OutPutText;
if (IsLeapYear.Checked)
{
if((MyDateTime._year % 4 == 0 && MyDateTime._year % 100 !=0) || MyDateTime._year % 400 ==0)
{
}
else
{
}
}
if(DayCheck.Checked)
{
if (MyDateTime.)
{
}
else
{
}
}
if(DayConvertToWeek.Checked)
{
}
if(Calendar.Checked)
{
}
{
MessageBox.Show("沒有勾選喔!!");
}
}
private void button6_Click(object sender, EventArgs e)
{
OutPutText.Clear();
}
private void MonthOfInput_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
課堂上寫的月曆類別程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class MyDateTime
{
public int _year;
public int _month;
public int _day;
public TextBox Message;
public bool IsLeap();
{
if((_year % 4 == 0 && _year % 100 !=0) || _year % 400 ==0)
{
return true;
}
else
{
return false;
}
}
public int DaysOfMonth(_month);
{
}
public bool IsValid();
{
if( year % 4 = 0)
{
return true;
}
else
{
return false;
}
}
public int WeekDay();
public string Print ();
{
}
}
}
以下請勿算分,不是在課堂上寫出來的。
月曆視窗程式
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 Calendar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDateTime MyDateTime = new MyDateTime();
MyDateTime._year = Convert.ToInt16(InputYear.Text);
MyDateTime._month = Convert.ToInt16(MonthComBox.Text);
MyDateTime._day = Convert.ToInt16(DateComBox.Text);
MyDateTime.Message = OutputText;
TextBox Message = MyDateTime.Message;
if (IsLeapCheck.Checked)
{
if (MyDateTime.IsLeap())
{
Message.AppendText(MyDateTime._year + "年是閏年");
}
else
{
Message.AppendText(MyDateTime._year + "是平年");
}
}
else if (DateCheck.Checked)
{
if (MyDateTime.IsValid())
{
Message.AppendText(MyDateTime.Print() + "合法");
}
else
{
Message.AppendText(MyDateTime.Print() + "非法");
}
}
else if (DayToWeek.Checked)
{
switch (MyDateTime.WeekDay())
{
case 0: Message.AppendText(MyDateTime.Print() + "是星期日");
break;
case 1: Message.AppendText(MyDateTime.Print() + "是星期一");
break;
case 2: Message.AppendText(MyDateTime.Print() + "是星期二");
break;
case 3: Message.AppendText(MyDateTime.Print() + "是星期三");
break;
case 4: Message.AppendText(MyDateTime.Print() + "是星期四");
break;
case 5: Message.AppendText(MyDateTime.Print() + "是星期五");
break;
case 6: Message.AppendText(MyDateTime.Print() + "是星期六");
break;
}
}
else if (Calendar.Checked)//還在修正算法
{
Message.AppendText("\tSun\tMon\tTue\tWed\tThr\tFri\tSat\t\n");
int[] array = new int[MyDateTime.DaysOfMonth()];
for (int j = 0; j < MyDateTime.WeekDay(); j++)
{
Message.AppendText("\t");
}
for (int i = 0; i < MyDateTime.DaysOfMonth(); i++)
{
array[i] = i + 1;
Message.AppendText(array[i].ToString());
Message.AppendText("\t");
if(i%7==0)
{
Message.AppendText("\n");
Message.AppendText("\t");
}
}
}
else
{
MessageBox.Show("沒有勾選喔!!");
}
}
private void button2_Click(object sender, EventArgs e)
{
OutputText.Clear();
}
}
}
月曆類別程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calendar
{
class MyDateTime
{
public int _year;
public int _month;
public int _day;
public TextBox Message;
public bool IsLeap()
{
if ((_year % 4 == 0) || (_year % 100 != 0 && _year % 400 == 0))
{
return true;
}
return false;
}
public int DaysOfMonth()
{
switch (_month)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
return 31;
// break;
case 4:
case 6:
case 8:
case 10:
return 30;
// break;
case 2:
if (IsLeap())
{
return 29;
}
return 28;
// break;
default:
return 0;
}
}
public bool IsValid()
{
if(_day <= DaysOfMonth())
{
return true;
}
return false;
}
public int WeekDay()
{
double w = ((_day + ((2.6 * _month) - 0.2) + (_year % 100) + ((_year % 100) / 4) + ((_year / 100) / 4) - 2 * (_year / 100)) % 7);
int x = Convert.ToInt16(Math.Floor(w));
return x;
}
public string Print()
{
return _year.ToString() + "/" + _month.ToString() + "/" + _day.ToString();
}
}
}