2013年9月13日星期五

java to write a program to determine whether the expression is legitimate

Such as: [ operators include + - * / - ( minus ) ]
as : x + y, x + y + z + (k + t) * p that is legitimate.
as : x + + y, x + y + (() which is illegal.


How to do ?

I feel so complicated that I can think of judgment criteria are:
1, left and right parentheses same number in brackets
2, two adjacent operator can not
3, two figures can not be adjacent
4, the operator can not be left parenthesis to the left and right can not be a right parenthesis . [If : ( + or + ) ]
5, right bracket can not be in the first position , the left parenthesis not in the final position . [If :) x ( ]
6, one pair of braces can not be empty and must be a valid expression [ such as : ( ) ]

contents inside a pair of parentheses must be legitimate, so that you can use recursion to achieve


I can think of so many, not enough feeling is not supposed to do, this is very complicated.
I do not know this was done without question Please advise Thank you O (∩ _ ∩) O ~


------ Solution ------------------------------------ --------
put forward a regular expression to determine
------ Solution ---------------------- ----------------------
so complicated . By a regular , then it was a relatively high level of job
or regular fit + logic to solve it
------ Solution ----------------------------- ---------------
data structure looks like there is such a problem , it can be , all the figures are placed in a stack , all operators are placed in a stack li . . . Then how to forget ,
------ Solution - -------------------------------------------
you this expression spaces are not allowed strings allowed ? And your expressions letters are numbers do ? Or variable ?
while (s.matches (". * \ \ ([0-9] + ([\ \ + \ \ - \ \ * \ \ /] [0-9] +) + \ \). * " ) ) {
s.replaceAll ("\ \ ([0-9] + ([\ \ + \ \ - \ \ * \ \ /] [0-9] +) + \ \)", " 0 " ) ;
}
return s.marches ("^ [0-9] + ([\ \ + \ \ - \ \ * \ \ /] [0-9] +) + $");
Suppose you the location of all the letters numbers and spaces are allowed , with the above code should be able to .
------ Solution ---------------------------------------- ----
if only brackets , super simple

put a counter

from the expression on the left , every encounter a left parenthesis , counter +1 , did not encounter a right parenthesis , counter -1

1 at any time , the counter can not be negative
2 play after traversing the counter should be 0
------ Solution -------------------------- ------------------
that is super simple, and comes with java javascript engine rhino.

did not hand the environment, pure handwritten, there is something wrong , then fix it yourself :

String expression = "...";
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
// 如果正确的表达式,返回的是个Number类型(比如Double)的对象,否则,可能返回null,或者抛出Exception
engine.eval(expression);

------ For reference only ----------------------------------- ----



allowed , a number, it can be seen as a variable of type int seen this regular expression studies do not really understand Oh Thank you again O (∩ _ ∩) O ~
------ For reference only ---------------------------- -----------

ah have not seen it this way Oh thank slightly O (∩ _ ∩) O ~
--- --- For reference only ---------------------------------------
/ * determine whether the expression specification and automatic correction * /
private boolean check () {
char t, tt = expression.charAt (0);
int sum = 0, add = 0; / / calculate the left and right parentheses to calculate the corrected median
boolean hasNum = false;
StringBuffer s = new StringBuffer (expression);
if (tt == '+' | | tt == '-' | | tt == '.') {
s.insert (0, '0 ');
add + +;
} else
if (tt == ')' | | tt == '*' | | tt == '/') {
return false;
}
for (int i = 0; i t = tt;
if (i! = expression.length () -1) {
tt = expression.charAt (i +1);
} else tt = 'E';
if (t> = '0 '&& t <= '9') {
hasNum = true;
if (tt == '(') {
s.insert (i + add +1, '*');
add + +;
}
} else
if (t == '(') {
sum + +;
if (tt == '.' | | tt == '+' | | tt == '-') {
s.insert (i + add +1, '0 ');
add + +;
} else
if (tt == '*' | | tt == '/' | | tt == ')') {
return false;
}
} else
if (t == ')') {
sum -;
if (tt> = '0 '&& tt <= '9') {
s.insert (i + add +1, '*');
add + +;
} else
if (tt == '.') {
return false;
}
} else
if (t == '+') {
if (tt == '+' | | tt == '-') {
s.deleteCharAt (i + add);
add -;
} else
if (tt == '.') {
s.insert (i + add +1, '0 ');
add + +;
} else
if (tt == '*' | | tt == '/' | | tt == ')') {
return false;
}
} else
if (t == '-') {
if (tt == '+') {
s.replace (i + add, i + add +2, "-");
add -;
} else
if (tt == '-') {
s.replace (i + add, i + add +2, "+");
add -;
} else
if (tt == '.') {
s.insert (i + add +1, '0 ');
add + +;
} else
if (tt == '*' | | tt == '/' | | tt == ')') {
return false;
}
} else
if (t == '*' | | t == '/') {
if (tt == ')' | | tt == '+' | | tt == '-' | | tt == '*' ; | | tt == '/') {
return false;
}
} else
if (t == '.') {
if (tt == '.') {
return false;
} else
if (tt <'0 '| | tt> '9') {
s.insert (i + add +1, '0 ');
add + +;
}
} else return false;
}
if (sum == 0 && hasNum && (expression.indexOf ('(') <= expression.indexOf (')'))) {
expression = s.substring (0);
return true;
} else
return false;
}
The only four arithmetic, you can reference details about

没有评论:

发表评论