码迷,mamicode.com
首页 > 其他好文 > 详细

编程练习

时间:2018-08-13 10:46:16      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:++   match   正确答案   strong   提醒   编程练习   输出   开始   read   

#!/bin/bash
echo "小学生测验 面向小学1~2年级学生,随机选择两个整数进行四则运算,要求学生解答。 功能要求:
(1)电脑随机出10道题,每题10分,程序结束时显示学生得分;
(2)确保算式没有超出1~2年级的水平,只允许进行100以内的四则运算,不允许两数或运算结果超出0~100的范围,负数更是不允许的;
(3)每道题学生有三次机会输入答案,当学生输入错误答案时,提醒学生重新输入,如果三次机会结束则输出正确答案;
(4)对于每道题,学生第一次输入正确答案得10分,第二次输入正确答案得7分,第三次输入正确答案得5分,否则不得分;
(5)总成绩90以上显示“SMART” ,80-90显示“GOOD”,70-80显示“OK, 60-70显示“PASS”,60以下“TRY AGAIN”
(6)保存学生做过的题目,保存的细节自己考虑。可以加载浏览。"
let count=0
let scount1=0
let scount2=0
let scount3=0
let scount4=0
let scount5=0
path=pwd

function chakan()
{
echo " ___"
echo "| 请选择 |"
echo "|*1.下一轮考试 ****|"
echo "|*
2.查看我的成绩单 *|"
echo "|***
3.退 出 **|"
while true
do
read -p ‘请输入操作序号:‘ xz
case $xz in
[1])
sh $0
;;
[2])
cat $path/$id
;;
[3])
echo "感谢使用!"
break
;;
esac
done
}

function chengji()
{
if [ $sum -ge 90 ];then
echo "SMART"
echo "SMART">>$path/$id
elif [ $sum -ge 80 ] && [ $sum -lt 90 ];then
echo "GOOD"
echo "GOOD">>$path/$id
elif [ $sum -ge 70 ] && [ $sum -lt 80 ];then
echo "OK"
echo "OK">>$path/$id
elif [ $sum -ge 60 ] && [ $sum -lt 70 ];then
echo "PASS"
echo "PASS">>$path/$id
elif [ $sum -lt 60 ];then
echo "TRY AGAIN"
echo "TRY AGAIN">>$path/$id
fi
chakan $id
}

let sum=0
while true
do
read -p "请输入八位数字学号:" id
id=${id:=0}
if [ $id -gt 0 ] >>/dev/null;then
len=expr length $id
jilu=find $path -name $id
jilu=${jilu:=0}

fi
RET=expr match $id "[0-9][0-9]*$"
RET=${RET:=0}

