2015年5月4日 星期一
2015年4月23日 星期四
2015年4月16日 星期四
[2015][Quiz]MidExam_60373012H
按我顯示
倒水程式 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();
}
}
}
[2015][Quiz]MidExam_40073037H
Form1.cs
JugPuzzle.cs第二題
按我顯示
按我顯示
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 WinJugPuzzle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_solve_Click(object sender, EventArgs e)
{
JugPuzzle jug=new JugPuzzle();
jug.Message = txt_Message;
jug.CupOfJugA=Convert.ToInt16(txt_CupOfJugA.Text);
jug.CupOfJugB=Convert.ToInt16(txt_CupOfJugB.Text);
jug.Target=Convert.ToInt16(txt_Target.Text);
jug.solve();
}
private void btn_Clear_Click(object sender, EventArgs e)
{
txt_Message.Text = "";
}
}
}
JugPuzzle.cs
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 WinJugPuzzle
{
class JugPuzzle
{
public int CupOfJugA;
public int CupOfJugB;
public int Target;
public TextBox Message;
public int CupA=0;
public int CupB=0;
public void solve()
{
if(!IsSolved(CupOfJugA,CupOfJugB))//檢查有沒有解答
{
Message.AppendText("無解,你被唬了,Boom\n");
return;
}
Message.AppendText("開始進行分水步驟\n");
Message.AppendText("====================\n");
while(true)
{
if (CupA==0)//位置:容器A沒水,容器B不管他
{
CupA+=CupOfJugA;
Message.AppendText("將容器A裝滿\n");
Action();
if(Target==CupA||(CupA+CupB)==Target)
{
break;
}
//到達:容器A水滿,容器B不管他
}
else if(CupA==CupOfJugA)//位置:容器A水滿,容器B不管他
{
if(CupA<=(CupOfJugB-CupB))//
{
CupB+=CupA;
CupA=0;
Message.AppendText("將容器A倒進容器B\n");
Action();
}
else if(CupA>(CupOfJugB-CupB))
{
CupA-=(CupOfJugB-CupB);
CupB=CupOfJugB;
Message.AppendText("將容器A的水道進容器B,直到B滿為止\n");
Action();
}
}
else if(CupB==CupOfJugB)
{
CupB=0;
Message.AppendText("將容器B的水倒光\n");
Action();
if(Target==CupA||(CupA+CupB)==Target)
{
break;
}
}
else if(CupA!=0&&CupB==0)
{
if(CupA<=(CupOfJugB-CupB))
{
CupB+=CupA;
CupA=0;
Message.AppendText("將容器A倒進容器B\n");
Action();
}
else if(CupA>(CupOfJugB-CupB))
{
CupA-=(CupOfJugB-CupB);
CupB=CupOfJugB;
Message.AppendText("將容器A的水道進容器B,直到B滿為止\n");
Action();
}
}
}
Message.AppendText("**********************\n");
Message.AppendText("**********************\n");
Message.AppendText("將容器A與一起放在磅秤上,即為所求炸彈拆除");
}
public double gcd(int Num1, int Num2)
{
if (Num1 % Num2 == 0)
{
return Num2;
}
return gcd(Num2, (Num1 % Num2));
}
public bool IsSolved(int CupOfJugA, int CupOfJugB)
{
if (Target % gcd(CupOfJugA, CupOfJugB) != 0 || (CupOfJugA + CupOfJugB) < Target)
{
return false;
}
else
return true;
}
public void Action()
{
Message.AppendText("(A,B)=("+CupA.ToString()+','+CupB.ToString()+')');
Message.AppendText("\n");
Message.AppendText("====================================\n");
}
}
}
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 exam_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lal_year_Click(object sender, EventArgs e)
{
}
private void lal_month_Click(object sender, EventArgs e)
{
}
private void lal_day_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn_run_Click(object sender, EventArgs e)
{
MyDAteTime mdt = new MyDAteTime();
mdt._year = int.Parse(txt_year.Text);
mdt._month = Convert.ToInt16( cob_month.SelectedItem.ToString());
mdt._Day = Convert.ToInt16(cob_day.SelectedItem.ToString());
string str = "";
if (rbtn_IsLeap.Checked)
{
str = Convert.ToString(mdt._year);
if (!mdt.IsLeap())
{
txt_Display.Text = str + "年是平年";
}
else
{
txt_Display.Text = str + "年是閏年";
}
}
if (rbtn_CheckDay.Checked)
{
str = "";
if (mdt.IsValid())
{
str += mdt.Print();
str += "合法";
txt_Display.Text = str;
}
else
{
str += mdt.Print();
str += "不合法";
txt_Display.Text = str;
}
}
/*if (rbtn_DatToWeek.Checked)
{
str += mdt.Print();
switch (mdt.WeekDay())
{
case 0:
str += "星期一";
break;
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;
}
txt_Display.Text = str;
}*/
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_Display.Text = "";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class MyDAteTime
{
public int _year;
public int _month;
public int _Day;
public bool IsLeap()
{
if ((_year % 4 == 0 && _year % 100 != 0 )|| _year % 400 == 0)
{
return true;
}
else
{
return false;
}
}
public bool IsValid()
{
if (_Day <= DaysOfMonth())
{
return true;
}
else
{
return false;
}
}
public int DaysOfMonth()
{
switch (_month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
return 31;
}
case 2:
if (IsLeap())
{
return 29;
}
else
{
return 28;
}
case 4:
case 6:
case 9:
case 11:
{
return 30;
}
default:
return 0;
}
}
public string Print()
{
string str = "";
str += _year.ToString();
str += '/';
str += _month.ToString();
str += '/';
str += _Day.ToString();
return str;
}
public int WeekDay()
{
int Y = _year;
int d = _Day;
int m = _month;
double w;
if (m == 1 || m == 2)
{
Y -= 1;
}
double y = Y % 100;
double c = Y / 100;
w = (d + Convert.ToInt16(Math.Floor(2.6 * m - 0.2)) + y + Convert.ToInt16(Math.Floor(y / 4)) + Convert.ToInt16(Math.Floor( c/ 4))-Convert.ToInt16( 2*c))/7;
if (w < 0)
{
w += 7;
return Convert.ToInt16(w);
}
else
{
return Convert.ToInt16(w);
}
}
}
訂閱:
文章 (Atom)