各种类型的运算
自定义四则运算
需求
脚步实现
#!/bin/bash
print_useage(){
printf "Please enter an integer!!!\n"
exit 1
}
read -p "Please input your number: " firstnum
if [ -n "`echo ${firstnum} | sed 's/[0-9]//g'`" ]
then
print_useage
fi
read -p "Please input your operator: " operator
if [ "${operator}" != "+" ] && [ "${operator}" != "-" ] && [ "${operator}" != "*" ] && [ "${operator}" != "/" ]
then
echo "只允许输入+|-|*|/"
exit 2
fi
read -p "Please input second number: " secondnum
if [ -n "`echo ${secondnum} | sed 's/[0-9]//g'`" ]
then
print_useage
fi
echo "${firstnum}${operator}${secondnum}结果是:$((${firstnum}${operator}${secondnum}))"
#echo "${firstnum}${operator}${secondnum}结果是:$((firstnum${operator}secondnum))" #也可以知识点解析
判断用户的输入是否是整数
不完美方式
相对完美方式(此方式需注意输入为空的情况,见脚本内说明)
完美方式
整数计算器
Last updated