Simple calculator with menu input - Need Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple calculator with menu input - Need Help
Prev   Next
# 1  
Old 01-17-2013
Wrench Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed.

eg. 1 + 1 = or 1 + 1 + 2 + 1 =

Both of these should be possible.

Code:
#!/bin/bash

read -p "what's the first number? " n1
PS3="what's the operation? "
select ans in add subtract multiply divide equals; do
case $ans in 
add) op='+' ; break ;;
subtract) op='-' ; break ;;
multiply) op='*' ; break ;;
divide) op='/' ; break ;;
*) echo "invalid response" ;;
esac
done
read -p "what's the second number? " n2
ans=$(echo "$n1 $op $n2" | bc -l)
printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"

exit 0

This is what I have written so far, but i cannot work out how to make it possible to let the user choose 'equals' or to loop back round to enter another operation. Any ideas what I can do to my code here? I have been stuck on this all day.

I dont want the user to enter the equation themselves, i want them to be choosing from a list.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD).

ADE is a UNIX environment for the ancient AMIGA A1200. By default this does NOT have the 'bc' command line calculator. Although I did a DEMO code to create a C source and compile it under python 1.4.0 and ADE using ksh88 and the gcc of the day, I decided to create this baby that requires no Python... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Help with 'select' for menu input

A lot of my scripting makes use of the 'select' command to create menu driven input. A typical example of how I use it is as: somevar='' PS3='Select one: ' while ]; do select somevar in $(sqlplus -s $dbuser/$dbpw@mydb <<EOF set echo off feedback off verify off... (7 Replies)
Discussion started by: edstevens
7 Replies

3. Homework & Coursework Questions

Simple Calculator

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: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

4. UNIX for Dummies Questions & Answers

Simple bash script menu

Dear Sir, May I know how do I go about adding the following feature into the script below: When user enter values other than 1,2,3,4, a) Message “Wrong entry !!! Pls select 1,2,3 or 4” is displayed b) The screen is cleared again and the menu is displayed. #!/bin/bash clear var=1... (2 Replies)
Discussion started by: fusetrips
2 Replies

5. Shell Programming and Scripting

Two columns output in simple case menu?

EDIT : System used : Any Linux distribution. Hello everyone, I m having quite a headache trying to figure out why I m having a 2 columns output in the following code : #!/bin/ksh menu_rotation() { #Variables CHOIX1="Rotation 1" CHOIX2="Rotation 2" CHOIX3="Rotation 3" ... (11 Replies)
Discussion started by: Sekullos
11 Replies

6. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

7. Shell Programming and Scripting

Perl simple text menu with options

Hopefully I'm in the right place. Im new to the forums and linux! I'm looking to add a menu to my perl hangman game i have created. The menu will use user input for the desired option and then perform the operation indicated. I would like something along the lines of: Welcome to Hangman... (1 Reply)
Discussion started by: jahburmski
1 Replies

8. UNIX for Dummies Questions & Answers

Input A Menu Selection In A Variable

hey all, me again....having a problem with my code, where I essentially am trying to show a menu, have the user select an option (from 1 to 5), then display which selection they picked... #!/bin/bash # A LIST OF THE ERROR MESSAGES AND THE PROPER SYNTAX: error_0="Correct amount of... (1 Reply)
Discussion started by: SoVi3t
1 Replies

9. Shell Programming and Scripting

help with scripting a simple menu

Hi there. I'm trying to teach myself UNIX but the book I bought is a bit confusing. I'm trying out this exercise and I think I'm on the right track, but I'd appreciate any suggestions on how to improve what I have so far. Also, I'm not clear on how to use the read command to utilize the user's... (3 Replies)
Discussion started by: Jsmith
3 Replies

10. Shell Programming and Scripting

Simple Menu and coding

I am very new to Unix and don't know much about it. I've been trying to create a menu and what I don't understand is how to execute a command once a user makes a selection. I have the menu set up. In fact, the following is the code that I have thus far: #! /bin/csh # This is the UNIX menu... (0 Replies)
Discussion started by: sinjin
0 Replies
Login or Register to Ask a Question
IC(1)							      General Commands Manual							     IC(1)

NAME
ic - integer calculator SYNOPSIS
ic [expression] EXAMPLES
ic # Start the calculator ic 250 300+ # Start calculator with 550 on the stack DESCRIPTION
Ic is a simple RPN (Reverse Polish Notation) calculator, used for small calculations and base conversions. All calculations are done using 32 bit integers. The standard input is usually a keyboard and the standard output requires a device with a 'termcap' entry. The program starts by interpreting any <args> as commands, where the separation between arguments is considered to be the same as the ENTER key. For example, "ic 692 784+ After reading the arguments input is from the keyboard. Stack Operations The operation of this program is similar to an RPN calculator. A six level stack is used. The ENTER key pushes the stack up one level. For example, '12+5' is entered as '12 ENTER 5 +". The top two entries on the stack are exchanged by the x command, and the stack is rolled down one (popped) by the p key. The top of the stack may be cleared by pressing the back-space key. The whole stack and the registers are initialized by a z. Numeric Entry The input and output bases are initially decimal, but they may be changed using the i and o commands. The i command changes both bases, but the o command changes just the output base. These commands take a one character argument of h, d, o or b to change to Hexadecimal, Deci- mal, Octal or Binary. While the input base is hexadecimal the letters a through f are used to represent the decimal values 10 through 15. When the input base is decimal: multiply, divide and remainder are signed, otherwise they are performed unsigned. The output base may also be changed to ASCII (a), this causes the least significant 7 bits of a value to be displayed as a character. To input an ASCII value the translate (t) command may be used, it accepts one character as its argument. Calculations The arithmetic operations supported are: Negate ('.'), Add ('+'), Subtract ('-'), Multiply ('*'), Divide ('/'), and Remainder ('%'). The logical (Boolean) operations available are: NOT ('~'), AND ('&'), OR ('|'), and EXCLUSIVE-OR ('^'). After one of these operations the last top of stack value is saved. It may be restored by pressing l (L). Saving Results Ten temporary registers are available. The Store (s) command followed by a digit ('0'..'9') will copy the top of the stack to the specified register. The Recall (r) command pushes the contents of a register onto the top of the stack. If the Store command is followed by a '+' preceding the digit, then the top of the stack will be added to the specified 'accumulator' reg- ister. Values may also be written to a file. The w command writes the top of the stack, using the current output base, to a file called 'pad' in the current directory. If the user does not have write access to the current directory then the file /tmp/pad_$USER is used as the scratch pad. The scratch pad file is erased on the first use of the w command within each new invocation of 'ic'. Miscellaneous The Quit (q) key causes an immediate exit. The m command temporarily leaves ic by invoking the shell as a sub-process. For help while using ic, hit the h key. If an erroneous key is pressed the bell will sound. Command Summary Note that many commands have an alternative key-code available on the extended AT keyboard. This aids entry by including most commands on the right side of the keyboard. ENTER Enter (push up) BS (DEL) Clear top of stack h Help i Input base (h, d, o, b) l (PGDN) Last top of stack m MINIX shell o Output base (h, d, o, b, a) p (DOWN) Pop stack (roll down) q (END) Quit r (LEFT) Recall (0-9) s (RIGHT) Store [+] (0-9) t Translate (char) w (PGUP) Write top of stack to scratch pad x (UP) Exchange top of stack z (HOME) Zero all state . Change sign + (+) Add - (-) Subtract * Multiply / Divide % (sh/5) Remainder (tilde) Not & And | Or ^ Exclusive-or Author Ic was written by Terrence W. Holm. IC(1)