LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 796|回复: 3

[求助]此程序怎么判断如果输入的除数为0?

[复制链接]
发表于 2005-11-12 18:36:40 | 显示全部楼层 |阅读模式
如题
程序如下:
#!/bin/bash

echo "Caculator:"
while true
do
echo -n "a)+   b)-   c)*   d)/  x)=exit     "
echo -n "input:"

read op
case $op in
a) op="+" ;;
b) op="-" ;;
c) op="*" ;;
d) op="/" ;;
x) exit ;;
*) echo "wrong ,input again..."
continue ;;
esac
echo "input two numbers:"
read n1 n2

echo -n "$n1""$op""$n2"
RESULT=$[$n1$op$n2]
echo "="$RESULT
echo -n "continue (y/n)? "
read answer
case $answer in
n) break;;
y) continue;;
*) echo "wrong input!! error!!!"
break;;
esac
done
echo "bye"

小弟刚刚学习
此程序也是改来改去的。
发表于 2005-11-12 22:50:08 | 显示全部楼层
#!/bin/bash

echo "Caculator:"
while true
do
echo -n "a)+ b)- c)* d)/ x)=exit "
echo -n "input:"

read op
case $op in
a) op="+" ;;
b) op="-" ;;
c) op="*" ;;
d) op="/" ;;
x) exit ;;
*) echo "wrong ,input again..."
continue ;;
esac
echo "input two numbers:"
read n1 n2

echo -n "$n1""$op""$n2"
if test $op="/" && test $n2 -eq 0
then
echo divide by 0
else
RESULT=$[$n1$op$n2]
echo "="$RESULT
fi

echo -n "continue (y/n)? "
read answer
case $answer in
n) break;;
y) continue;;
*) echo "wrong input!! error!!!"
break;;
esac
done
echo "bye"
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-11-13 02:08:40 | 显示全部楼层
如果这样的话,算加法的时候第2个数也不能为0了。要其他算法也正常的
回复 支持 反对

使用道具 举报

发表于 2005-11-13 22:08:28 | 显示全部楼层
sorry没有仔细调试,写得不好。
把判断部分改成这样:

if [ "$op" = "/" ] && [ "$n2" = "0" ]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表