最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux shell實現(xiàn)判斷輸入的數(shù)字是否為合理的浮點數(shù)

 更新時間:2016年08月14日 11:06:57   投稿:mdxy-dxy  
這篇文章主要介紹了linux shell實現(xiàn)判斷輸入的數(shù)字是否為合理的浮點數(shù),需要的朋友可以參考下

這個shell是來判斷輸入的數(shù)字是否為合理的浮點數(shù)

實現(xiàn)代碼如下:

#!/bin/sh

# validfloat -- Tests whether a number is a valid floating-point value.
# Note that this script cannot accept scientific (1.304e5) notation.

# To test whether an entered value is a valid floating-point number, we
# need to split the value at the decimal point. We then test the first part
# to see if it's a valid integer, then test the second part to see if it's a
# valid >=0 integer, so -30.5 is valid, but -30.-8 isn't.

. validint  # Bourne shell notation to source the validint function

validfloat()
{
 fvalue="$1"

 if [ ! -z $(echo $fvalue | sed 's/[^.]//g') ] ; then

  decimalPart="$(echo $fvalue | cut -d. -f1)"
  fractionalPart="$(echo $fvalue | cut -d. -f2)"

  if [ ! -z $decimalPart ] ; then
   if ! validint "$decimalPart" "" "" ; then
    return 1
   fi
  fi

  if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] ; then
   echo "Invalid floating-point number: '-' not allowed \
    after decimal point" >&2
   return 1
  fi
  if [ "$fractionalPart" != "" ] ; then
   if ! validint "$fractionalPart" "0" "" ; then
    return 1
   fi
  fi

  if [ "$decimalPart" = "-" -o -z "$decimalPart" ] ; then
   if [ -z $fractionalPart ] ; then
    echo "Invalid floating-point format." >&2 ; return 1
   fi
  fi

 else
  if [ "$fvalue" = "-" ] ; then
   echo "Invalid floating-point format." >&2 ; return 1
  fi

  if ! validint "$fvalue" "" "" ; then
   return 1
  fi
 fi

 return 0
}

notice:
1): if [ ! -z $(echo $fvalue | sed 's/[^.]//g') ] 將輸入,以.分成整數(shù)和小數(shù)部分。
2):if [ "${fractionalPart%${fractionalPart#?}}" = "-" ] 判斷小數(shù)點后面如果接‘-'號,這輸出字符不合法
3)接著的一些if語句就是判斷小數(shù)及整數(shù)部分合不合法
4)由于 valiint函數(shù)沒給出,腳本不能完全執(zhí)行,valiint函數(shù)是判斷字符串是否全為數(shù)字.

相關(guān)文章

最新評論

东平县| 富民县| 炉霍县| 柳江县| 阿克苏市| 阿坝县| 廊坊市| 临泽县| 昭平县| 和顺县| 湖南省| 乌鲁木齐县| 金寨县| 大新县| 登封市| 平凉市| 津市市| 丰原市| 广宗县| 左权县| 阿拉善盟| 徐水县| 二连浩特市| 黑山县| 余庆县| 尉氏县| 上思县| 崇明县| 旬邑县| 洪洞县| 浪卡子县| 同江市| 阿图什市| 治县。| 长兴县| 仁寿县| 五大连池市| 璧山县| 塘沽区| 台湾省| 威信县|