编写一个脚本,运行时从键盘上输入三个参数,两个操作数,一个操作符,实现两个数的四则运算。
时间: 2024-03-07 19:49:01 浏览: 82
好的,您可以参考下面这个简单的Shell脚本实现:
```shell
#!/bin/bash
# 读取三个参数
echo "请输入两个操作数和一个操作符:"
read num1 num2 op
# 根据操作符进行四则运算
case $op in
"+")
echo "$num1 + $num2 = $(($num1 + $num2))"
;;
"-")
echo "$num1 - $num2 = $(($num1 - $num2))"
;;
"*")
echo "$num1 * $num2 = $(($num1 * $num2))"
;;
"/")
if [ $num2 -eq 0 ]; then
echo "除数不能为0!"
else
echo "$num1 / $num2 = $(($num1 / $num2))"
fi
;;
*)
echo "非法操作符!"
;;
esac
```
在运行脚本时,会提示用户依次输入两个操作数和一个操作符,然后根据操作符进行对应的四则运算并输出结果。需要注意的是,除法运算时需要判断除数是否为0。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)