Tag:expression calculator calculator
#!/bin/bash# by inputting data calculating results expression= '; calculator () { echo "Please enter a number:"; read x; echo "Please enter another number:"; read y ; echo "Please enter operation symbol:"; read symbol; case $symbol in +) ret= ' expr $x + $y '; expression= "$x + $y"; ;; -) ret= ' expr $x - $y '; expression= "$x-$y"; ;; \*) ret= ' expr $x \* $y '; expression= "$x * $y"; ;; /) ret= ' expr $x / $y '; expression = "$x/$y"; ;; %) ret= ' expr $x % $y '; ;; esac;return $ret;} while : do echo "Start calculation: Y/N:"; read -n1 flg; case $FLG in n| N) echo ' calculation terminated '; break; ;; y| Y) ;; *) echo ' compute terminated '; break; ;; esac; # if [[ $flg == "N" ]] # then # echo ' compute terminated '; # break; # fi; calculator; res=$ ? echo -e "Operation result is: ${expression}= $res \ n";d one
Record the shell to achieve a simple calculator effect, run the effect:
650) this.width=650; "title=" Qq.png "src=" https://s1.51cto.com/wyfs02/M01/8F/91/ Wkiol1jltvyyuqouaabxrqooexy385.png-wh_500x0-wm_3-wmp_4-s_1307697279.png "alt=" Wkiol1jltvyyuqouaabxrqooexy385.png-wh_50 "/>
The implementation of Shell grammar exercises simple Calculator