Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active July 1, 2020 17:41
Show Gist options
  • Save jatinsharrma/5c15dacd91479e4f6ab93036c5b479a5 to your computer and use it in GitHub Desktop.
Save jatinsharrma/5c15dacd91479e4f6ab93036c5b479a5 to your computer and use it in GitHub Desktop.
Mathematical Expression Evaluator : For Calculator
// Python code for this problem : https://gist.github.com/jatinsharrma/e64bfbe225d9d97d770873d4700e5765
// This program is based on Operator Precedence Parser concept.
var lastNumeric : Boolean = false
var lastDot : Boolean = false
var stack = ArrayList<String>()
var parCount : Int = 0
var parFlag : Boolean = false
var wrongFlag : Boolean = false
var negativeFlag : Boolean = false
fun main(args : Array<String>) {
for (i in "11+(2)+3"){
literals(i.toString())
println(stack.toString())
}
if (!wrongFlag) {
println(onEqual())
}else{
println("Wrong")
}
}
fun onEqual() : String {
if (lastNumeric) {
var stack1 = ArrayList<String>()
var stack2 = ArrayList<Double>()
stack1.add("$")
var i: Int = 0
while (i != stack.size) {
if (stack[i] !in "+-*/()") {
stack2.add(stack[i].toDouble())
} else {
var size = stack.size
var size1 = stack1.size
var size2 = stack2.size
if (stack[i] == "(") {
stack1.add(stack[i])
} else if (stack[i] == ")") {
if (stack1[size1 - 1] != "(") {
while (stack1[size1 - 1] != "(") {
calculateAndDelete( stack1, stack2)
size1--
size2 -= 2
}
}
stack1.removeAt(size1 - 1)
} else {
var previous: String = stack1[size1 - 1]
if (precedence(previous, stack[i])) {
stack1.add(stack[i])
} else {
calculateAndDelete(stack1, stack2)
stack1.add(stack[i])
}
}
}
i++
//println(stack1.toString())
//println(stack2.toString())
//println("---------------")
}
var size1 = stack1.size
while (stack1.size > 1) {
calculateAndDelete( stack1, stack2)
}
return(stack2[0].toString())
}
return "0"
}
private fun operation(opt1 : Double , opt2 : Double , op : String) : Double{
var res : Double = 0.0
try {
when (op) {
"*" -> res = opt1 * opt2
"/" -> res = opt1 / opt2
"+" -> res = opt1 + opt2
"-" -> res = opt1 - opt2
}
}catch (e : TypeNotPresentException){
e.printStackTrace()
}
return res
}
private fun precedence(op1 : String , op2: String) : Boolean{
if (op1 in "+-" && op2 in "*/")
return true
if (op1 in "$(" && op2 in "-+/*")
return true
return false
}
private fun calculateAndDelete(
array1 : ArrayList<String>,array2 : ArrayList<Double>){
var size1 = array1.size
var size2 = array2.size
var opt1 = array2[size2-2]
var opt2 = array2[size2-1]
var op = array1[size1-1]
array1.removeAt(size1-1)
array2.removeAt(size2-1)
array2.removeAt(size2-2)
size1 --
size2 -= 2
var res = operation(opt1,opt2,op)
array2.add(res)
//println(array1.toString())
//println(array2.toString())
}
fun literals(new : String){
if (new.length != 0){
if (!(lastNumeric || parFlag)){
if (new == "("){
stack.add(new)
parCount ++
}else if (new == (")") && parCount == 0){
wrongFlag = true
}else if (new == "-" && !negativeFlag){
stack.add(new)
lastNumeric = true
negativeFlag = true
}else if (new !in "+-/*)."){
stack.add(new)
lastNumeric = true
parFlag = false
negativeFlag = false
}else if (new == "." && !(lastDot)){
stack.add("0.")
lastNumeric = true
lastDot = true
negativeFlag = true
}else{
wrongFlag = true
}
}else if (lastNumeric && !(parFlag)){
if (new == ")" && parCount >0){
stack.add(new)
parFlag = true
lastNumeric = false
lastDot = false
parCount --
}else if (new !in "-+/*()."){
literalHelper(new)
negativeFlag = false
}else if (new == "." && !lastDot){
literalHelper(new)
lastDot = true
}else if (new == "-" && negativeFlag){
}else if (new in "-+/*" && !negativeFlag){
stack.add(new)
lastNumeric = false
parFlag = false
lastDot = false
}else{
wrongFlag = true
}
}else if (!lastNumeric && parFlag){
if (new in "+-*/"){
stack.add(new)
parFlag = false
}else if (new == ")" && parCount > 0 ){
parCount --
stack.add(new)
}else{
wrongFlag = true
}
}
}else{
wrongFlag = true
}
}
fun literalHelper(new : String){
var size = stack.size
var previous = stack[size - 1]
stack.removeAt(size - 1)
size --
previous += new
stack.add(previous)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment