用一门非常小众的语言REBOL写日历(万年历)打印的程序

先上效果图
用一门非常小众的语言REBOL写日历(万年历)打印的程序_第1张图片
由于课程设置,选修了一门非常非常小众的REBOL语言的程序设计概论,就自己观点来看,这门语言小众,语法晦涩,教材也不太好,书中作者试图一直把我们以门外汉这样灌输知识给我们,我觉得这点作者做的不是特别好,比如说流行语言中的变量,用书中作者的话就变成了单字。。。赋值就是设字。。。这看来非常的不专业,而且某种程度上貌似在拐弯,这是我对这本教材不能接受的地方。当然,对于初学者来说,这无可厚非。欸,怪我怪我,来了这么渣的专业,全机电学大物3,就我们班学大物2(2比3难),你说一堆搞机器人的学的比一堆学影视动画广告特技的还简单,这课程设置真是呵呵哒。 
好了,吐槽完了,说正事吧。这个日历是课程大作业,占总成绩30分。由于本系不是每个人都喜欢程序设计,就像我讨厌画画一样,欸,所以,在家靠父母,出门靠朋友,能帮的尽量帮咯。作业要求是本程序中的第二种输出,我多做了几个功能,源码也贴上吧。看到博客的相信你们都有需求,能改的尽量改咯,全抄的后果是啥我没试过。。。欸,也考考你们的动手能力啦

REBOL
[
    title: "Calendar"
    version: 1.0.0
    author:wizChen
]
;判断数据是否合法
validata: func["判断数据是否合法"
    year [integer!]
    month [integer!]
    day [integer!]
]
[
    if( (year < 1) or (year > 9999) or (month < 1) or (month > 12))
    [
        return FALSE
    ]
    if( day < 0)
    [
        return FALSE
    ]
    if(day == 29)
    [
        return ( (month == 2) and (isLeapYear year) or (month != 2))
    ]
    if(day == 30)
    [
        return ((month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12))
    ]
    if(day > 31)
    [
        return FALSE
    ]
    return TRUE
]
;如果输入某年某月某日,进行'1'操作处理
setDays: func["如果输入某年某月某日,进行'1'操作处理"]
[
    weekDay: 0
    year: str2int ask { please input the year:}
    month: str2int ask { please input the month:}
    day: str2int ask { please input the day:}
    while[ ! validata year month day ]
    [
        year: str2int ask { please input the year:}
        month: str2int ask { please input the month:}
        day: str2int ask { please input the day:}
    ]
    weekDay: remainder ( getAllDays year month day ) 7
    switch/default weekDay
    [
        0 
        [
            print [ year"/"month"/"day"/" "today is Sunday" ]
            outThreeLines
        ]
        1 
        [
            print [ year"/"month"/"day"/" "today is Monday" ]
            outThreeLines
        ]
        2 
        [
            print [ year"/"month"/"day"/" "today is Tuesday" ]  
            outThreeLines
        ]
        3 
        [
            print [ year"/"month"/"day"/" "today is Wednesday" ]
            outThreeLines
        ]
        4 
        [
            print [ year"/"month"/"day"/" "today is Thursday" ]
            outThreeLines
        ]
        5 
        [
            print [ year"/"month"/"day"/" "today is Friday" ]
            outThreeLines
        ]
        6 
        [
            print [ year"/"month"/"day"/" "today is Saturday" ]
            outThreeLines
        ]

    ][print "data is illegal!"]
]
;如果选输入某年某月,进行'2'操作处理
setMonth: func["如果选输入某年某月,进行'2'操作处理"]
[
    weekDay: 0
    year: str2int ask { please input the year:}
    month: str2int ask { please input the month:}
    day: 1
    while[ ! validata year month day ]
    [
        year: str2int ask { please input the year:}
        month: str2int ask { please input the month:}
    ]
    weekDay: ( remainder ( getAllDays year month day ) 7 + 1 )
    outPrint year month
]
;如果输入某年,进行'3'操作处理
setYear: func["如果输入某年,进行'3'操作处理"]
[
    m: 1
    year: str2int ask { please input the year:}
    month: 1
    day: 1
    while[ ! validata year month day ]
    [
        year: str2int ask { please input the year:}
    ]
    while[ m <= 12 ]
    [
        outPrint year m
        m: m + 1
    ]

]
;函数功能:提示用户输入,进行操作选择
inputData: func ["提示用户输入,进行操作选择"]
[
    flag: TRUE
    while[flag]
    [
        print "Welcome to my Calendar,please input the selections below"
        print ""
        print "1:input the year,month and day,showing what day is it today"
        print ""
        print "2:input the year and month,showing the month calendar"
        print ""
        print "3:input the year,showing the year calendar"
        print ""
        print "4:exit"
        print ""
        choose: str2int ask { please input a number: }
        switch/default choose
        [
            1 [ setDays ]
            2 [ setMonth ]
            3 [ setYear ]
            4 [ quit ]
        ]
        [
            print "wrong input,please input again!"
            inputData
        ]
    ]
]
;得到这一天距离公元1年1月1日多少天
getAllDays: func ["得到这一天距离公元1年1月1日多少天"
    year [integer!]
    month [integer!]
    day [integer!]
]
[
    i: 1
    j: 1
    monthDays: 0
    yearDays: 0
    sumDays: 0
    mEveryMonth: [ 31 28 31 30 31 30 31 31 30 31 30 31 ]
    ;while循环自加计算年数对天数的影响
    while[ i < year ]
    [
        either isLeapYear i
        [
            yearDays: yearDays + 366
        ]
        [
            yearDays: yearDays + 365
        ]
        i: i + 1
    ]

    ;while循环自加计算月数对天数的影响
    while[ j < month]
    [
        monthDays: monthDays + ( pick mEveryMonth j)
        if(isLeapYear year )
        [
            monthDays: monthDays + 1
        ]
        j: j + 1
    ]
    sumDays: yearDays + monthDays + day
    return sumDays
]
;判断是否为闰年
isLeapYear: func["判断是否为闰年"
    year [integer!]
]
[
    ;取余运算符 remainder arg1 arg2
    return ((( remainder year 4 ) == 0) and (( remainder year 100 ) != 0) or (( remainder year 400 ) == 0 ))
]
;string类型转integer类型
str2int: func["string类型转integer类型"
    str [string!]
]
[
    return ( to-integer str )
]
;月份翻译转换
monthTrans: func["月份翻译转换"
    month [integer!]
]
[
    switch/default month
    [
        1 [return "January"]
        2 [return "February"]
        3 [return "March"]
        4 [return "April"]
        5 [return "May"]
        6 [return "June"]
        7 [return "July"]
        8 [return "August"]
        9 [return "September"]
        10 [return "October"]
        11 [return "November"]
        12 [return "December"]
    ]
    [
        print "data is illegal!"
    ]
]
;格式化输出
outPrint: func["格式化输出"
    year [integer!]
    month [integer!]
    ]
