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 -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 #当月的第一天 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<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("请输入查询的年份:"); scanf("%d",&year); printf("请输入查询的月份:"); scanf("%d",&month); if(year<1990 || month<1 || month>12) { printf("输入错误,请重新输入\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; }
这两种方法下的编译思想是一样的,只是语法不同而已!如果能够利用函数封装代码,势必更简化整洁!