Exercise: Read an arithmetic expression grouped by parentheses and verify if it is syntactically well formed.
Consider:
1. Parentheses close and open correctly, there are pairs of them.
2. Letters should be used as literals (upper or lower case).
3.Review the proper use of operators (which need two operands).
4. Parentheses properly open and close expressions: (a + b) and not (a + ) b.
I already wrote the code, and the only problem I have is that when I give it as input: a+()
it gives as output: La expresion esta escrita de manera correcta
, but it is incorrect, how could I solve the problem ?
Note: The code compiles fine.
public class Prueba {
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
System.out.println("De que tamaño sera su expresion?");
int n = x.nextInt();
LinkedList pila = new LinkedList();
LinkedList pila2 = new LinkedList();
LinkedList pila3 = new LinkedList();
LinkedList pp = new LinkedList();
LinkedList ppo = new LinkedList();
LinkedList pilaC = new LinkedList();
for (int i = 0; i < n; i++) {
System.out.println("Introduzca su elemento");
x.nextLine();
String e = x.nextLine();
pila.push(e);
pila2.push(e);
pila3.push(e);
}
int a = 0, b = 0;
while (!pila.isEmpty()) {
String element = (String) pila.pop();
if ((0 == element.compareTo(")")||0 == element.compareTo("("))
&& !pila.isEmpty()) {
String sim = (String) pila.pop();
if (0 == sim.compareTo("+") || 0 == sim.compareTo("-") || 0
== sim.compareTo("*") || 0 == sim.compareTo("/")) {
a++;
}
}
}
if (a != 0) {
System.out.println("La expresion no esta escrita de manera"+
"correcta");
} else {
while (!pila2.isEmpty()) {
String simbolo = (String) pila2.pop();
if ((0 == simbolo.compareTo("+") || 0 ==
simbolo.compareTo("-") || 0 == simbolo.compareTo("*") || 0 ==
simbolo.compareTo("/")) && !pila2.isEmpty()) {
String el = (String) pila2.pop();
if (0 == el.compareTo("(")||0 == el.compareTo(")") || 0
== el.compareTo("+") || 0 == el.compareTo("-") || 0 ==
el.compareTo("*") || 0 == el.compareTo("/")) {
b++;
}
}
}
if (b != 0) {
System.out.println("La expresion no esta escrita de manera"
+ "correcta :/");
} else {
while (!pila3.isEmpty()) {
String e = (String) pila3.pop();
if (0 == e.compareTo("(") || 0 == e.compareTo(")")) {
pp.push(e);
}
}
while (!pp.isEmpty()) {
ppo.push(pp.pop());
}
int w=0;
while(!ppo.isEmpty() && w!=1){
String s=(String)ppo.pop();
if(0==s.compareTo("(") && !pilaC.isEmpty())
pilaC.pop();
else{
if(0==s.compareTo("(") && pilaC.isEmpty()){
System.out.println("La expresion no esta"+
"escrita de manera correcta ");
w=1;
}
else
pilaC.push(s);
}
}
if(w==1)
System.out.println();
else
{
if(!pilaC.isEmpty())
System.out.println("La expresion no esta escrita de"+
"manera correcta ");
else
System.out.println("La expresion esta escrita de"+
"manera correcta :)");
}
}
}
}
}
Very good, just as I did in my answer to your question related to this topic, I am going to try to show you a code that is simple to understand and handle.
previous comments
Let's forget about all those stacks in the first place, we just need a list . On the other hand, to make it easier to debug, we are going to create a function that allows us to simplify the conditionals.
Concept
What we are going to do is go through our list and analyze the elements in pairs between the current and the previous one . For example: if my current element is a ( , the previous has to be an operator by force +,-,/,* , otherwise the expression is wrong.
Code
code notes
Notice a couple of things about the code:
{}
settings in all the blocks. I recommend that you try to get used to following a good code style. I use curly braces even for 1-line conditionals.x.nextLine();
within thefor
.Input/output example
I hope you understand and serve you.
Solution of the exercise using only batteries :
Note: Like DanielGS's answer, if
input: a+()
,output:"La expresion es correcta"
. And so it must be, because what he is saying is that toa
add"vacio" o () o 0
.