-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathlib
executable file
·70 lines (56 loc) · 1.35 KB
/
mathlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# library of functions used in math problem shell scripts
# provides a random number between 1 and x
function rand() {
echo `expr $(($RANDOM % $1 + 1))`
}
# provides a random number between 0 and x
function randzero() {
local x=`expr $1 + 1`
echo `expr $(($RANDOM % $x))`
}
# Parameters:
# $1 is the name of the variable, such as 'x'
# $2 is the value of the variable
#
# Requires a variable MAX be defined containing the largest valid input
function solvealg() {
local answer=-1
while [ $answer -ne "$2" ]
do
read -p "Enter the value of $1: " answer
case $answer in
*[!0-9]) echo "Invalid input!"
answer=-1
continue ;;
esac
if [ $answer -gt $MAX ] || [ $answer -lt 1 ] ; then
printf "Input out of range!\nAll values between 1 and %s\n" \
$MAX
answer=-1
elif [ $answer -ne $2 ] ; then
echo "Incorrect, try again!"
fi
done
printf "\nGood job, you are correct!\n\n"
}
# Parameters:
# $1 is the equation to be solved
# $2 is the answer
function solvemath () {
printf "Solve the following problem\n\n"
local answer=-1
while [ $answer -ne "$2" ]
do
read -p "$1" answer
case $answer in
*[!0-9]*) printf "Invalid input! Try again!\n\n"
answer=-1
continue ;;
esac
if [ $answer -ne "$2" ]; then
printf "Incorrect, try again!\n\n"
fi
done
printf "\nGood job, you are correct!\n\n"
}
# vim: filetype=sh