怎样用shell语言实现小数的加减乘除运算

如题所述

原来我拿shell写的计算器:[root@liuxitingtestdir]#catcalculator.sh#!/bin/bashecho"usage:1+3,qisquit"while[1]doread-p"->>"str1>>/dev/nulla=`echo$str|awk-F'+|-|*|/''{print$1}'`if[$a==q]thenbreakfib=`echo$str|awk-F'+|-|*|/''{print$2}'`o=`echo$str|grep-o"[[:punct:]]"|grep-v"\."`case$oin+)awk'BEGIN{printf"=%.2f\n",'$a'+'$b'}';;-)awk'BEGIN{printf"=%.2f\n",'$a'-'$b'}';;\*)awk'BEGIN{printf"=%.2f\n",'$a'*'$b'}';;/)if[$b-eq0]thenecho0CanNOTbedenominator!continuefiawk'BEGIN{printf"=%.2f\n",'$a'/'$b'}';;#^)awk'BEGIN{printf"=%.2f\n",'$a'**'$b'}';;*)echoerror;;esacdone刚刚写的倒序输出:[root@liuxitingtestdir]#catdaoXuShuChu.sh#!/bin/bashecho"usage:123456,qisquit"while[1]doecho-n"Pleaswenternumber:"readnif[$n=='q']thenbreakfisd=0rev=""on=$necho"$n"while[$n-gt0]dosd=$(($n%10))#getRemaindern=$(($n/10))#getnextdigitrev=$(echo$rev$sd)doneecho"$oninareverseorder$rev"done
温馨提示:答案为网友推荐,仅供参考
相似回答