Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用不同的加权百分比计算成绩,并在bash中输出最终成绩_Linux_Bash - Fatal编程技术网

Linux 使用不同的加权百分比计算成绩,并在bash中输出最终成绩

Linux 使用不同的加权百分比计算成绩,并在bash中输出最终成绩,linux,bash,Linux,Bash,我需要编写一个bashshell脚本,要求用户输入他们的姓名和4个不同百分比的测试分数,然后计算他们的总分数并输出他们的字母分数 数值如下: 作业30% 期中30% 测验10% 最后30% 我已经尝试在读取变量后(30/100)多个变量,但我无法让bash接受多行算术。到目前为止,我只能把它们加起来除以4。我在这一点上迷路了。任何帮助都是感激的 echo“你叫什么名字?” 读名字 echo“你的作业分数是多少?” 阅读s1 echo“你的测验分数是多少?” 阅读s2 echo“你期中考试的分数

我需要编写一个bashshell脚本,要求用户输入他们的姓名和4个不同百分比的测试分数,然后计算他们的总分数并输出他们的字母分数

数值如下:

作业30%
期中30%
测验10%
最后30%
我已经尝试在读取变量后(30/100)多个变量,但我无法让bash接受多行算术。到目前为止,我只能把它们加起来除以4。我在这一点上迷路了。任何帮助都是感激的

echo“你叫什么名字?”
读名字
echo“你的作业分数是多少?”
阅读s1
echo“你的测验分数是多少?”
阅读s2
echo“你期中考试的分数是多少?”
阅读s3
echo“你期末考试的分数是多少?”
阅读s4
总计=$(expr$s1+$s2+$s3+$s4)
平均值=$(expr$总计/4)
如果[$avg-通用电气80]
然后
echo“$name的成绩为A”
elif[$avg-通用电气70]
然后
echo“$name的成绩为a B”
elif[$avg-通用电气60]
然后
echo“$name的成绩为C”
elif[$avg-通用电气50]
然后
echo“$names的成绩为D”
其他的
echo“$name的成绩为F”
fi

根据我上面的评论,您可以做以下几件事。首先使用POSIX算术运算符
(…)
,而不是古老的
expr
。接下来,由于bash只提供整数数学,为了处理百分比,必须将百分比乘以
100
,然后除以
100
,以获得
总数。你不需要除以
4
,你的百分比已经占到了权重分数的100%。此外,在bash和then中,您必须依赖像
bc
这样的浮点实用程序来处理所有浮点计算,包括到达
avg
的除法

但是,当您在
if-then elif中比较
avg
时,仅此一点并不能消除舍入误差。。。其他
语句(例如,将
79.9
视为
79
)。使用
bc
获得
79.9
您仍然需要一种方法来正确处理舍入,以便从
79.5
(或更好)获得
80
,对于小于
79.5
但大于或等于
78.5
的任何内容,都可以获得
79

谢天谢地,
printf-v
提供了一种将转换结果存储为变量并处理舍入的方便方法,因为它使用C-print转换逻辑。例如,要保存由
bc
转换的
avg
的结果,然后进行适当的舍入(保存在变量名
舍入
中),可以执行以下操作:

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100))  ## not used but saved for output demonstration below
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)
#!/bin/bash

echo "What is your name?"
read name 
echo "What is your score on the Assignment?"
read s1 
echo "What is your score on the Quiz?"
read s2 
echo "What is your score on the Midterm Exam?"
read s3 
echo "What is your score on the Final Exam?"
read s4 

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100)) 
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)

if [ "$rounded" -ge 80 ] 
then 
    echo "$name's grade is an A"
elif [ "$rounded" -ge 70 ] 
then 
    echo "$name's grade is a B" 
elif [ "$rounded" -ge 60 ] 
then 
    echo "$name's grade is a C"
elif [ "$rounded" -ge 50 ] 
then 
    echo "$names's grade is a D"
else 
    echo "$name's grade is an F" 
fi
总而言之,你可以做到:

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100))  ## not used but saved for output demonstration below
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)
#!/bin/bash

echo "What is your name?"
read name 
echo "What is your score on the Assignment?"
read s1 
echo "What is your score on the Quiz?"
read s2 
echo "What is your score on the Midterm Exam?"
read s3 
echo "What is your score on the Final Exam?"
read s4 

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100)) 
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)

if [ "$rounded" -ge 80 ] 
then 
    echo "$name's grade is an A"
elif [ "$rounded" -ge 70 ] 
then 
    echo "$name's grade is a B" 
elif [ "$rounded" -ge 60 ] 
then 
    echo "$name's grade is a C"
elif [ "$rounded" -ge 50 ] 
then 
    echo "$names's grade is a D"
else 
    echo "$name's grade is an F" 
fi
要确认已正确处理舍入,您可以在保存后添加一个简单的
printf
,例如添加:

printf "\ntotal: %d\navg  : %d\nrounded avg: %d\n\n" \
        "$total" "$avg" "$rounded"
然后,要确认,请输入分数,该分数将产生一个数字,如果不正确四舍五入,将产生一个
“B”
,而不是一个
“a”
(您在
80
的分数上给出了宽松的分数),例如

示例使用/输出

$ bash student.sh
What is your name?
John
What is your score on the Assignment?
78
What is your score on the Quiz?
80
What is your score on the Midterm Exam?
81
What is your score on the Final Exam?
81

