How to make the program recognize ' x ' as multiplication?

Asked

Viewed 243 times

3

It has how to treat x as *, because the user will type x to multiply a number, it would be like the program to recognize x as multiplication via the code?

example double b = 5 x 5;

  • 3

    When you say "user" do you mean the person using the system you created in Java or yourself programming in Java? It is unclear if you intend to change the * for x when programming (i.e., in code) or evaluating the mathematical expression typed by the user in a UI. If this is the second case, Dé uma olhada neste outra pergunta: http://answall.com/questions/51324/como-calcular-express%C3%A3o-matematicas-numa-string

  • 1

    Not in the Java code itself. But in its data entry, of course, it depends on how you’re handling it. Edit the question by posting your code.

  • even the most layman uses *, it makes no sense to think that he will type X...

2 answers

6


No. The multiplication operator in Java is *. However, you can do this for the end user.

For example, he can even type 5 x 5 as a multiplication, hence the program takes the string and replaces the x for * and mathematically evaluate the expression resulting from the substitution.

Why is it so?

Let’s assume the following mathematical instructions in a Java code:

int x = 10;
int a = 10 * x; //Compila corretamente, com a recebendo o valor 100;
int b = 10 x x; //Como deve ser entendido cada x? Um é multiplicação e outro é identificador?
                //Duas multiplicações? 
                //Dois identificadores?

The language must be uniform and precise. Otherwise, compromises occur and nobody knows why the hell the code doesn’t do what should be done.

How to solve this problem?

When you receive the user input, you can change it without their knowledge and display the correct result with an expression other than their own, provided that the expression used to calculate is equivalent to the one the user gave to the program.

Example: Calculator with graphical interface

Let’s assume a traditional calculator, with numbers from 0 to 9 and only the multiplication operator (to simplify).

Each number puts its value on the display and the multiplication button puts the letter x. What the button of = should do?

The algorithm would be:

  1. Retrieve the string that the user created and assign to a variable;
  2. Replace all the x of the variable by *. Easily done with replaceAll, and note that it is not necessary for this new string to be shown to the user;
  3. Make the Parsing of the numbers that each * surrounds and multiply;
  4. Replace in string with result;
  5. String only has numbers? Show user;
  6. Otherwise, go back to step 3.

2

By the question tag, I will assume you are implementing in Java.

There are several ways to do this, the one I’m going to suggest is just one of them, not necessarily the best or simplest:

  1. Do the expression Parsing and convert the symbols that the user understands to the symbols that the language understands. Ex: from "b = 5 x 5" to "b = 5 * 5".
  2. Pass the processed string to a library that does this for you, for example, the Beanshell

A hypothetical example using Beanshell would be:

Interpreter interp = new Interpreter();
interp.eval("b = 5 * 5");
System.out.println("resultado: b="+interpreter.get("b"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.