shell腳本下編寫(xiě)萬(wàn)年歷
代碼如下:
calWhat() { year=$1 month=$2 if [ $year -lt 1990 ] || [ $month -lt 1 ] || [ $month -gt 12 ] then echo "時(shí)間不符..." 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 -a
expr $i % 100!= 0 ] || [
expr $i % 400== 0 ] then let allDay+=366 else let allDay+=365 fi let i++ done if [
expr $year % 4== 0 -a
expr $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 #當(dāng)月的第一天 echo -e "\t\t\t${year}/${month}" echo -e "日\(chéng)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 "參數(shù)?" fi
C語(yǔ)言編寫(xiě)萬(wàn)年歷
代碼如下:
#include<stdio.h> 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("請(qǐng)輸入查詢(xún)的年份:"); scanf("%d",&year); printf("請(qǐng)輸入查詢(xún)的月份:"); scanf("%d",&month); if(year<1990 || month<1 || month>12) { printf("輸入錯(cuò)誤碟联,請(qǐng)重新輸入\n"); } else { int syear; for(syear=1990;syear<year;syear++) { if((syear%400==0) || (syear%4==0&&syear%100!=0)) { sum+=366; } else { sum+=365; } } int smonth; for(smonth=1;smonth<month;smonth++) { if((year%400==0) || (year%4==0&&year%100!=0)) { mon[2]=29; } sum+=mon[smonth]; printf("sum=%d\n",sum); } int week; week=(sum+1)%7; printf("日 一 二 三 四 五 六\n"); int j; for(j=0;j<week;j++) { printf(" "); } for(j=1;j<=mon[month];j++) { printf("%-3d",j); if((j+week)%7==0) { printf("\n"); } } } printf("\n"); return 0; }
這兩種方法下的編譯思想是一樣的,只是語(yǔ)法不同而已弹灭!如果能夠利用函數(shù)封裝代碼习绢,勢(shì)必更簡(jiǎn)化整潔纠修!