shell脚本和C语言编写万年历

shell脚本下编写万年历

代码如下:

calWhat() { year=$1 month=$2 if [ $year -lt 1990 ] || [ $month -lt 1 ] || [ $month -gt 12 ] then echo "时间不符..." else i=1990 allDay=0 array=(0 31 28 31 30 31 30 31 31 30 31 30 31) while [ $i -lt $year ] do if [expr $i % 4== 0 -aexpr $i % 100!= 0 ] || [expr $i % 400== 0 ] then let allDay+=366 else let allDay+=365 fi let i++ done if [expr $year % 4== 0 -aexpr $year % 100!= 0 ] || [expr $year % 400== 0 ] then array[2]=29 else array[2]=28 fi i=1 while [ $i -lt $month ] do let allDay+=array[i] let i++ done let week=allDay%7 let week=(week+1)%7 #当月的第一天 echo -e "\t\t\t${year}/${month}" echo -e "日\t一\t二\t三\t四\t五\t六" colum=0 i=1 while [ $i -le $week ] do printf " \t" let i++ let colum++ done i=1 while [ $i -le ${array[$month]} ] do printf "${i}\t" let colum++ if [expr $colum % 7== 0 ] then printf "\n" fi let i++ done printf "\n" fi } if [ $# -eq 0 ] then argument1=date +%Yargument2=date +%mcalWhat ${argument1} ${argument2} elif [ $# -eq 1 ] then echo "尽情期待..." elif [ $# -eq 2 ] then argument1=$1 argument2=$2 calWhat ${argument1} ${argument2} else echo "参数❎" fi

C语言编写万年历

代码如下:

#include int main () { int year; int month; int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int sum=0; printf("请输入查询的年份:"); scanf("%d",&year); printf("请输入查询的月份:"); scanf("%d",&month); if(year<1990 || month<1 || month>12) { printf("输入错误,请重新输入\n"); } else { int syear; for(syear=1990;syear

这两种方法下的编译思想是一样的,只是语法不同而已!如果能够利用函数封装代码,势必更简化整洁!

你可能感兴趣的:(shell脚本和C语言编写万年历)