首页 > 系统服务 > 详细

shell编程入门之成绩统计

时间:2018-01-01 20:03:43      阅读:151      评论:0      收藏:0      [点我收藏+]

备注:如果你是厂里的童鞋,写作业找到了这篇文章,希望你不要直接copy^_^,其实试着自己敲一遍会对你帮助很大。

题目要求是把student.txt内的成绩进行各个阶段的统计,文本格式都是"学号:成绩"。

下面是我的做法:

#! bin/bash
s=$(cat score.txt | cut -d : -f 2 | tr " " ",");
arr=($s);
num=(0,0,0,0,0);
grade=(0,0,0,0,0);

for x in ${arr[@]};do
	if [ $((x)) -lt 100 -a $((x)) -gt 90 ];then
		num[0]=$(($((num[0])) + 1));
		grade[0]=$((grade[0] + $((x))));
	elif [ $((x)) -gt 80 ];then
		num[1]=$(($((num[1])) + 1));
		grade[1]=$((grade[1] + $((x))));
	elif [ $((x)) -gt 70 ];then
		num[2]=$(($((num[2])) + 1));
		grade[2]=$((grade[2] + $((x))));
	elif [ $((x)) -gt 60 ];then
		num[3]=$(($((num[3])) + 1));
		grade[3]=$((grade[3] + $((x))));
	else
		num[4]=$(($((num[4])) + 1));
		grade[4]=$((grade[4] + $((x))));
	fi
done

echo the avg score of 90~100 is $((${grade[0]} / ${num[0]})),and the number of student is ${num[0]};
echo the avg score of 80~89 is $((${grade[1]} / ${num[1]})),and the number of student is ${num[1]};
echo the avg score of 70~79 is $((${grade[2]} / ${num[2]})),and the number of student is ${num[2]};
echo the avg score of 60~69 is $((${grade[3]} / ${num[3]})),and the number of student is ${num[3]};
echo the avg score of ~60 is $((${grade[4]} / ${num[4]})),and the number of student is ${num[4]}; 

 

shell编程入门之成绩统计

原文:https://www.cnblogs.com/rimochiko/p/8168606.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!