F# 简易万年历程序

这个程序是我初学F#写的第一个程序~~~

程序可以根据给出的年和月生成该月的日历,年可以是1-9999中的任何一年

程序示例:

225030_PWvf_1425762.png

代码如下:

// 在 http://fsharp.net 上了解有关 F# 的更多信息
// 请参阅“F# 教程”项目以获取更多帮助。

type YearMonth(Year: int, Month: int) =
    //判断某年某月有多少天
    member x.DaysInMonth(): int = 
        System.DateTime.DaysInMonth(Year, Month)
    //判断某月的第一天是星期几
    member x.DayOfWeek1st(): System.DayOfWeek =
        System.DateTime(Year, Month, 1).DayOfWeek
    
[<EntryPoint>]
let main argv = 

    //参数的合法性校验
    if argv.Length <> 2 then 
        System.Console.WriteLine("程序用法: Calendar.exe year month")
        System.Environment.Exit(0)
        
    let year = System.Int32.Parse(argv.[0])    //输入年
    let month = System.Int32.Parse(argv.[1])   //输入月

    //年只能取公元1-9999年
    if year < 1 || year > 9999 then
        System.Console.WriteLine("年的取值在1-9999之间")
        System.Console.WriteLine("程序用法: Calendar.exe year month")
        System.Environment.Exit(0)
        
    //月只能取1-12月
    if month < 1 || month > 12 then 
        System.Console.WriteLine("月的取值必须在1-12之间")
        System.Console.WriteLine("程序用法: Calendar.exe year month")
        System.Environment.Exit(0)

    let yearmonth = new YearMonth(year, month) //年月类
    let fst = yearmonth.DayOfWeek1st()         //该月的第一天是星期几
    let len = yearmonth.DaysInMonth()          //该月的天数

    //存储日期的矩阵
    let mutable matrix = [| [| 0; 0; 0; 0; 0; 0; 0 |];
                            [| 0; 0; 0; 0; 0; 0; 0 |];
                            [| 0; 0; 0; 0; 0; 0; 0 |];
                            [| 0; 0; 0; 0; 0; 0; 0 |];
                            [| 0; 0; 0; 0; 0; 0; 0 |];
                            [| 0; 0; 0; 0; 0; 0; 0 |] |]
    
    //日历首部
    //System.Console.Clear()
    System.Console.WriteLine()
    System.Console.WriteLine("\t===================================================")
    System.Console.ForegroundColor <- System.ConsoleColor.Cyan
    System.Console.WriteLine("\t\t\t     " + argv.[0] + "年" + argv.[1] + "月")
    System.Console.ForegroundColor <- System.ConsoleColor.White
    System.Console.WriteLine("\t===================================================")
    System.Console.ForegroundColor <- System.ConsoleColor.Cyan
    System.Console.WriteLine("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT")
    System.Console.ForegroundColor <- System.ConsoleColor.White

    //x:行 y:列
    let mutable x = 0
    let mutable y = 
        match fst with
        | System.DayOfWeek.Sunday -> 0
        | System.DayOfWeek.Monday -> 1
        | System.DayOfWeek.Tuesday -> 2
        | System.DayOfWeek.Wednesday -> 3
        | System.DayOfWeek.Thursday -> 4
        | System.DayOfWeek.Friday -> 5
        | System.DayOfWeek.Saturday -> 6
        | _ -> 0
    
    //计算矩阵中的值
    for i = 1 to len do
        matrix.[x].[y] <- i
        y <- y + 1
        if y >= 7 then
            y <- y - 7
            x <- x + 1

    //输出矩阵
    for i = 0 to 5 do 

        //没找到break语句,被迫用异常退出循环 :-(
        try

            System.Console.Write("\t")
            for j = 0 to 6 do

                //设置显示颜色
                let temp = System.Console.ForegroundColor <- 
                                    if j = 0 || j = 6 then 
                                        System.ConsoleColor.Green 
                                    else 
                                        System.ConsoleColor.Yellow
                //输出日期
                if matrix.[i].[j] <> 0 then
                     System.Console.Write(" " + matrix.[i].[j].ToString() + "\t")
                elif j <> 0 || i = 0 then
                    System.Console.Write("\t")
                else
                    //遇到整行都是0的情况,抛出“异常”,退出循环
                    raise (System.FormatException())
              
            //从周日输出到周六后换行
            System.Console.WriteLine()
        
        with
        | :? System.FormatException -> printf "\b" //抵消掉多余的换行符

    //日历输出完毕后,将颜色改回
    let temp = System.Console.ForegroundColor <- System.ConsoleColor.White
        
    //日历尾部
    System.Console.WriteLine("\t===================================================")
    System.Console.ForegroundColor <- System.ConsoleColor.Cyan
    System.Console.WriteLine("\t\t\t" + System.DateTime.Now.ToString())
    System.Console.ForegroundColor <- System.ConsoleColor.White
    System.Console.WriteLine("\t===================================================")
    System.Console.WriteLine()

    //按任意键继续(退出)
    System.Console.WriteLine("按任意键继续")
    let temp = System.Console.ReadKey()
    let temp = System.Console.Write("\b\t") //删除按下的“任意键”字符

    0 // 返回整数退出代码

END

转载于:https://my.oschina.net/Tsybius2014/blog/316561

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值