Hello!
I really need your help. I'm new to programming, so I’m sorry if I ask a stupid question :)
I make a calculator for android. It should be able to calculate the expression, for example: (84-12) + (18 + 5)%
Third-party libraries (such as arity) are prohibited.
I have a problem with writing code to calculate interest. It should be just the percentage, not the remainder of the division.
Please suggest links where I can see a similar algorithm (a parser from a string into a mathematics expression with the possibility of calculating interest) or a sample code.
Thank you in advance
equal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String orgString = textView.getText().toString(); result.setText(findValue (orgString)); textView.setText(null); clearResult = false; doubleMathActionBug = false; doubleDotBug = true; } public String findValue (String finalStr) { while (finalStr.contains("(") && finalStr.contains(")")) { int fIndex = finalStr.indexOf("("); int nIndex = finalStr.indexOf(")"); String subString = finalStr.substring(fIndex + 1, nIndex); finalStr = finalStr.substring(0, fIndex) + calculate(subString) + finalStr.substring(nIndex + 1, finalStr.length()); } return calculate(finalStr); } public String calculate(String finalString) { while (finalString.contains("(") && finalString.contains(")")) { findValueInBraces(finalString); } while (!isNum(finalString)) { List<Integer> positions = getOperandPosition(finalString); int pos = positions.get(0); if (positions.size() >= 2 && positions.get(1) != null) { int nxtPos = positions.get(1); finalString = getValue(finalString.substring(0, nxtPos), pos) + finalString.substring(nxtPos, finalString.length()); } else { finalString = getValue( finalString.substring(0, finalString.length()), pos); } } return finalString; } public boolean isNum(String str) { if (str.contains("+") || str.contains("-") || str.contains("*") || str.contains("/") || str.contains("%")) { return false; } return true; } public List<Integer> getOperandPosition(String str) { List<Integer> integers = new ArrayList<Integer>(); if (str.contains("+")) { integers.add(str.indexOf("+")); } if (str.contains("-")) { integers.add(str.indexOf("-")); } if (str.contains("*")) { integers.add(str.indexOf("*")); } if (str.contains("/")) { integers.add(str.indexOf("/")); } if (str.contains("%")) { integers.add(str.indexOf("%")); } Collections.sort(integers); return integers; } public String getValue(String str, int pos) { double finalVal = 0; double a = Double.parseDouble(str.substring(0, pos)); double b = Double.parseDouble(str.substring(pos + 1, str.length())); char c = str.charAt(pos); if (c == '*') { finalVal = a * b; } else if (c == '/') { finalVal = a / b; } else if (c == '+') { finalVal = a + b; } else if (c == '-') { finalVal = a - b; }else if (c == '%') { c = str.charAt(0, pos); if (c == '*') { finalVal = a/100*b; } else if (c == '+') { finalVal = a + a/100*b; } else if (c == '-') { finalVal = a - a/100*b; } } return String.valueOf(finalVal); } });
c = str.charAt(0, pos);insideif (c == '%')It seems to bec = str.charAt(pos-1);or something like that. You need the previous statement there, not the same one. - lllyct