VB icon

zCalcEvalExpression

Email
Submitted on: 11/12/2016 10:14:53 PM
By: Zehel  
Level: Advanced
User Rating: Unrated
Compatibility: Java (JDK 1.5)
Views: 587
author picture
 
     Evaluates a mathematical expression and returns the double value.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: zCalcEvalExpression
// Description:Evaluates a mathematical expression and returns the double value.
// By: Zehel
//**************************************

//••••----------••••
private Double zCalcEvalExpression(final String[] inStrRy, int[] inIntRy) {
// The input to this method should be the math expression
// in tokens, Example: {"9×3","3÷-2","-2+4","4×etc..."}; I
// have shared that method already here at planet source code
// titled zCalcSplit44 ---- The inIntRy the operators, ×=1,÷=2,+=3,-=4
// This method and submethods will return a valid double from those
// parameters without fail, enjoy, zandidigital1@gmail.com
double retDbl = 0.0; double[] dblRy = new double[2];
int len = inIntRy.length, dx = 0, inDx = 0;
Boolean isStrt = true, hasMultOrDiv = false, multOrDiv = false, multOrDivTrp = true;
StringBuilder sb1 = new StringBuilder(); ArrayList<String> stckStr = new ArrayList<String>();
while(dx < len) { stckStr.add(inStrRy[dx]); dx++;} dx = 0;
ArrayList<Integer> stckInt = new ArrayList<Integer>();
while(dx < len) { stckInt.add(inIntRy[dx]); dx++;} dx = 0;
while(dx < len) { if(isStrt) { isStrt = false; while(inDx < len) {
if(stckInt.get(inDx) == 1 || stckInt.get(inDx) == 2) {
dblRy = extrctToDblRy(stckStr.get(inDx));
if(stckInt.get(inDx) == 1) { retDbl = dblRy[0]*dblRy[1];}
else { retDbl = dblRy[0]/dblRy[1];} if(inDx+1 == stckStr.size()) {
sb1.append(extrct(stckStr.get(inDx-1).toCharArray(), false));
stckStr.set(inDx-1, sb1.toString() + String.valueOf(retDbl));
stckInt.set(inDx-1, ncOprtr); stckStr.remove(inDx); stckInt.remove(inDx);}
else { sb1.append(extrct(stckStr.get(inDx+1).toCharArray(), true));
stckStr.set(inDx, String.valueOf(retDbl) + sb1.toString());
stckStr.remove(inDx+1); stckInt.set(inDx, ncOprtr); stckInt.remove(inDx+1);}
hasMultOrDiv = true; len = stckInt.size(); inDx = len;} inDx++;}}
else { if(hasMultOrDiv) { if(multOrDivTrp) { multOrDiv = false;
while(dx < len) { if(stckInt.get(dx) == 1 || stckInt.get(dx) == 2) {
multOrDiv = true; dx = len;} dx++;} if(!multOrDiv) { multOrDivTrp = false;}}
if(multOrDiv) { sb1 = new StringBuilder(); while(inDx < len) {
if(stckInt.get(inDx) == 1 || stckInt.get(inDx) == 2) {
dblRy = extrctToDblRy(stckStr.get(inDx));
if(stckInt.get(inDx) == 1) { retDbl = dblRy[0]*dblRy[1];}
else { retDbl = dblRy[0]/dblRy[1];} if(inDx != 0) {
sb1.append(extrct(stckStr.get(inDx-1).toCharArray(), false));
stckStr.set(inDx-1, sb1.toString() + String.valueOf(retDbl));
stckInt.set(inDx-1, ncOprtr); stckStr.remove(inDx); stckInt.remove(inDx);}
else { sb1.append(extrct(stckStr.get(inDx+1).toCharArray(), true));
stckStr.set(inDx, String.valueOf(retDbl) + sb1.toString());
stckStr.remove(inDx+1); stckInt.set(inDx, ncOprtr); stckInt.remove(inDx+1);}
hasMultOrDiv = true; len = stckInt.size(); inDx = len;} inDx++;}}
else { String[] fnlInStrRy = new String[stckStr.size()]; dx = 0;
while(dx < fnlInStrRy.length) { fnlInStrRy[dx] = stckStr.get(dx); dx++;}
return evaltAS(fnlInStrRy);}} else { retDbl = evaltAS(inStrRy); dx = len;}}
if(len == 1) { dblRy = extrctToDblRy(stckStr.get(0)); if(stckInt.get(0) == 1) { retDbl = dblRy[0]*dblRy[1];}
else if(stckInt.get(0) == 2) { retDbl = dblRy[0]/dblRy[1];} else if(stckInt.get(0) == 3) { retDbl = dblRy[0]+dblRy[1];}
else if(stckInt.get(0) == 4) { retDbl = dblRy[0]-dblRy[1];} break;} else {
dx = 0; inDx = 0;}} return retDbl;}
//••••----------••••
private String extrct(final char[] chrRy, final Boolean isR) {
StringBuilder sb = new StringBuilder(); final int len = chrRy.length;
Boolean oprtrTrp = false; int dx = 1;
if(isR) { while(dx < len) { if(!oprtrTrp) {
if(chrRy[dx] == '×' || chrRy[dx] == '÷' || chrRy[dx] == '+' || chrRy[dx] == '-') {
if(chrRy[dx] == '×') { ncOprtr = 1;} else if(chrRy[dx] == '÷') { ncOprtr = 2;}
else if(chrRy[dx] == '+') { ncOprtr = 3;} else if(chrRy[dx] == '-') { ncOprtr = 4;}
sb.append(String.valueOf(chrRy[dx])); oprtrTrp = true;}}
else { sb.append(String.valueOf(chrRy[dx]));} dx++;}}
else { sb.append(chrRy[0]); dx = 1; while(dx < len) {
if(chrRy[dx] == '×' || chrRy[dx] == '÷' || chrRy[dx] == '+' || chrRy[dx] == '-') {
if(chrRy[dx] == '×') { ncOprtr = 1;} else if(chrRy[dx] == '÷') { ncOprtr = 2;}
else if(chrRy[dx] == '+') { ncOprtr = 3;} else if(chrRy[dx] == '-') { ncOprtr = 4;}
sb.append(String.valueOf(chrRy[dx])); break;}
else { sb.append(String.valueOf(chrRy[dx]));} dx++;}} return sb.toString();}
//••••----------••••
private Double evaltAS(final String[] strRy) {
double retDbl = 0.0; final int len = strRy.length;
int inDx = 0; Boolean isAS_Strt = true; double[] dblRy = new double[2];
final String[] asStrRy = chkVldEqtStrRy(strRy); final int[] asIntRy = chkVldEqtIntRy(asStrRy);
while(inDx < len) { dblRy = extrctToDblRy(asStrRy[inDx]);
if(isAS_Strt) { isAS_Strt = false; if(asIntRy[inDx] == 3) { retDbl = dblRy[0]+dblRy[1];}
else { retDbl = dblRy[0]-dblRy[1];}} else { if(asIntRy[inDx] == 3) { retDbl = retDbl+dblRy[1];}
else { retDbl = retDbl-dblRy[1];}} inDx++;} return retDbl;}
//••••----------••••
private int[] chkVldEqtIntRy(final String[] eqtStrRy) {
int[] retIntRy = new int[eqtStrRy.length]; final int len = retIntRy.length; Boolean strt = true;
int dx = 0, inDx = 0; char[] chrRy = null; while(dx < len) { chrRy = eqtStrRy[dx].toCharArray();
if(strt) { strt = false; inDx = 1;} else { inDx = 0;} while(inDx < chrRy.length) {
if(chrRy[inDx] == '×') { retIntRy[dx] = 1; inDx = chrRy.length;}
else if(chrRy[inDx] == '÷') { retIntRy[dx] = 2; inDx = chrRy.length;}
else if(chrRy[inDx] == '+') { retIntRy[dx] = 3; inDx = chrRy.length;} else if(chrRy[inDx] == '-') {
retIntRy[dx] = 4; inDx = chrRy.length;} inDx++;} dx++;} return retIntRy;}
//••••----------••••
private String[] chkVldEqtStrRy(final String[] eqtStrRy) {
StringBuilder sb = new StringBuilder(String.valueOf(eqtStrRy[0].charAt(0)));
String[] retStrRy = new String[eqtStrRy.length]; final int len = retStrRy.length;
int dx = 1, inDx = 0; char[] chrRy = null; chrRy = eqtStrRy[0].toCharArray();
while(dx < chrRy.length) { if(chrRy[dx] == '+' || chrRy[dx] == '-') { if(chrRy[dx+1] == '+' || chrRy[dx+1] == '-') {
sb.append(String.valueOf(chrRy[dx+1])); dx++;} else { sb.append(String.valueOf(chrRy[dx]));}}
else { sb.append(String.valueOf(chrRy[dx]));} dx++;} dx = 1; retStrRy[0] = sb.toString();
if(retStrRy.length == 1) { return retStrRy;} sb = new StringBuilder(); while(dx < len) {
chrRy = eqtStrRy[dx].toCharArray(); inDx = 0; while(inDx < chrRy.length) {
if(chrRy[inDx] == '+' || chrRy[inDx] == '-') { if(inDx > 0) { if(chrRy[inDx+1] == '+' || chrRy[inDx+1] == '-') {
sb.append(String.valueOf(chrRy[inDx+1])); inDx++;} else { sb.append(String.valueOf(chrRy[inDx]));}}}
else { sb.append(String.valueOf(chrRy[inDx]));} inDx++;} retStrRy[dx] = sb.toString(); sb = new StringBuilder(); dx++;} return retStrRy;}
//••••----------••••
private double[] extrctToDblRy(final String eqt) {
StringBuilder sb1 = new StringBuilder(String.valueOf(eqt.charAt(0)));
double[] retDblRy = new double[2]; final char[] chrRy = eqt.toCharArray();
final int len = chrRy.length; int dx = 1, ryDx = 0;
while(dx < len) { if(ryDx == 0) {
if(chrRy[dx] == '×' || chrRy[dx] == '÷' || chrRy[dx] == '+' || chrRy[dx] == '-') {
retDblRy[ryDx] = Double.parseDouble(sb1.toString()); sb1 = new StringBuilder();
ryDx++;} else { sb1.append(String.valueOf(chrRy[dx]));}} else {
sb1.append(String.valueOf(chrRy[dx]));} dx++;}
retDblRy[ryDx] = Double.parseDouble(sb1.toString()); return retDblRy;}
//••••----------••••
private String chkEqtStr(final String eqtStr) {
final char[] chrRy = eqtStr.toCharArray(); StringBuilder sb1 = new StringBuilder();
final int len = chrRy.length; int dx = 0; chkEqtStr = 0; char lstChr = '*';
Boolean isStrt = true, isOprtr = false, oprtrTrp = true;
while(dx < len) { if(isStrt) { isStrt = false;
if(chrRy[dx] == '÷' || chrRy[dx] == '×') { chkEqtStr = 1; return "";}
else { if(chrRy[dx] == '.' || chrRy[dx] == '-' || chrRy[dx] == '0' || chrRy[dx] == '1' || chrRy[dx] == '2' || chrRy[dx] == '3' || chrRy[dx] == '4' || chrRy[dx] == '5' || chrRy[dx] == '6' || chrRy[dx] == '7' || chrRy[dx] == '8' || chrRy[dx] == '9') {
sb1.append(String.valueOf(chrRy[dx]));}}} else { if(chrRy[dx] == '÷' || chrRy[dx] == '×' || chrRy[dx] == '+' || chrRy[dx] == '-') {
if(oprtrTrp) { if(lstChr == '.' || lstChr == '0' || lstChr == '1' || lstChr == '2' || lstChr == '3' || lstChr == '4' || lstChr == '5' || lstChr == '6' || lstChr == '7' || lstChr == '8' || lstChr == '9') {
sb1.append(String.valueOf(chrRy[dx])); isOprtr = true;}
else { chkEqtStr = 2; return "";}} else { chkEqtStr = 2; return "";}}
else { if(isOprtr) { if(chrRy[dx] == '.' || chrRy[dx] == '+' || chrRy[dx] == '-' || chrRy[dx] == '0' || chrRy[dx] == '1' || chrRy[dx] == '2' || chrRy[dx] == '3' || chrRy[dx] == '4' || chrRy[dx] == '5' || chrRy[dx] == '6' || chrRy[dx] == '7' || chrRy[dx] == '8' || chrRy[dx] == '9') {
if(chrRy[dx] == '+') { isOprtr = false; oprtrTrp = false;}
else { sb1.append(String.valueOf(chrRy[dx])); isOprtr = false; oprtrTrp = false;}}
else { chkEqtStr = 2; return "";}} else { sb1.append(String.valueOf(chrRy[dx]));}}}
lstChr = chrRy[dx]; dx++;} return sb1.toString();}
//••••----------••••


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/13/2016 7:38:43 PMZehel

To have it work completely correct on all expressions you will have to update the next token expression(right) after a multiply or divide before remove of expression index and operator index. Example: if(inDx 1 != stckStr.size()) { sb1 = new StringBuilder(extrct(stckStr.get(inDx 1).toCharArray(), true)); stckStr.set(inDx 1, String.valueOf(retDbl) sb1.toString());} ...then removes. There others of doing this, it is up to you, you have the outline of doing eval correctly
(If this comment was disrespectful, please report it.)

 
11/13/2016 8:28:35 PMZehel

Also, say it is reading expression -20-2÷2×1000+8, on 1rst pass would be fine, it would see -80-2÷2×1000+8, however second the first list index would then be [-80--1]
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.