乙個計算器的指令碼

2021-09-25 00:10:52 字數 965 閱讀 4056

#!/bin/bash

while true 

doread -p "please input one equation: " x f y

help()

#判斷是否需要幫助

if [ "$x" = "help" ] || [[ "$x" = "?" ]] || [[ "$x" = "h" ]]; then

help

fi#判斷是否為數字

num1=`echo $x|sed 's/[0-9]//g'|sed 's/[.]//'`

if [ ! -z $num1 ]; then

echo "first num error,please input num!"

help

finum2=`echo $y|sed 's/[0-9]//g'|sed 's/[.]//'`

if [ ! -z $num2 ]; then

echo "second num error,please input num!"

help

fi#因為乘法符號的特殊性,所以先判斷是否為乘法,如果不是乘法再判斷是否為其他的運算子

if [ "$f" == "*" ]; then

echo -n "a * b = " ;echo $x$f$y|bc

continue

fi#判斷是否為 + - / % 運算子

opt=`echo \$f|sed 's/[+,/,%,-]//g'`

if [ ! -z $opt ]; then

echo "please input a right operational character!"

help

fi#echo "a" $f  "b =" $(($x$f$y)) #對部分運算子不支援,bc可以支援小數運算

echo -n "a ";echo -n $f;echo -n " b = ";echo $x$f$y|bc

done

實現乙個計算器

一直以來,我都想寫一門語言,但無從下手。我找到了很多編譯原理的教程,但始終覺得內容晦澀,理解不了,所以先嘗試寫乙個簡單的,比如 計算器。網上有很多關於計算器的實現,但大多需要有編譯原理的基礎,對於我這種小白實在難以理解。我決定採用暴力模擬的方式,需要用正規表示式,但我不想自己實現,所以用js。計算器...

實現乙個計算器

一直以來,我都想寫一門語言,但無從下手。我找到了很多編譯原理的教程,但始終覺得內容晦澀,理解不了,所以先嘗試寫乙個簡單的,比如 計算器。網上有很多關於計算器的實現,但大多需要有編譯原理的基礎,對於我這種小白實在難以理解。我決定採用暴力模擬的方式,需要用正規表示式,但我不想自己實現,所以用js。計算器...

乙個簡單的計算器

乙個非常簡單的計算器 來自sololearn 只能進行單一計算 while true print 設定 print enter 增加 to 增加 two numbers print enter 減去 to 減去 two numbers print enter 相乘 to 相乘 two numbers...