码迷,mamicode.com
首页 > 系统相关 > 详细

Shell 实现计算器

时间:2020-05-26 10:40:30      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:print   computer   syn   expr   code   lang   bash   outlook   ash   

话不多说,上代码:

#!/bin/env sh

# Author: BlueMiaomiao
# E-mail: xv2017@outlook.com
# GitHub: bluemiaomiao.github.io
# Desc  : Simple Computer Design

FIR_NUM=`echo $1 | awk -F ‘+|-|*|/‘ ‘{print $1}‘`
SEC_NUM=`echo $1 | awk -F ‘+|-|*|/‘ ‘{print $2}‘`
OPS_SYM=`echo $1 | sed ‘s/[0-9]//g‘`

if [ "$OPS_SYM" == ‘+‘ ]
then
   echo "$FIR_NUM + $SEC_NUM = `expr $FIR_NUM + $SEC_NUM`"
elif [ "$OPS_SYM" == ‘-‘ ]
then
   echo "$FIR_NUM - $SEC_NUM = `expr $FIR_NUM - $SEC_NUM`"
elif [ "$OPS_SYM" == ‘*‘ ]
then
   echo "$FIR_NUM * $SEC_NUM = `expr $FIR_NUM \* $SEC_NUM`"
elif [ "$OPS_SYM" == ‘/‘ ]
then
   if [ "$SEC_NUM" == 0 ]
   then
       echo ‘Params error.‘
   else
       echo "$FIR_NUM ÷ $SEC_NUM = `expr $FIR_NUM / $SEC_NUM`"
   fi
else
   echo ‘Syntax error.‘
fi

Shell 实现计算器

标签:print   computer   syn   expr   code   lang   bash   outlook   ash   

原文地址:https://blog.51cto.com/xvjunjie/2498408

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!