免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1361 | 回复: 9
打印 上一主题 下一主题

i need help [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-02-24 01:24 |只看该作者 |倒序浏览
i had this question but i dontknow how to solve it,

Write a shell script which receives any year from the keyboard and determines whether the year is leap year or not

i am a new user in unix
hopefully someone will know how to do it
thanks

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-02-24 03:07 |只看该作者

i need help

牛一点的
  1. #!/bin/ksh

  2. function cal_year {
  3. echo "please enter the year: "
  4. read year
  5. leap_value=` expr $year % 4 `

  6. if [ $leap_value -eq 0 ]
  7.   then
  8.     echo "$year is leap year"
  9.   else
  10.     echo "$year is not leap year"
  11. fi

  12. echo "Do you have some other year to enter? (please input yes or no) "
  13. read input

  14. case $input in
  15. y|Y|yes|Yes|YES) cal_year ;;
  16. n|N|no|No|NO)    exit ;;
  17. *)               echo "pleas enter yes or no!" ;;
  18. esac
  19. }

  20. cal_year
复制代码


袖珍型的


  1. #!/bin/ksh
  2. echo "please enter the year: "
  3. read year
  4. leap_value=` expr $year % 4 `

  5. if [ $leap_value -eq 0 ]
  6.   then
  7.     echo "$year is leap year"
  8.   else
  9.     echo "$year is not leap year"
  10. fi
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-02-24 03:10 |只看该作者

i need help

弟兄们,俺是不是有点笨啦?

论坛徽章:
0
4 [报告]
发表于 2003-02-24 03:28 |只看该作者

i need help

thanks for your help men

论坛徽章:
0
5 [报告]
发表于 2003-02-24 04:21 |只看该作者

i need help

...

it's quite easy. you may write a subroutine that verifies:

1)  if modulo 4 gives 0, it's a leap year
2)  but if modulo 100 also gives 0, then it isn't
3)  however, if modelo 400 gives 0 again, then it's a leap year again.

but i'd like not do so complicated. Much easier will be:


  1. #!/bin/sh

  2. test -z $1 && echo "Usage: $0 YYYY" && exit 1
  3. YYYY=$1
  4. STR=`cal 02 $YYYY`
  5. LASTDAY=`echo $STR | awk '{print $NF}'`
  6. if  [ x$LASTDAY = x28 ]; then
  7.         echo "No, year $YYYY is NOT a leap year."
  8. elif  [ x$LASTDAY = x29 ]; then
  9.         echo "Yes, year $YYYY is a leap year."
  10. else
  11.         echo "eh... something wrong with year $YYYY..."
  12.         exit 2
  13. fi
复制代码


but if you are a student majored in computer science and it's your assignment, you'd better do it in ordinary way, since your professor will be happy with your algorithm instead of solution.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-02-24 06:18 |只看该作者

i need help

> 2) but if modulo 100 also gives 0, then it isn't


原来能被100整除就不是闰年啦?真就不知也

论坛徽章:
0
7 [报告]
发表于 2003-02-24 09:44 |只看该作者

i need help

没学过数学?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2003-02-24 09:57 |只看该作者

i need help

数学里有这个?

论坛徽章:
0
9 [报告]
发表于 2003-02-24 10:04 |只看该作者

i need help

偶记得小学数学就讲过什么是闰年的问题,难道大虾是传说中的神童,不用读小学?

论坛徽章:
0
10 [报告]
发表于 2003-02-24 10:34 |只看该作者

i need help

俺也给一个

#usage: command YYYY

aa=`cal 02 $1`
bb=`echo $aa|awk '{print $NF}'`
if [ $bb -eq 29 ];then
echo "$1 is leap year"
fi
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP