给我写一个计算三角形面积的shell脚本
时间: 2023-07-07 13:38:40 浏览: 123
好的,下面是一个计算三角形面积的shell脚本:
```bash
#!/bin/bash
echo "请输入三角形的底和高:"
read base height
area=$(echo "scale=2;0.5*$base*$height" | bc)
echo "三角形的面积为:$area"
```
运行该脚本后,会提示用户输入三角形的底和高,然后计算出三角形的面积并输出。其中,使用了bc命令来进行浮点数计算,scale=2表示保留两位小数。
相关问题
Linux计算三角形面积的shell脚本
以下是一个计算三角形面积的shell脚本:
```shell
#!/bin/bash
# 获取三角形的三个边长
read -p "请输入三角形的第一条边长:" a
read -p "请输入三角形的第二条边长:" b
read -p "请输入三角形的第三条边长:" c
# 计算半周长
s=$(echo "scale=2;($a+$b+$c)/2"|bc)
# 计算面积
area=$(echo "scale=2;sqrt($s*($s-$a)*($s-$b)*($s-$c))"|bc)
echo "三角形的面积为:$area"
```
该脚本首先获取三角形的三个边长,然后计算出半周长$s$。最后使用海伦公式计算出三角形的面积,并输出结果。注意,在计算过程中需要使用`bc`命令来进行浮点数运算。
用shell脚本计算三角形面积
以下是使用shell脚本计算三角形面积的方法:
1. 计算直角三角形的面积
```shell
#!/bin/bash
read -p "请输入直角三角形的底:" base
read -p "请输入直角三角形的高:" height
area=$(echo "scale=2; 0.5 * $base * $height" | bc)
echo "直角三角形的面积为:" $area
```
2. 计算等腰三角形的面积
```shell
#!/bin/bash
read -p "请输入等腰三角形的底:" base
read -p "请输入等腰三角形的高:" height
area=$(echo "scale=2; 0.5 * $base * $height" | bc)
echo "等腰三角形的面积为:" $area
```
阅读全文
相关推荐














