Advertisement
Guest User

Untitled

a guest
May 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.66 KB | None | 0 0
  1. #!/bin/bash
  2. option=x
  3. #sudo apt-get install bc
  4. areaOfSquare ()
  5. {
  6. printf "Please enter the length of a side of the square:\n"
  7. read side
  8.  
  9. area=$((side**2))
  10. printf "The area of the square is: $area\n"
  11. }
  12.  
  13. areaOfRectangle ()
  14. {
  15. printf "Please enter the length of the rectangle:\n"
  16. read length
  17.  
  18. printf "Please enter the width of the rectangle:\n"
  19. read width
  20.  
  21. area=$(($length * $width))
  22. printf "The area of the rectangle is: $area\n"
  23. }
  24.  
  25. areaOfCircle ()
  26. {
  27. printf "Please enter the radius of the circle:\n"
  28. read radius
  29.  
  30. area=$( echo "scale=6; $radius * $radius * 3.14159" | bc )
  31. printf "The area of the circle is: $area\n"
  32. }
  33.  
  34. areaOfCylinder ()
  35. {
  36. printf "Please enter the radius of the top of the cylinder:\n"
  37. read radius
  38.  
  39. printf "Please enter the height of the cylinder:\n"
  40. read height
  41.  
  42. areaCircle=$( echo "scale=6; $radius * $radius * 3.14159" | bc )
  43.  
  44. areaCylinder=$(echo "scale=6; $areaCircle * $height" | bc)
  45.  
  46. printf "The area of the cylinder is: $areaCylinder\n"
  47. }
  48.  
  49. printf "Use one of the following options:\n"
  50. printf "s:  to calculate the area of a square\n"
  51. printf "r:  to calculate the area of a rectangle\n"
  52. printf "c:  to calculate the area of a circle\n"
  53. printf "cy: to calculate the area of a cylinder\n"
  54.  
  55. until [ "$option" = "s" -o "$option" = "r" -o "$option" = "c" -o "$option" = "cy" ]
  56. do
  57.     printf "Please enter your option:\n"
  58.     read option
  59.    
  60.     # if [ "$option" != "s" -o "$option" != "r" -o "$option" != "c" -o "$option" != "cy" ]
  61.         # then
  62.             # printf "You have entered an incorrect option.\n"
  63.     # fi
  64.    
  65. done
  66.  
  67. case "$option" in
  68.     s)  areaOfSquare
  69.         ;;
  70.     r)  areaOfRectangle
  71.         ;;
  72.     c)  areaOfCircle
  73.         ;;
  74.     cy) areaOfCylinder
  75.         ;;
  76.        
  77. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement