0% found this document useful (0 votes)
15 views4 pages

UNIX Calculator

Scripts to build a basic UNIX calculator 1. calcapp.sh (main menu) 2. ADD.sh 3.SUB.sh 4. MULT.sh 5. DIV.sh ERROR.sh (input validation)

Uploaded by

mahogany2783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

UNIX Calculator

Scripts to build a basic UNIX calculator 1. calcapp.sh (main menu) 2. ADD.sh 3.SUB.sh 4. MULT.sh 5. DIV.sh ERROR.sh (input validation)

Uploaded by

mahogany2783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

###Monique C.

Wilson
###CT152 Class Project
###Due: December 15, 2023
###################################################################################
#####################
###ORDER OF SCRIPTS: calcApp.sh, ADD.sh, SUB.sh, MULT.sh, DIV.sh, ERROR.sh

###calcApp.sh
###Calculator Main Menu

flag=0
while true
do
if [ $flag -eq 0 ]
then
tput clear
tput cup 3 15
echo -e "Calculator Main Menu\n\t------------------------------------------
---\n"
echo -e "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Ex
it\n"
flag=0
fi
echo -e "Enter your selection (1 - 5): \b\c"
read selection

case $selection in
1 ) ./ADD.sh ;; #call ADD program to perform addition operation
2 ) ./SUB.sh ;; #call SUB program to perform subtraction operation
3 ) ./MULT.sh ;; #call MULT program to perform multiplication operation
4 ) ./DIV.sh ;; #call DIV program to perform division operation
5 ) tput clear ; exit 0 ;;
* ) ./ERROR.sh 20 10 #call ERROR program due to invalid input
tput cup 20 1
tput ed
flag=1
esac
done
exit 0

###################################################################################
########################
###ADD.sh
###program to accept two floating point numbers, add them, and display result

answer=y
while [ "$answer" = y ]
do
tput clear
tput cup 5 15
echo -e "ADDITION"
echo -e "Enter two decimal numbers to add (ex: 3.4 1.2)"
read -p "Enter the first decimal number: " num1
if [[ $num1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
read -p "Enter the second decimal number: " num2
if [[ $num2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
echo -e "The sum of $num1 and $num2 is: $(echo "$num1 + $num2" | bc
-l)"
else
echo -e "Invalid input. Please input two decimal numbers."
fi
else
echo -e "Invalid input. Please input two decimal numbers."
fi

echo -e "Would you like to perform another addition operation? Enter (Y)
for yes or
(N) for no: \b\c"
read answer
case $answer in
[Yy]* ) answer=y ;;
* ) answer=n ;;
esac
done
exit 0

###################################################################################
#######################
###SUB.sh
###program to accept to floating point numbers, subtract one from the other, and
display re
sult

answer=y
while [ "$answer" = y ]
do
tput clear
tput cup 5 15
echo -e "SUBTRACTION"
echo -e "Enter two decimal numbers to subtract (ex: 3.4 1.2)"
read -p "Enter the first decimal number: " num1
if [[ $num1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
read -p "Enter the second decimal number: " num2
if [[ $num3 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
echo -e "The difference between $num1 and $num2 is: $(echo "$num1 -
$num2" | bc -l)"
else
echo -e "Invalid input. Please input two decimal numbers."
fi
else
echo -e "Invalid input. Please input two decimal numbers."
fi
echo -e "Would you like to perform another subtraction operation? Enter (Y) for yes
or (N) for no: \b\c"
read answer
case $answer in
[Yy]* ) answer=y ;;
* ) answer=n ;;
esac
done
exit 0

###################################################################################
########################
###MULT.sh
###program to accept to floating point numbers, multiply them, and display result

answer=y
while [ "$answer" = y ]
do
tput clear
tput cup 5 15
echo -e "MULTIPLICATION"
echo -e "Enter two decimal numbers to add (ex - 3.4 1.2)"
read -p "Enter the first decimal number: " num1
if [[ $num1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
read -p "Enter the second decimal number: " num2
if [[ $num2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
echo -e "The product of $num1 and $num2 is: $(echo "$num1 * $num2"
| bc -l)"
else
echo -e "Invalid input. Please enter two decimal numbers."
fi
else
echo -e "Invalid input. Please enter two decimal numbers."
fi
echo -e "Would you like to perform another multiplication operation? Enter (Y) for
yes or (N) for no: \b\c"
read answer
case $answer in
[Yy]* ) answer=y ;;
* ) answer=n ;;
esac
done
exit 0

###################################################################################
########################
###DIV.sh
###program to accept two floating integers, divide them, and display result

answer=y
while [ "$answer" = y ]
do
tput clear
tput cup 5 15
echo -e "DIVISION"
echo -e "Enter two decimal numbers to divide (ex: 3.4 1.2)"
read -p "Enter the first decimal number: " num1
if [[ $num1 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
read -p "Enter the second decimal number: " num2
if [[ $num2 =~ ^[+-]?[0-9]*\.?[0-9]+$ ]]
then
SCALE=2
echo -e "The quotient of $num1 and $num2 is: $(echo
"$num1 / $num2"
| bc -l)"
else
echo -e "Invalid input. Please input two decimal numbers."
fi
else
echo -e "Invalid input. Please input two decimal numbers."
fi
echo -e "Would you like to perform another division operation? Enter (Y) for yes or
(N) for no: \b\c"
read answer
case $answer in
[Yy]* ) answer=y ;;
* ) answer=n ;;
esac
done
exit 0

###################################################################################
########################
###ERROR.sh
###default case if user input is invalid

tput cup $1 $2
echo -e "Invalid input. \nPress any key to continue: \b\c"
read userIn
exit 0

You might also like