當前位置:首頁 » 網頁前端 » 用shell腳本寫一個BMI計算器
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

用shell腳本寫一個BMI計算器

發布時間: 2022-07-18 23:51:21

A. 在Linux系統下,編寫一個Shell腳本,從鍵盤讀入5個整數,然後顯示最大數、最小數、和平均數。

#!/bin/bash

#任意輸入5個數,判斷最大值,最小值,總和

s=0

read -p "please input:" num

s=$(($s+$num))

max=$num

min=$num

avg=$(($s/5))

for i in `seq 4`

do

read -p "please input:" num
s=$(($s+$num))

if [ $num -le $min ];then
min=$num
fi

if [ $num -ge $max ];then
max=$num
fi

done

echo max:$max min:$min avg:$avg

(1)用shell腳本寫一個BMI計算器擴展閱讀:

for循環:

一般格式為:

for var in item1 item2 ... itemN

do

command1

command2

...

commandN

done

寫成一行:

for var in item1 item2 ... itemN; do command1; command2… done;

B. 如何用php做一個bmi計算器

就是在Eclipse裡面新建一個Android工程,根據BMI的計算公式來計算,簡單的布局一下,可以進行身高體重的輸入,有確定按鈕根據輸入值進行計算就可以了。

C. 剛學shell腳本寫了個小 計算器,加減除都可以,乘法不行,哪兒錯了

你的問題在:elif [ $fangfa = " \* " ]; 這句並不能判斷到「*「乘號;
正確的結果如下:
#!/bin/bash
echo "first number"
read a
echo "fangfa"
read fangfa
echo "second number"
read b
if [ "$fangfa" = "+" ];then
echo $(($a+$b))
elif [ "$fangfa" = "-" ];then
echo $(($a-$b))
elif [ "$fangfa" = "*" ];then
echo $(($a*$b))
elif [ "$fangfa" = "/" ];then
echo $(($a/$b))
fi
===============================================
腳本優化版本:
#!/bin/bash

read -p "input first number: " num1
read -p "input operator: " operator
read -p "input second number: " num2

if [ "$operator" == "+" ];then
echo " num1 + num2 = $(($num1+$num2))"
elif [ "$operator" == "-" ];then
echo "num1 - num2 = $(($num1-$num2))"
elif [ "$operator" == "*" ];then
echo "num1 * num2 = $(($num1*$num2))"
elif [ "$operator" == "/" ];then
echo "num1 / num2 = $(($num1/$num2))"
fi

D. 編寫一個shell腳本,完成以下功能,請高手進來!

#!/bin/sh
# vim: set sw=4 ts=4 et:
ver="0.1"
help()
{
cat < rotatefile -- rotate the file name

USAGE: rotatefile [-h] filename

OPTIONS: -h help text

EXAMPLE: rotatefile out
This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1
and create an empty out-file

The max number is 10

version $ver
HELP
exit 0
}

error()
{
echo "$1"
exit 1
}
while [ -n "$1" ]; do
case $1 in
-h) help;shift 1;;
--) break;;
-*) echo "error: no such option $1. -h for help";exit 1;;
*) break;;
esac
done

# input check:
if [ -z "$1" ] ; then
error "ERROR: you must specify a file, use -h for help"
fi
filen="$1"
# rename any .1 , .2 etc file:
for n in 9 8 7 6 5 4 3 2 1; do
if [ -f "$filen.$n" ]; then
p=`expr $n + 1`
echo "mv $filen.$n $filen.$p"
mv $filen.$n $filen.$p
fi
done
# rename the original file:
if [ -f "$filen" ]; then
echo "mv $filen $filen.1"
mv $filen $filen.1
fi
echo touch $filen
touch $filen

E. 用C語言編寫程序:輸入體重和身高,輸出體重指數BMI

例子如下:

知識擴展:

BMI指數(即身體質量指數,簡稱體質指數又稱體重,英文為Body Mass Index,簡稱BMI),是用體重公斤數除以身高米數平方得出的數字,是目前國際上常用的衡量人體胖瘦程度以及是否健康的一個標准。主要用於統計用途,當我們需要比較及分析一個人的體重對於不同高度的人所帶來的健康影響時,BMI值是一個中立而可靠的指標。

