Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class parser {
- public static String equation = "((x^2)+3)";
- public static void main(String[] args) {
- //checkForParenthesis(equation);
- System.out.println(checkForParenthesis(equation, 1));
- }
- public static String checkForParenthesis(String s, int n) {
- String equationNew = "";
- int openParenth = 0;
- int closedParenth = 0;
- for(int i = 0; i < s.length(); i++) {
- if(s.charAt(i) == '(') {
- openParenth++;
- }
- if(s.charAt(i) == ')') {
- closedParenth++;
- }
- if(openParenth == closedParenth) {
- if(openParenth > n) {
- equationNew = equationNew.substring(1);
- equationNew = equationNew.substring(0, equationNew.length()-1);
- checkForParenthesis(equationNew, (n-1));
- break;
- }
- if(openParenth == n) {
- equationNew = equationNew + s.charAt(i);
- break;
- }
- if(openParenth < n) {
- equationNew = equationNew + s.charAt(i);
- break;
- }
- }
- equationNew = equationNew + s.charAt(i);
- }
- return equationNew;
- }
- public static String checkForExponants(String s) {
- return "^";
- }
- }
Add Comment
Please, Sign In to add comment