permaculture playing cards*
The moose likes Java in General and the fly likes Using a Char as an operator in a sum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using a Char as an operator in a sum" Watch "Using a Char as an operator in a sum" New topic
Author

Using a Char as an operator in a sum

Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
Hi again, it's me, with another stupid problem.
I'm kinda bored so i'm making a calculator in java however, I want to carry out sums replacing an operator such as + with a char with same value. Is this possible?
for example, say i have

char op ='+';

I could do.
int a = b + c;
What I want to do is.
int a = b op c;
and that would mean the same thing

Any ideas?
Reagrds
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Writing a calculator in Java, and using an '+' as operator seems to be a legitimate Approach, while defining a Class Operator which hides the concrete implementation seems to be more OO.

What's confusing me is your example:


Will the user of your calculator write such a line?
Should he really use the keyword 'int' and shall it have the same meaning as in Java?
Or will you compile userinput with java?
Shall that line be compiled by java?

There is no operator-overloading as in c++, and even in c++ you may not overwrite the operator-meaning for the build-in type int.

And last question: shall '+' be an operator or an character?

Yes - the user gives an input, which is a char or a String or something, which is interpreted as operator by your calculator.


http://home.arcor.de/hirnstrom/bewerbung
Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
Basically the user can enter various operators. + * - etc
so, they may do. 7+7
To evaluate the expression the users enter I get the values and put transfer them to another method, ie 7 and 7. and then store + in a public static char.

so basically using the example above, i now have a method with two ints in it, 7 and 7. The user may have selected any operator, from + - *. To operator value would be stored in the char.
so from the method I want to do
int result=7 op 7. Obviouslly I cant do that cause op is a char and wont be stored to an int. I just want the value of char, ie, +, so that expression would turn into int result =7+7
But i dont know how this would be done
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
One thing to remember, a char is an integral value. It is just a number. Look up the table of ascii values. '+' is equivilent to the decimal number 43. What do you mean that op is a char? That is technically a string of character values, not a character.

I am not sure exactly what you are asking.

Do you mean if a user types in 7+7, you want the answer returned? You can do that in a single method, or one for each type of operator, taking expression as a string, extracting the numbers and converting them to integral values, then pull out the operator and calling the appropriate method based on its value. There are few reasons for explicitly using the integral value of + though.

If you are looking for operator overloading, you are using the wrong language, but you can approximate it. If op always means +, then you can approximate operator overloading, by a similar method in the second paragraph.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
hi,

you see that everyone is a bit confused about your exact question :-)

from what i understood:
- the user enters 1 line of text, "12+33" would be one, "3343434*12" another option. maybe also 3 lines, which would be easier to parse for you "12", "+", "33"
- your question goes about evaluation the expression. you are looking for a one-liner that calculates the result based on the parsed terms "value1 op value 2"

my answer:
no, this is not possible. to my best knowledge you cannot evalute a expression in that way. you need a switch / if / strategy pattern to decide which operation to execute.

was this your question?

jan
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
Your best bet to writing a command line calculator would probably be to write a parser, which was stated before. Since the lines will likely be short, a top-down (infinite recursion) approach would be probably best, or LL(1). LR(0) would also work, but it's much tricker to design and implement, especially if you have little to no experience in parsing.

It may be overkill to do a full lexer and parser for a simple calculator program. In fact, a ver powerful, full functioning calculator can be written with nothing but lex (a lexer creator program), but it is definitely worth the time to learn how to do it. There are plenty of sites with information on LL/LR parsing, and even more on simple lexing algorithms.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using a Char as an operator in a sum
 
Similar Threads
Infix to postfix algorithm
Shifting
++ promotion for byte,short char?
Switch Statements
help needed