#!/bin/bash
#echo "1.$12.$23.$3"
if [ "$2" == "+" ]
then
RES=`expr $1 + $3`
echo ">>the result is:$RES"
elif [ "$2" == "-" ]
then
RES=`expr $1 - $3`
echo ">>the result is:$RES"
#这里说明下,之所以不用“*”而用“x”,
#是因为在命令行输入*,shell解析用问题,
#*不能以参数的形式传递给脚本,所以就用x代替了,
#这样能规避这个问题,但是我仍然不知道*在命令行不能传递给脚本,
#那位大侠能帮忙解释下,非常感谢~
elif [ "$2" == "x" ]then
RES=`expr $1 \* $3`
echo ">>the result is:$RES"
elif [ "$2" == "%" ]
then
if [ "$3" == "0" ] ##判断除数是否为0
then
echo "input error,division by 0"
exit
fi
SMALL=`expr $1 % $3` ##取余
LONG=6 ##小数点后保留的位数
COUNT=0 ##循环的计数器,要小于LONG
RES1=0 ##
##无限循环,目的是如果SMALL不为零,说明没有整除,计算余数的方法就是
##在SMALL后加上LONG个0,除以除数取整就可以了,LONG是小数点后精确的位数
do
SMALL=`expr $SMALL \* 10`
COUNT=`expr $COUNT + 1`
if [ $COUNT == $LONG ]
then
break
fi
done
RES1=`expr $SMALL / $3` ##到此余数就计算好了
echo ">>The result is:$BIG.$RES1" ##把整数部分和余数部分拼接起来输出就是结果了
elif [ "$1" == "--help" ]
then
echo "input format:"
echo "calculator.sh x + y"
echo "calculator.sh x - y"
echo "calculator.sh x x y"
echo "calculator.sh x % y"
echo "calculator.sh --help"
else
echo "input format error!"
fi