BMI值原來的設計是一個用於公眾健康研究的統計工具。當我們需要知道肥胖是否對某一疾病的致病原因時,我們可以把病人的身高及體重換算成BMI值,再找出其數值及病發率是否有線性關聯。不過,隨著科技進步,現時BMI值只是一個參考值。要真正量度病人是否肥胖,還需要利用微電力量度病人的阻抗,以推斷病者的脂肪厚度。因此,BMI的角色也慢慢改變,從醫學上的用途,變為一般大眾的纖體指標。

F. 使用gedit編寫特定功能的shell腳本,熟悉shell腳本的變數、表達式和流程式控制制。

以下代碼已經經過測試,可以滿足你的所有要求:

#!/bin/bash
echo-e[1].listdirectory\

echo-e[1].deletedirectory\

echo-e[3].displayfilecontent\

echo-e[4].deletefile\

printf'inputthe0exitthisprogam. Pleaseinputyourchoice:'
read-t11num
case$numin
0)
exit
;;
1)
read-p':'-t11dir
if[-d$dir];
then
ls$dir
else
echo-e"the$dirnotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
1)
read-p':'-t11dir
if[-d$dir];
then
rmdir$dir
else
echo-e"the$dirnotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
3)
read-p':'-t11ffile
if[-f$fflie];
then
cat$ffile
else
echo-e"the$ffilenotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
4)
read-p':'-t11ffile
if[-f$fflie];
then
rm$ffile
else
echo-e"the$ffilenotexist."
fi
sleep1
sh/home/testshell/custom.sh
;;
*)
echo'idonotknowwhatyouinput!'
sleep1
sh/home/testshell/custom.sh
;;
esac
exit0

G. 如何用shell編寫一個簡單的計算器

如果只做四個簡單的運算這應應該可以

shell腳本代碼如下:
#!/bin/bash
read -p "input num1:" a
read -p "input num2:" b
read -p "input operator:" o
case $o in
+) let "res=a + b"
echo $res;;
-) let "res=a - b"
echo $res;;
/) awk 'BEGIN{printf "%.2f\n",'$a'/'$b'}';;
*) let "res=a * b"
echo $res;;
esac

H. 如何用shell腳本實現下列功能

樓上兩個真經典, 受教受教. 我菜鳥冒昧解釋解釋兩位的命令

cat log.txt| gawk -F'#| ' '{print $5}'|sort|uniq -c|gawk '{print $2,$1}'
-F'#| ' 利用#或者空格分隔, 第一次碰到學習了
sort 排序
uniq -c 合並重復, 統計個數

awk -F"[# ]" '{a[$5]=a[$5]"z"}END{for( i in a )print i"\n", length(a[i])}' temp.log
太nb了, 佩服佩服awk用的如此之嫻熟
-F"[# ]" 同樣用#或者空格分隔, 學習學習
a[$5]=a[$5] awk關聯數組, 太經典了, a[$5]用字元串做數組a的下標, 而字元串本身就是IP地址, =a[$5]"z"太nb了, 看了半天才明白, 數字內部保存的只是z, 如果遇到重復的下標只是保存了zz, NB呀高手. 後面結束的時候循環只是列印出來了下標i(IP地址), 而數組的內容實際上是a[i](也就是1個或者多個z), 然後用內置函數length取出來數字內有多少個z就是重復了幾次. 高!

厚顏無恥的補充一下排序加上sort就行了
awk -F"[# ]" '{a[$5]=a[$5]"z"}END{for( i in a )print i" ", length(a[i]) | "sort"}' datafile

to 樓上
謙虛謙虛了
看了優化命令
awk -F"#| " '{a[$5]++}END{for(i in a)print i"\n"a[i]}' temp.log 1>aaa.txt
不錯, 我又沒有想到!!! 直接自增1 不用length統計了直接列印. 太棒了