gpt4 book ai didi

linux - 创建一个成绩计算脚本,用于计算每个 Prelim 期中考试和期末考试的平均分

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:45 25 4
gpt4 key购买 nike

我想创建一个 linux 脚本,允许用户输入他/她有多少主题。该脚本应该计算每个主题的平均值。所有科目都有 3 个学期。初步。 Midterm 和 final.Script 也应该得到 prelim 的平均值。期中和期末总平均分(Prelim+Midterm+final/3)

#!/bin/bash

counter=0
number=0
declare -a my_array
read -p "Enter the number of subects:" subj
declare -a array_term=(Prelim Midterm Final)
tres=3

for(( i = 1;i <= $subj; i++ ))
do
k=0
sum=0
number=0
declare -a my_array
read -p "Enter the number of subects:" subj
declare -a array_term=(Prelim Midterm Final)
tres=3

for(( f = 1; f <= 3; f++ ))
do
read -p "Enter ${array_term[$k]} Grade for ${my_array[$k]}:" grade
grade_array+=( $grade )
prelim=$(echo "${grade_array[0]}")
midterm=$(echo "${grade_array[1]}")
final=$(echo "${grade_array[2]}")
k=$(echo $k + 1)
done

sum=$(echo $prelim + $midterm + $final | bc)
printf '%.4f\n' $(echo $sum / $tres | bc -l)
echo "Prelim Grade is:" $prelim
echo "Midterm Grade is:" $midterm
echo "Final grade is:" $final
echo "Subject Average is":
printf '%.4f\n' $(echo $sum / $tres | bc -l)
done

结果应该是这样的

Enter number of subject: 3
Enter subject Name: Math
Enter Prelim Grade For Math: 89
Enter Midterm Grade For Math:89
Enter Final Grade For Math:89
Math Average is: 89
Enter subject Name: English
Enter Prelim Grade For English: 90
Enter Midterm Grade For English:90
Enter Final Grade For English:90
English Average is: 90
Enter subject Name: Science
Enter Prelim Grade For Science: 91
Enter Midterm Grade For Science:91
Enter Final Grade For Science:91
Science Average is: 91
Prelim Average is: 90
Midterm Average is:90
Final Average is:90
Overall Average is:90

最佳答案

是的,给你:

#!/bin/bash
declare -A matrix
#terms
num_rows=3
#subjects
read -p "Enter the number of subects:" num_columns

declare -A subj_array
for ((i=1;i<=num_columns;i++)) do
read -p "Enter the name of subject ${i}:" subj_name
subj_array[$i+1]=${subj_name}
for ((j=1;j<=num_rows;j++)) do
if [[ $j == "1" ]] ; then
read -p "Enter prelim marks for subject ${subj_name}: " jj
elif [[ $j == "2" ]]; then
read -p "Enter midterm marks for subject ${subj_name}: " jj
else
read -p "Enter final marks for subject ${subj_name}: " jj
fi
matrix[$j,$i]=$jj
done
done

f1="%$((${#num_rows}+1))s"
f2=" %9s"

printf "$f1" ''
for ((i=1;i<=num_rows;i++)) do
printf "$f2" $i
done
echo

for ((j=1;j<=num_columns;j++)) do
printf "$f1" $j
for ((i=1;i<=num_rows;i++)) do
printf "$f2" ${matrix[$i,$j]}
done
echo
done


for ((i = 1; i <= $num_columns; i++)); do
sum_per_subj=0
for ((j = 1; j <= $num_rows; j++)); do
sum_per_subj=$(echo $sum_per_subj+${matrix[$j,$i]} | bc)
done

echo "Subject ${subj_array[$i+1]} Average is":
printf '%.4f\n' $(echo $sum_per_subj / $num_columns | bc -l)
done

overall_sum=0;
for ((i = 1; i <= $num_rows; i++)); do
sum_per_term=0
for ((j = 1; j <= $num_columns; j++)); do
sum_per_term=$(echo $sum_per_term+${matrix[$i,$j]} | bc)
done
overall_sum=$(echo $overall_sum+${sum_per_term} | bc)


if [[ $i == "1" ]] ; then
echo "Term prelim Average is":
elif [[ $i == 2 ]]; then
echo "Term mideterm Average is":
else
echo "Term final Average is":
fi
printf '%.4f\n' $(echo $sum_per_term / $num_rows | bc -l)
done
echo "overall Average is":
printf '%.4f\n' $(echo $overall_sum / $num_rows | bc -l)

我使用了一个二维矩阵(来自 https://stackoverflow.com/a/16487733/7486005 ),主题跨行存储,术语跨列存储。不客气。

关于linux - 创建一个成绩计算脚本,用于计算每个 Prelim 期中考试和期末考试的平均分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58777445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com