PYDK

Equation Parser

Mar 3rd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package org.gamboodle.vsf;
  2.  
  3. public class parser {
  4.        
  5.     public static String equation = "((x^2)+3)";
  6.    
  7.     public static void main(String[] args) {
  8.         //checkForParenthesis(equation);
  9.         System.out.println(checkForParenthesis(equation, 1));
  10.     }
  11.    
  12.     public static String checkForParenthesis(String s, int n) {
  13.         String equationNew = "";
  14.         int openParenth = 0;
  15.         int closedParenth = 0;
  16.         for(int i = 0; i < s.length(); i++) {
  17.             if(s.charAt(i) == '(') {
  18.                 openParenth++;
  19.             }
  20.             if(s.charAt(i) == ')') {
  21.                 closedParenth++;
  22.             }
  23.             if(openParenth == closedParenth) {
  24.                 if(openParenth > n) {
  25.                     equationNew = equationNew.substring(1);
  26.                     equationNew = equationNew.substring(0, equationNew.length()-2);
  27.                     checkForParenthesis(equationNew, (n));
  28.                     break;
  29.                 }
  30.                 if(openParenth == n) {
  31.                     equationNew = equationNew + s.charAt(i);
  32.                     break;
  33.                 }
  34.                 if(openParenth < n) {
  35.                     equationNew = equationNew + s.charAt(i);
  36.                     break;
  37.                 }
  38.             }
  39.             equationNew = equationNew + s.charAt(i);
  40.         }
  41.         return equationNew;
  42.     }
  43.  
  44.     public static String checkForExponants(String s) {
  45.         return "^";
  46.     }
  47.    
  48. }
Add Comment
Please, Sign In to add comment