total: 7980
avg  : 79
rounded avg: 80

John's grade is an A
使用
案例
语句

$ bash student.sh
What is your name?
John
What is your score on the Assignment?
78
What is your score on the Quiz?
80
What is your score on the Midterm Exam?
81
What is your score on the Final Exam?
81

total: 7980
avg  : 79
rounded avg: 80

John's grade is an A

你也可以考虑删除你的长链<代码>如果是ELIF然后是ELIF然后…< /代码>语句用简单的<代码>案例…esac

声明,将大大清理问题。例如,您可以替换整个
if-then-elif。。。else
带有

printf "%s's grade is an '" "$name"
case "$rounded" in
    100 | [89]? ) echo "A'";;
             7? ) echo "B'";;
             6? ) echo "C'";;
             5? ) echo "D'";;
             *  ) echo "F'";;
esac
示例与
案例一起使用/输出

$ bash student.sh
What is your name?
John
What is your score on the Assignment?
78
What is your score on the Quiz?
80
What is your score on the Midterm Exam?
80
What is your score on the Final Exam?
80

total: 7940
avg  : 79
rounded avg: 79

John's grade is an 'B'

仔细检查一下,如果你有问题,请告诉我。

根据我上面的评论,你可以做几件事。首先使用POSIX算术运算符
(…)
,而不是古老的
expr
。接下来,由于bash只提供整数数学,为了处理百分比,必须将百分比乘以
100
,然后除以
100
,以获得
总数。你不需要除以
4
,你的百分比已经占到了权重分数的100%。此外,在bash和then中,您必须依赖像
bc
这样的浮点实用程序来处理所有浮点计算,包括到达
avg
的除法

但是,当您在
if-then elif中比较
avg
时,仅此一点并不能消除舍入误差。。。其他
语句(例如,将
79.9
视为
79
)。使用
bc
获得
79.9
您仍然需要一种方法来正确处理舍入,以便从
79.5
(或更好)获得
80
,对于小于
79.5
但大于或等于
78.5
的任何内容,都可以获得
79

谢天谢地,
printf-v
提供了一种将转换结果存储为变量并处理舍入的方便方法,因为它使用C-print转换逻辑。例如,要保存由
bc
转换的
avg
的结果,然后进行适当的舍入(保存在变量名
舍入
中),可以执行以下操作:

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100))  ## not used but saved for output demonstration below
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)
#!/bin/bash

echo "What is your name?"
read name 
echo "What is your score on the Assignment?"
read s1 
echo "What is your score on the Quiz?"
read s2 
echo "What is your score on the Midterm Exam?"
read s3 
echo "What is your score on the Final Exam?"
read s4 

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100)) 
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)

if [ "$rounded" -ge 80 ] 
then 
    echo "$name's grade is an A"
elif [ "$rounded" -ge 70 ] 
then 
    echo "$name's grade is a B" 
elif [ "$rounded" -ge 60 ] 
then 
    echo "$name's grade is a C"
elif [ "$rounded" -ge 50 ] 
then 
    echo "$names's grade is a D"
else 
    echo "$name's grade is an F" 
fi
总而言之,你可以做到:

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100))  ## not used but saved for output demonstration below
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)
#!/bin/bash

echo "What is your name?"
read name 
echo "What is your score on the Assignment?"
read s1 
echo "What is your score on the Quiz?"
read s2 
echo "What is your score on the Midterm Exam?"
read s3 
echo "What is your score on the Final Exam?"
read s4 

total=$((s1 * 30 + s2 * 30 + s3 * 10 + s4 * 30))
avg=$((total / 100)) 
printf -v rounded "%.0f" $(printf "scale=2; $total/100\n" | bc)

if [ "$rounded" -ge 80 ] 
then 
    echo "$name's grade is an A"
elif [ "$rounded" -ge 70 ] 
then 
    echo "$name's grade is a B" 
elif [ "$rounded" -ge 60 ] 
then 
    echo "$name's grade is a C"
elif [ "$rounded" -ge 50 ] 
then 
    echo "$names's grade is a D"
else 
    echo "$name's grade is an F" 
fi
要确认已正确处理舍入,您可以在保存后添加一个简单的
printf
,例如添加:

printf "\ntotal: %d\navg  : %d\nrounded avg: %d\n\n" \
        "$total" "$avg" "$rounded"
然后,要确认,请输入分数,该分数将产生一个数字,如果不正确四舍五入,将产生一个
“B”
,而不是一个
“a”
(您在
80
的分数上给出了宽松的分数),例如

示例使用/输出

$ bash student.sh
What is your name?
John
What is your score on the Assignment?
78
What is your score on the Quiz?
80
What is your score on the Midterm Exam?
81
What is your score on the Final Exam?
81

total: 7980
avg  : 79
rounded avg: 80

John's grade is an A
使用
案例
语句

$ bash student.sh
What is your name?
John
What is your score on the Assignment?
78
What is your score on the Quiz?
80
What is your score on the Midterm Exam?
81
What is your score on the Final Exam?
81

total: 7980
avg  : 79
rounded avg: 80

John's grade is an A

你也可以考虑删除你的长链<代码>如果是ELIF然后是ELIF然后…< /代码>语句用简单的<代码>案例…esac

语句,将极大地清洁薄