Advertisement
darkreplive

assignment5Q2

Dec 11th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.83 KB | None | 0 0
  1. #!bin/bash
  2.  
  3.  
  4. run=1
  5. while (run=1); do
  6. echo "hi"
  7.     echo "Do you want to add(a), subtract(b), multiply(c), divide(d), or exit(e)?"
  8.     read input
  9.     if [[ $input == "a" ]]; then
  10.         add
  11.     elif [[ $input == "b" ]]; then
  12.         subtract
  13.     elif [[ $input == "c" ]]; then
  14.         multiply
  15.     elif [[ $input == "d" ]]; then
  16.         divide
  17.     elif [[ $input == "e" ]]; then
  18.         exit
  19.     else
  20.         echo "Unexpected input, try again"
  21.     fi
  22.  
  23. done
  24.  
  25. add () {
  26. echo "Please enter two numbers whole numbers your adding and the sum"
  27.   read -r one
  28.   read -r two
  29.   read -r three
  30.   result=$(($one +$two))
  31.   if [[ $three -eq $result ]]; then
  32.   echo "correct"
  33.  
  34.   else
  35.   echo "false"
  36.   fi
  37.   run=0
  38.   return $run
  39. }
  40.  
  41. subtract () {
  42.   echo "Please enter two numbers numbers your subtracting and the result"
  43.   read -r one
  44.   read -r two
  45.   read -r three
  46.   result=$(($one - $two))
  47.   if [[ $three -eq $result ]]; then
  48.   echo "Correct! The answer is $three"
  49.  
  50.   else
  51.   echo "Wrong! The answer was $result"
  52.   fi
  53.   run=0
  54.   return $run
  55. }
  56.  
  57. divide () {
  58.   echo "Please enter two numbers numbers your divided and the answer"
  59.   read -r one
  60.   read -r two
  61.   read -r three
  62.   result=$(($one / $two))
  63.   if [[ $three = 0 ]]; then
  64.   echo "Error! You can't divide by zero"
  65.  
  66.   elif [[ $three -eq $result ]]; then
  67.   echo "Correct! The answer is $result"
  68.  
  69.   else
  70.   echo "Wrong! The answer was $result"
  71.   fi
  72.   run=0
  73.   return $run
  74. }
  75.  
  76. multiply () {
  77.     echo "Please enter two numbers numbers your multiplying and the answer"
  78.     read -r one
  79.     read -r two
  80.     read -r three
  81.     result=$(($one * $two))
  82.     if [[ $result -eq $three ]]; then
  83.     echo "Correct! The answer is $three"
  84.  
  85.     else
  86.     echo "Wrong! The answer was $result"
  87.     fi
  88.     run=0
  89.   return $run
  90. }
  91.  
  92. exit () {
  93.   echo "Goodbye"
  94.   run=0
  95.   return $run
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement