VB icon

Split a calculation into calculation tokens

Email
Submitted on: 11/8/2016 10:54:37 PM
By: Zehel  
Level: Advanced
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 1443
author picture
 
     Splits a calculation into calculation tokens
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Split a calculation into calculation tokens
// Description:Splits a calculation into calculation tokens
// By: Zehel
//**************************************

//••••----------••••
private String[] zCalcSplit44(final char[] inChrRy) {
// this splits a calculation into further calculation tokens
// EXAMPLE:: (23÷-6×4×-9÷2) will return {"23÷-6", "-6×4", "4×-9", "-9÷2"}
// *it also keeps track of operators inline LtoR via oprtrLst
// *does not use regex, does not fail!!!
// zandidigital1@gmail.com
final int len = inChrRy.length;
int dx = 1, inDx = 0, lstOprtrDx = 0; char lstChr = '*';
Boolean isStrt = true, hasOprts = false, isOprtr = false, oprtrTrp = false, lpTrp = false, skpTrp = true;
if(inChrRy[0] == '÷' || inChrRy[0] == '×') { return new String[]{"##!"};}
while(dx < len) {
if(inChrRy[dx] == '÷' || inChrRy[dx] == '×' || inChrRy[dx] == '+' || inChrRy[dx] == '-') {
hasOprts = true;} dx++;}
String[] retStrRy = null;
if(hasOprts) {
ArrayList<String> eqtLst = new ArrayList<String>();
ArrayList<Integer> oprtrLst = new ArrayList<Integer>();
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
dx = 0;
while(dx < len) {
if(isStrt) { isStrt = false;
if(inChrRy[dx] == '-' || inChrRy[dx] == '0' || inChrRy[dx] == '1' || inChrRy[dx] == '2' || inChrRy[dx] == '3' || inChrRy[dx] == '4' || inChrRy[dx] == '5' || inChrRy[dx] == '6' || inChrRy[dx] == '7' || inChrRy[dx] == '8' || inChrRy[dx] == '9') {
sb1.append(String.valueOf(inChrRy[dx]));}
else if(inChrRy[dx] == '÷' || inChrRy[dx] == '×') { return new String[]{"##!"};}}
else {
if(inChrRy[dx] == '×') {
if(oprtrTrp) { return new String[]{"##!"};}
else {
if(!lpTrp) {
sb1.append(String.valueOf(inChrRy[dx]));}
isOprtr = true; oprtrTrp = true; oprtrLst.add(1);}}
else if(inChrRy[dx] == '÷') {
if(oprtrTrp) { return new String[]{"##!"};}
else {
if(!lpTrp) {
sb1.append(String.valueOf(inChrRy[dx]));}
isOprtr = true; oprtrTrp = true; oprtrLst.add(2);}}
else if(inChrRy[dx] == '+') {
if(lstChr == '×' || lstChr == '÷' || lstChr == '-') {
isOprtr = false; oprtrTrp = false;}
else { if(!oprtrTrp && !lpTrp) {
sb1.append(String.valueOf(inChrRy[dx]));}
isOprtr = true; oprtrTrp = true; oprtrLst.add(3);}}
else if(inChrRy[dx] == '-') {
if(oprtrTrp) {
if(lstChr == '+' || lstChr == '-') { return new String[]{"##!"};}
else if(lstChr == '×' || lstChr == '÷') {
if(!lpTrp) { sb1.append(String.valueOf(inChrRy[dx]));}
else { sb2.append(String.valueOf(inChrRy[dx]));}
isOprtr = false; oprtrTrp = false;}}
else {
if(!lpTrp) {
sb1.append(String.valueOf(inChrRy[dx]));}
isOprtr = true; oprtrTrp = true; oprtrLst.add(4);}}
else if(inChrRy[dx] == 'E' || inChrRy[dx] == '.' || inChrRy[dx] == '0' || inChrRy[dx] == '1' || inChrRy[dx] == '2' || inChrRy[dx] == '3' || inChrRy[dx] == '4' || inChrRy[dx] == '5' || inChrRy[dx] == '6' || inChrRy[dx] == '7' || inChrRy[dx] == '8' || inChrRy[dx] =='9') {
if(!lpTrp) { sb1.append(String.valueOf(inChrRy[dx]));}
else { sb2.append(String.valueOf(inChrRy[dx]));}
isOprtr = false; oprtrTrp = false;}
if(isOprtr) {
if(skpTrp) { skpTrp = false; lpTrp = true;}
else {
if(lpTrp) { lstOprtrDx = dx;
eqtLst.add(sb1.toString() + sb2.toString());
sb1 = new StringBuilder(sb2.toString() + String.valueOf(inChrRy[dx]));
sb2 = new StringBuilder();}}}}
lstChr = inChrRy[dx];
dx++;
if(dx == len) {
inDx = lstOprtrDx+1;
sb2 = new StringBuilder();
while(inDx < len) { sb2.append(String.valueOf(inChrRy[inDx])); inDx++;}
eqtLst.add(sb1.toString() + sb2.toString());}}
retStrRy = new String[eqtLst.size()];
for(int z = 0; z < eqtLst.size(); z++) {
retStrRy[z] = eqtLst.get(z);}}
else { /* no need to split here, just return what came in toString(); */}
return retStrRy;}
//••••----------••••


Other 20 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/9/2016 12:35:00 AMZehel

You will need to add just after isStrt = false a inChrRy index check for == '.', a decimal point. Sorry about that, all else of it good to go.
(If this comment was disrespectful, please report it.)

 
11/9/2016 8:44:21 PMZehel

If you need check for other things like "005" or "0.÷", etc. you would add more else ifs after if(dx == len) checking lstChr with current char, inChrRy. Easily done, and just add another boolean as strtTrp that gets falses at the very end of the main while so it doesn't reach for a index of -1 after start split catch was a decimal or -
(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.