[
    weekDay: 0
    day :1
    n: 1
    isFisrtLine: TRUE
    print ["            "year"A.D"(monthTrans month)]
    print "      SUN      MON      TUE      WES      THU      FRI      SAT"
    weekDay: (remainder (getAllDays year month day) 7)
    switch/default weekDay
    [
        0 [
            prin ""
        ]
        1 [
            prin "              "
        ]
        2 [
            prin "                       "
        ]
        3 [
            prin "                                "
        ]
        4 [
            prin "                                         "
        ]
        5 [
            prin "                                                  "
        ]
        6 [
            prin "                                                           "
        ]
    ][print "data is illegal!"]
    mEveryMonth: [ 31 28 31 30 31 30 31 31 30 31 30 31 ]
    while[ n <= (pick mEveryMonth month) ]
    [
        weekDay: remainder weekDay 7
        either weekDay == 0
        [
            either isFisrtLine
            [
                prin ""
            ]
            [
                print ""
            ]
            isFisrtLine: FALSE
            prin "     "
            outChar n
        ]
        [
            either isFisrtLine
            [
                prin ""
            ]
            [
                prin "     "
            ]
            isFisrtLine: FALSE
            outChar n
        ]
        weekDay: weekDay + 1
        n: n + 1

    ] 
    outThreeLines
]
;格式化输出字符串
outChar: func["格式化输出字符串"
    dayNum[integer!]
]
[
    either dayNum < 10
    [
        prin ["  "dayNum]
    ]
    [
        prin[" "dayNum]
    ]
]
;输出三行空字符
outThreeLines: func["输出三行空字符"]
[
    i: 0
    while[i <= 2]
    [
        print ""
        i: i + 1
    ]
]
;执行脚本
inputData

  

这个同样功能的日历用C++写的时候200多行,用这门语言写用了300行。欸,C++已经够繁重了,这们脚本语言看起来真不咋地,比他强的一抓一大把,个人觉得学python都比学这好。至少人家用的python人数比rebol多多少。。。欸,吐槽归吐槽,然后又有什么卵用呢。

最后几个内建REBOL函数解释一下吧,免得看的云里雾里(也可以自己去官方文档查看用法,或者直接在REBOL输入? FunctionName即可查看函数的功能,这儿FunctionName指的是函数的名字,比如说你要查exit函数的功能,输入? exit,它便会返回此函数的功能): 
1.remainder函数功能:取余 
2.pick函数功能:简单讲就是从一个block中取出对应下标的数据 
好像其他的也没什么好讲了,就这样吧

版权声明:本文为博主原创文章,未经博主允许不得转载。

原博发布在CSDN,地址:http://blog.csdn.net/wiz_Chen/article/details/46780533

 

你可能感兴趣的:(日历)