if [ ${RET} -eq 8 ] && [ "$jilu" = "0" ] ; then
echo "您的学号是$id,开始考试!"
sleep 2
########################################
echo "考试记录单" >>$path/$id
echo "date -d "today" +%Y%m%d-%T">>$path/$id
for((i=1;i<100;i++))
do
a[$i]=$i
done
########################################
while true
do
a=echo ${a[$RANDOM % ${#a[@]}]}
b=echo ${a[$RANDOM % ${#a[@]}]}
v=(1 2 3 4)br/>len=${#v[@]}
index=$(( RANDOM % len ))
c=echo ${v[$index]}
###加法
if [ $c -eq 1 ];then
let num=a+b
if [ $num -ge 0 ] && [ $num -le 99 ];then
let scount1=scount1+1
let scount5=scount1+scount2+scount3+scount4

echo ""
echo "
">>$path/$id
echo "第$scount5题:"
echo "第$scount5题:">>$path/$id
echo "$a + $b = (?)"
echo "$a + $b = (?)">>$path/$id
while true
do
read -p ‘请输入答案:‘ as
let count=count+1

if [ "$num" == "$as" ];then
echo "你的回答是$as,回答正确!"
echo "你的回答是$as,回答正确!">>$path/$id

if [ $count -eq 1 ];then
let sum=sum+10
elif [ $count -eq 2 ];then
let sum=sum+7
elif [ $count -eq 3 ];then
let sum=sum+5
else
let sum=sum+0
fi

let count=0
break
else
echo "你的回答是$as,答案错误,请重新输入!"
echo "你的回答是$as,答案错误,请重新输入!">>$path/$id
if [ $count -ge 3 ];then
echo "回答错误超过三次,正确答案为$num"
echo "回答错误超过三次,正确答案为$num">>$path/$id
let count=0
break
fi
fi

done

if [ $scount5 -eq 10 ];then
echo "考试结束!"
echo "考试结束!">>$path/$id
echo
echo " ">>$path/$id
echo "你的成绩是$sum"
echo "你的成绩是$sum">>$path/$id
chengji $chengji
break
fi

fi

fi

##减法
if [ $c -eq 2 ];then
let num=a-b
if [ $num -ge 0 ] && [ $num -le 99 ];then
let scount2=scount2+1
let scount5=scount1+scount2+scount3+scount4

echo ""
echo "第$scount5题:"
echo "$a - $b = (?)"
echo "
">>$path/$id
echo "第$scount5题:">>$path/$id
echo "$a - $b = (?)">>$path/$id
while true
do
read -p ‘请输入答案:‘ as
let count=count+1

if [ "$num" == "$as" ];then
echo "你的回答是$as,回答正确!"
echo "你的回答是$as,回答正确!">>$path/$id

if [ $count -eq 1 ];then
let sum=sum+10
elif [ $count -eq 2 ];then
let sum=sum+7
elif [ $count -eq 3 ];then
let sum=sum+5
else
let sum=sum+0
fi

let count=0
break
else
echo "你的回答是$as,答案错误,请重新输入!"
echo "你的回答是$as,答案错误,请重新输入!">>$path/$id
if [ $count -ge 3 ];then
echo "回答错误超过三次,正确答案为$num"
echo "回答错误超过三次,正确答案为$num">>$path/$id
let count=0
break
fi
fi

done

if [ $scount5 -eq 10 ];then
echo "考试结束!"
echo "考试结束!">>$path/$id
echo
echo " ">>$path/$id
echo "你的成绩是$sum"
echo "你的成绩是$sum">>$path/$id
chengji $chengji
break
fi

fi

fi

##乘法
if [ $c -eq 3 ];then
let num=a-b
if [ $num -ge 0 ] && [ $num -le 99 ];then
let scount3=scount3+1
let scount5=scount1+scount2+scount3+scount4

echo ""
echo "第$scount5题:"
echo "$a - $b = (?)"
echo "
">>$path/$id
echo "第$scount5题:">>$path/$id
echo "$a - $b = (?)">>$path/$id
while true
do
read -p ‘请输入答案:‘ as
let count=count+1

if [ "$num" == "$as" ];then
echo "你的回答是$as,回答正确!"
echo "你的回答是$as,回答正确!">>$path/$id

if [ $count -eq 1 ];then
let sum=sum+10
elif [ $count -eq 2 ];then
let sum=sum+7
elif [ $count -eq 3 ];then
let sum=sum+5
else
let sum=sum+0
fi

let count=0
break
else
echo "你的回答是$as,答案错误,请重新输入!"
echo "你的回答是$as,答案错误,请重新输入!">>$path/$id
if [ $count -ge 3 ];then
echo "回答错误超过三次,正确答案为$num"
echo "回答错误超过三次,正确答案为$num">>$path/$id
let count=0
break
fi
fi

done

if [ $scount5 -eq 10 ];then
echo "考试结束!"
echo "考试结束!">>$path/$id
echo
echo " ">>$path/$id
echo "你的成绩是$sum"
echo "你的成绩是$sum">>$path/$id
chengji $chengji
break
fi

fi

fi

##除法
if [ $c -eq 4 ];then
let num=a/b
let ys=a%b
if [ $num -ge 0 ] && [ $num -le 99 ];then
let scount3=scount3+1
let scount5=scount1+scount2+scount3+scount4

echo ""
echo "第$scount5题:"
echo "$a ÷ $b = (?)"
echo "
">>$path/$id
echo "第$scount5题:">>$path/$id
echo "$a ÷ $b = (?)">>$path/$id
while true
do
read -p ‘请输入答案:‘ as
if [ "$ys" != "0" ];then
read -p ‘请输入余数:‘ yushu
fi
let count=count+1

if [ "$num" == "$as" ] && [ "$ys" == "$yushu" ];then
echo "你的回答是$as,余数为$yushu,回答正确!"
echo "你的回答是$as,余数为$yushu,回答正确!">>$path/$id

if [ $count -eq 1 ];then
let sum=sum+10
elif [ $count -eq 2 ];then
let sum=sum+7
elif [ $count -eq 3 ];then
let sum=sum+5
else
let sum=sum+0
fi

let count=0
break
else
echo "你的回答是$as,余数为$yushu,答案错误,请重新输入!"
echo "你的回答是$as,余数为$yushu,答案错误,请重新输入!">>$path/$id
if [ $count -ge 3 ];then
echo "回答错误超过三次,正确答案为$num,余数为$ys"
echo "回答错误超过三次,正确答案为$num,余数为$ys">>$path/$id
let count=0
break
fi
fi

done

if [ $scount5 -eq 10 ];then
echo "考试结束!"
echo "考试结束!" >>$path/$id
echo
echo "" >>$path/$id
echo "你的成绩是$sum"
echo "你的成绩是$sum" >>$path/$id
chengji $chengji
break
fi

fi

fi

done
break
else
echo "输入非法或你已经参加过考试!"
fi
done

编程练习

标签:++   match   正确答案   strong   提醒   编程练习   输出   开始   read   

原文地址:http://blog.51cto.com/12240228/2158822

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