while true
do
echo " Enter the operand1"
read operand1
echo " Enter the operator"
read operator
echo " Enter the operand2"
read operand2
if [ "operand1" == "$result" ]
then
echo "operand1 is: $result"
echo ""
fi
if [ "$operator" == "+" ]
then
result=$(($operand1 + $operand2))
elif [ "$operator" == "-" ]
then
result=$(($operand1 - $operand2))
elif [ "$operator" == "x" ]
then
result=$(($operand1 x $operand2))
elif [ "$operator" == "/" ]
then
if [ $operand2 -eq 0]
then
echo "Can't divide by zero. please re-enter divisor."
else
result=$(($operand1 / $operand2))
fi
elif [ "$operator" == "X" ]
then
exit
elif [ "$operator" == "MS" ]
then
MS -> $result
elif [ "$operator" == "MR" ]
then
operand2=$result
MR -> $operand2
elif [ "$operator" == "M+" ]
then
M+ -> $((result+MS))
elif [ "$operator" == "MC" ]
then
$result=0
MC -> 0
else
echo "Sorry, is not a valid operator. plrase re-enter operator. "
fi
echo "$operand1 $operator $operand2 = $result"
done
$ ./calc
[ user instruction displayed ]
Enter operand: 3
Enter operator: +
Enter operand: 4
3 + 4 = 7
Enter operator: -
enter operand: 12
7 - 12 = -5
Enter operator: MS
-5 -> M
Enter operator: x
enter operand: 3
-5 x 3 = -15
Enter operator: C
results cleared(清除结果)
enter operand: MR
M -> -5
Enter operator: x
enter operand: MR
M -> -5
-5 x -5 = 25
Enter operator: M+
25 + -5 = 20 -> M
Enter operator: /
enter operand: 0
can't divide by zero. plesae re-enter divisor.(不能除以0,请重新输入)
enter operand: 0
can't divide by zero. plesae re-enter divisor.(不能除以0,请重新输入)
enter operand: -6
25 / -6 = -4
Enter operator: +
enter operand: C
results cleared(清除结果)
enter operand: -9
Enter operator: K
Sorry, k, is not a valid operator.please re-enter the operator. (k不是一个有效的operator,请重新输入)
Enter operator: Q
Sorry, Q, is not a valid operator.please re-enter the operator. (Q 不是一个有效的operator,请重新输入)
Enter operator: MC
0 -> M
Enter operator: +
enter operand:3
-9 + 3 = -6
Enter operator: X
Good bye.(再见)
在sh中添加echo和pause语句调试
……
虽然当时时间来不及了,我还是补上完美答案吧:
#!/bin/bash
echo "[ user instrution displayed ]"
echo "number + - x / C MS M+ MR MC supported, X exit"
result=0
memory=0
while true
do
read -p "Enter the operand:" operand1
if [ "$operand1" == "MR" ]
then
operand1=$memory
echo " M -> $operand1"
fi
while true
do
read -p " Enter the operator:" operator
if [ "$operator" == "+" ]
then
read -p "Enter the operand:" operand2
if [ "$operand2" == "MR" ]
then
operand2=$memory
echo " M -> $operand2"
elif [ "$operand2" == "C" ]
then
result=0
echo " results cleared."
break
fi
result=$(($operand1 + $operand2))
echo " $operand1 + $operand2 = $result"
operand1=$result
elif [ "$operator" == "-" ]
then
read -p "Enter the operand:" operand2
if [ "$operand2" == "MR" ]
then
operand2=$memory
echo " M -> $operand2"
echo "$operand2"
elif [ "$operand2" == "C" ]
then
result=0
echo " results cleared."
break
fi
result=$(($operand1 - $operand2))
echo " $operand1 - $operand2 = $result"
operand1=$result
elif [ "$operator" == "x" ]
then
read -p "Enter the operand:" operand2
if [ "$operand2" == "MR" ]
then
operand2=$memory
echo " M -> $operand2"
elif [ "$operand2" == "C" ]
then
result=0
echo " results cleared."
break
fi
result=$(($operand1 * $operand2))
echo " $operand1 x $operand2 = $result"
operand1=$result
elif [ "$operator" == "/" ]
then
while true
do
read -p "Enter the operand:" operand2
if [ "$operand2" == "MR" ]
then
operand2=$memory
echo " M -> $operand2"
elif [ "$operand2" == "C" ]
then
result=0
echo " results cleared."
break
fi
if [ $operand2 -eq 0 ]
then
echo "Can't divide by zero. please re-enter divisor."
else
result=$(($operand1 / $operand2))
echo " $operand1 / $operand2 = $result"
operand1=$result
break
fi
done
if [ "$operand2" == "C" ]
then
break
fi
elif [ "$operator" == "X" ]
then
echo "Good bye."
exit
elif [ "$operator" == "MS" ]
then
memory=$result
echo " $result -> M"
elif [ "$operator" == "C" ]
then
result=0
echo " results cleared."
break
elif [ "$operator" == "MR" ]
then
operand1=$memory
result=$operand1
echo " M -> $result"
elif [ "$operator" == "M+" ]
then
oldmemory=$memory
memory=$(($operand1 + $oldmemory))
echo " $operand1 + $oldmemory = $memory -> M"
elif [ "$operator" == "MC" ]
then
memory=0
echo " 0 -> M"
else
echo "Sorry, $operator is not a valid operator. please re-enter the operator."
fi
done
done
直接用bc命令呗