码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程入门之成绩统计

时间:2018-01-01 20:40:41      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:gpo   highlight   ber   编程   done   ash   统计   写作   pre   

备注:如果你是厂里的童鞋,写作业找到了这篇文章,希望你不要直接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编程入门之成绩统计

标签:gpo   highlight   ber   编程   done   ash   统计   写作   pre   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!