![]() |
|
#1
|
|||
|
|||
how to write a simple calculator code? Help!Hey, guys,
I am now have to write a java programm to implement a simple calculator of basic operations(addition, subtraction, multiplication and division) on integers. The user may enter a string comprising an operand, a binary operator, and a second operand, with any number of space characters between them. There may be leading spaces and trailing spaces in the input. If an invalid input is encountered, a message "Error in expression" is displayed and the calculation is skipped. Otherwise, the respective operation is carried out, and the result displayed. The user then enters another string. To end the calculation, the user enters 'q'. I also have to handle division-by-zero error so that your program does not crash when it happens. The operand is an integer. Examples of valid operands are 23, 0, -123, but not 23.5 (not an integer), 51.0 (not an integer), +123 (unary plus not accepted). Anyone has idea about how to write this program?? Below , i attached a partial code for this program. Hope someone can help me with this program. Thanks a lot! |
|
#2
|
||||
|
||||
Re: how to write a simple calculator code? Help!Senior members might not reply to this thread if you:
We encourage you to post again once you've done that. __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
Re: how to write a simple calculator code? Help!i did a program almost exactly like this in my datastructures class...
except all we had to use is digits 0-9... if i were you, i would definately use StringTokenizer... you can set the delimiter to "+-*/".. this mean when you call JAVA Code:
it not only accepts strings(substrings) separated by space characters, but also any string separated by +,-,/ and *. so what you can do is have a string that is called operators... that is set up like so.. JAVA Code:
and when you call StringTokenizer.nextToken() you know you're going to get a integer or a operator(+.-,/,*). so when you want to decide if it is a int or a operator try the following... JAVA Code:
and if you have to paranthesis the expression.... just iterate through the string a see if the number of '(' and ')' are equal.. hope this stuff helps a lil... the stringtokenizer should make it a lot easier on you... and if you want to know if a certain char is a digit... use Character.isDigit(char a); Justin to use StringTokenizer.. import java.util.StringTokenizer; |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Starting in the business? Time to write a Business Plan ! | WEBDOMAIN.com | Web Hosting Forum | 2 | 01-Aug-2006 04:12 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| How to write simple program like ........? | laputa9000 | C++ Forum | 3 | 29-Oct-2003 12:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The