日期类Date控制台应用程序设计C#

本文介绍了如何使用C#设计一个日期类Date,包括定义字段year、month、day,实现构造方法、判断闰年、获取年中天数等功能。通过实例化Date类并在控制台应用中展示日期和星期,加深了对面向对象编程的理解。
摘要由CSDN通过智能技术生成

日期类Date控制台应用程序设计

实验要求

实验要求:写出类 Date 的定义代码,含有如图所示的 3 个字段及 4 个方法。

实验目的

实验目的: 学会自定义类及类成员并实例化对象解决实际问题。

实验内容

(1) 在VS开发环境的控制台应用中,从菜单“项目”→“添加类”,添加自定义的类Date。

定义类的字段year,month,day用于记录年月日信息。

(2)定义类的构造方法Date,用于初始化日期对象为公元元年元旦。

internal Date()

{        year=1; month=1; day=1;         }

(3)定义五个方法分别完成显示日期、判断闰年、

获取年中天数、获取是星期几。

internal string DisplayDate()

bool IsLeapYear(int year) //判断该年是否为闰年

internal bool IsValidDateValue()

int GetDayOfYear()

public string GetDayOfWeek()

(4)对类Date的构造方法及DisplayDate方法进行重载,

接受与显示用户输入的日期。

internal Date(int year,int month,int day)

internal string DisplayDate(int year, int month, int day)

(5)实例化Date类,访问类的方法显示日期及日期是星期几。
在这里插入图片描述

代码实现

Date.cs代码

class Date
{
    int year, month, day;
    public Date()
    {
        year = 1; month = 1; day = 1;
    }
    public Date(int year, int month, int day)
    {
        this.year = year; this.month = month; this.day = day;
    }
    internal string DisplayDate()
    {
        return year + "-" + month + "-" + day;
    }
    internal string DisplayDate(int year, int month, int day)
    {
        return "公元" + year + "年" + month + "月" + day + "日";
    }
    //static
    internal bool IsLeapYear(int year)//判断该年是否为闰年
    {
        bool isLeap = false;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            isLeap = true;
        return isLeap;
    }
    //static
    internal int GetDayOfYear()
    {
        int dayOfYear = 0;
        int[] monthDaysEachYear = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int[] monthDaysLeapYear = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (IsLeapYear(year))
        {
            for (int i = 0; i < month; i++)
                dayOfYear += monthDaysLeapYear[i];
        }
        else
        {
            for (int i = 0; i < month; i++)
                dayOfYear += monthDaysEachYear[i];
        }
        dayOfYear += day;
        return dayOfYear;
    }

    internal bool IsValidDateValue()
    {
        if (month < 1 || month > 12)
        {
            return false;
        }
        if (day < 1 || day > 31)
        {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31))
        {
            return false;
        }
        if (month == 2)
        {
            bool leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day == 29 && !leap))
            {
                return false;
            }
        }
        return true;
    }
    internal string GetDayOfWeek()
    {
        int daysCount = 0;
        for (int i = 1; i < year; i++)
        {
            if (IsLeapYear(i))
                daysCount += 366;
            else
                daysCount += 365;
        }
        daysCount += GetDayOfYear();
        string weekDay = "星期" + "日一二三四五六"[(daysCount % 7)];
        return weekDay;
    }
}

Program.cs代码

using System;
class TestDate
{
    static void Main(string[] args)
    {
        Console.WriteLine("欢迎使用Data类,C#程序设计by SDNU咸鱼小十七酱ovo");
        Date dateNoArgs = new Date();//声明及实例化对象
        Console.WriteLine("设定的初始日期是:" + dateNoArgs.DisplayDate());
        Console.WriteLine("公元元年元旦是:" + dateNoArgs.GetDayOfWeek());
        int yearValue, monthValue, dayValue;
        Console.WriteLine("请依次输入年、月、日:");
        Console.WriteLine("请输入年:");
        yearValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("请输入月:");
        monthValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("请输入日:");
        dayValue = Convert.ToInt32(Console.ReadLine());
        Date userInputDate = new Date(yearValue, monthValue, dayValue);//声明及实例化对象
        if (userInputDate.IsValidDateValue())
        {
            Console.WriteLine("输入的日期是:" + userInputDate.DisplayDate());
            Console.WriteLine("输入的日期是:" + userInputDate.DisplayDate(yearValue, monthValue, dayValue));
            Console.WriteLine(userInputDate.DisplayDate() + "是" + userInputDate.GetDayOfWeek());
        }
        else
            Console.WriteLine("您输入的日期不正确,无法完成计算!");
        Console.ReadLine();

    }
}

运行结果

CSharp程序调试运行界面截屏//调试平台:Visual Studio 2019

收获与体会

学会了自定义类。Date.cs用了86行,Program.cs用了30行。借鉴了老师对于实验日期类Date控制台应用程序设计的视频讲解中的代码。对于C#的基本知识了解的还是太少,还需要多看课本,熟悉课本,才能更好的写代码,要继续加油喔!

有问题欢迎指正说明,并联系QQ2062642718

好的,我可以为您设计一个日期类date,实现日期的基本功能。 首先,我们需要确定日期类的属性,即年、月、日。因此,我们可以在的私有部分定义三个整型变量,分别表示年、月、日。同时,我们还需要定义一些成员函数,来实现对日期的各种操作。 下面是一个简单的日期类的示例代码: ```cpp class Date { private: int year, month, day; public: // 构造函数 Date(int y, int m, int d) { year = y; month = m; day = d; } // 获取日期的年份 int getYear() const { return year; } // 获取日期的月份 int getMonth() const { return month; } // 获取日期的日份 int getDay() const { return day; } // 判断是否为闰年 bool isLeapYear() const { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; } // 获取当前月份的天数 int getDaysOfMonth() const { int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear() && month == 2) return 29; else return days[month - 1]; } // 判断日期是否合法 bool isValid() const { if (year < 0 || month < 1 || month > 12 || day < 1 || day > getDaysOfMonth()) return false; else return true; } // 重载运算符"==",判断两个日期是否相等 bool operator==(const Date& d) const { return (year == d.year && month == d.month && day == d.day); } // 重载运算符"<",判断当前日期是否小于另一个日期 bool operator<(const Date& d) const { if (year < d.year) return true; else if (year > d.year) return false; else if (month < d.month) return true; else if (month > d.month) return false; else if (day < d.day) return true; else return false; } // 增加一天 void increaseOneDay() { day++; if (day > getDaysOfMonth()) { day = 1; month++; if (month > 12) { month = 1; year++; } } } // 输出日期 void print() const { cout << year << "-" << setw(2) << setfill('0') << month << "-" << setw(2) << setfill('0') << day; } }; ``` 这个日期类中包含了获取年、月、日、判断是否为闰年、获取当前月份的天数、判断日期是否合法、重载运算符等常用功能,还有增加一天、输出日期等操作。您可以根据自己的需求进行扩展和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值