Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you do not use the template your post may be deleted! Special homework rules apply here.

Creating a calculator with condition

Homework & Coursework Questions


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-20-2017   -   Original Discussion by mugiboya
mugiboya's Unix or Linux Image
mugiboya mugiboya is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 20 November 2017, 4:50 PM EST
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Creating a calculator with condition

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Create a calculator application in your home folder called itncacl that will perform the following applications:

Add two numbers
Subtract two numbers
Multiply two numbers
Divide two numbers
You cannot divide by zero, so create a condition that checks for and prevents a that condition.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):


Code:
#!/bin/bash

clear


echo "Enter the first number:"
read n1

echo "Choose an operation:"
echo "1. add"
echo "2. subtract"
echo "3. multiply"
echo "4. divide"
read opr

echo "Enter the second number:"
read n2

if [ $opr = "1" ]
   then
      echo $((n1+n2))
elif [ $opr = "2" ]
   then
      echo $((n1-n2))
elif [ $opr = "3" ]
   then
      echo $((n1*n2))
elif [ $opr = "4" ]
   then
       echo $((n1/n2))
fi
exit 0

My only question is how do I add to my code so that when I try to divide by zero it will show Cannot divide by zero or invalid? Thanks!

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Northern Virginia Comm College, VA, USA, Saleh, ITN171

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by Scott; 11-20-2017 at 04:07 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 11-20-2017   -   Original Discussion by mugiboya
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 20 May 2018, 3:57 AM EDT
Posts: 9,060
Thanks: 398
Thanked 1,279 Times in 1,082 Posts
I would think the simplest way to handle this would be to add an additional condition inside your pre-existing condition (that is not related to Obama Care Linux) to test if "n2" is 0 (zero) before you attempt to divide by it:



Code:
elif [ $opr = "4" ]
   then
     ... test if n2 is zero, if not, do this...
       echo $((n1/n2))
    ... else do this
fi

e.g.


Code:
...
elif [ $opr = "4" ]
   then
     if [ "$n2" != 0 ]; then # for numbers, normally, you would use "-ne" (not equal to) in place of "!=",  but you didn't sanitise the input
       echo $((n1/n2))
     else
       echo "Divide by zero"
     fi
fi
...

If the intention is not to show that you are dividing by zero, but to prevent it doing so altogether, you can amend the existing elif that handles that case to also check that "n2" is not zero. Talking of which, it may be better (simpler) to use a "case" statement instead of "if" - ifs can get a bit clumsy looking, when there's lots of elifs going on.
The Following User Says Thank You to Scott For This Useful Post:
mugiboya (11-20-2017)
Sponsored Links
    #3  
Old Unix and Linux 11-20-2017   -   Original Discussion by mugiboya
mugiboya's Unix or Linux Image
mugiboya mugiboya is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 20 November 2017, 4:50 PM EST
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Made it work! Thanks!
    #4  
Old Unix and Linux 11-21-2017   -   Original Discussion by mugiboya
rovf's Unix or Linux Image
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 18 May 2018, 2:47 AM EDT
Posts: 221
Thanks: 31
Thanked 48 Times in 42 Posts
Scotts solution has the disadvantage that, if someone would enter 00 as the second number, you would still get a division by zero.

In general, I prefer testing numerical values in numerical context, for instance



Code:
if (( n2 == 0 ))
then
  ...
else
  ...
fi

The Following User Says Thank You to rovf For This Useful Post:
Scott (11-21-2017)
Sponsored Links
    #5  
Old Unix and Linux 02-22-2018   -   Original Discussion by mugiboya
florachavezz's Unix or Linux Image
florachavezz florachavezz is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 22 February 2018, 3:25 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How about SED commands? Will they work for such a task? I should extract records based on a particular condition in my project.
Sponsored Links
    #6  
Old Unix and Linux 02-22-2018   -   Original Discussion by mugiboya
rovf's Unix or Linux Image
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 18 May 2018, 2:47 AM EDT
Posts: 221
Thanks: 31
Thanked 48 Times in 42 Posts
Quote:
Originally Posted by florachavezz View Post
How about SED commands? Will they work for such a task? I should extract records based on a particular condition in my project.
Sorry, I don't quite understand the point here. In your posting, you wrote: My only question is how do I add to my code so that when I try to divide by zero it will show Cannot divide by zero or invalid? I don't see how this relates to record extraction.

If you have questions about other subjects, please create separate posts.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Creating a calculator with condition mugiboya UNIX for Beginners Questions & Answers 1 11-20-2017 04:00 PM
Creating a condition on a bash script germany1517 Shell Programming and Scripting 3 10-17-2017 04:07 AM
redirect stdout echo command in condition A run in condition B jao_madn Shell Programming and Scripting 3 11-02-2011 09:01 PM
redirecting output and creating condition for while loop. mrwatkin Shell Programming and Scripting 2 08-06-2009 10:37 AM
calculator jdougy UNIX for Dummies Questions & Answers 2 01-15-2009 02:26 AM



All times are GMT -4. The time now is 04:21 PM.