嵌入式判断闰年平年
using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 例4_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//********************************************************************
// 【计算】按钮的单击事件代码
//********************************************************************
private void btnCount_Click(object sender, EventArgs e)
{
int y, m, d, sum;
y = int.Parse(txtInputYear.Text);
m = int.Parse(txtInputMonth.Text);
d = int.Parse(txtInputDay.Text);
if (y < 1 || y > 9999)
{
txtResult.Text = "请输入正确的年号!";
txtInputYear.Text = ""; // 清除错误的年号
txtInputYear.Focus(); // 输入年号的文本框获得焦点,等待重新输入
goto count; // 年号错误转到程序尾部,即结束应用程序
}
else if (m < 1 || m > 12)
{
txtResult.Text = "请输入正确的月份!";
txtInputMonth.Text = "";
txtInputMonth.Focus();
goto count;
}
else
switch(m)
{
case 2:
if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
{
if (d < 1 || d > 29)
{
txtResult.Text = "闰年二月29天,请输入正确的日
期!";
txtInputDay.Text = "";
txtInputDay.Focus();
goto count;
}
}
else
{
if (d < 1 || d > 28)
{
txtResult.Text = "平年二月28天,请输入正确的日
期!";
txtInputDay.Text = "";
txtInputDay.Focus();
goto count;
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (d < 1 || d > 30)
{
txtResult.Text = "小月为30天,请输入正确的日期!";
txtInputDay.Text = "";
txtInputDay.Focus();
goto count;
}
break;
default:
if (d < 1 || d > 31)
{
txtResult.Text = "大月为31天,请输入正确的日期!";
txtInputDay.Text = "";
txtInputDay.Focus();
goto count;
}
break;
}
if (m == 1 || m == 2)
sum = (m - 1) * 31 + d;
else
{
switch (m)
{
case 9:
case 11:
sum = (m - 1) * 30 + d - 2 + m / 2 + 1;
break;
default:
sum = (m - 1) * 30 + d - 2 + m / 2;
break;
}
if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
sum = sum + 1;
}
txtResult.Text = y + "年" + m + "月" + d + "日为" + y + "年的第" + sum + "天!";
// 在程序尾部设置一条空语句,并添加一个标签,当输入错误时跳转至此
count: ;
}
}
}