Skip to main content
2 of 8
added 3041 characters in body
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Java 8, 420 418 402 bytes

import java.util.*;s->f(s,new Stack())int f(String s,Stack<Integer>S){int i=0,c,t;for(;(t=S.size()<1?1:S.peek())*0+i++<s.length();){if((c=s.charAt(i-1))<42)return c<41?f(s.substring(i),S):t==0?t:f(s,S);if(c<44)S.add(c<43?S.pop()*S.pop():S.pop()+S.pop());if(c==46)System.out.println(t);if(c>57)S.add(c>99?(int)(Math.random()*1e5):t);if(c==45|c==47){t=S.pop();S.add(c<46?S.pop()-t:S.pop()/t);}}return t;} 

Try it online.

Explanation:

import java.util.*; // Required import for 2x Stack s-> // Method with String parameter and no return-type f(s,new Stack()) // Call the recursive function, with a new Stack // Separated recursive method with String and Stack parameters int f(String s,Stack<Integer>S){ int i=0, // Index integer c, // Temp integer used for the current character t; // Temp integer used for the peeked top of the stack for(;(t=S.size()<1?1:S.peek())*0+ // Peek the stack (if not empty) and set `t` to this value i++<s.length();){ // Loop `i` in the range [0, String-length): if((c=s.charAt(i-1)) // Set `c` to the current character <42) // If the character is either '(' or ')': return c<41? // If the character is '(': f(s.substring(i),// Remove everything before and including the "(" in the String S) // And do a recursive call with this trailing part : // Else (the character is ')') t==0? // If the top of the stack is 0: t // Return this 0 : // Else: f(s,S); // Do a recursive call if(c<44) // If the character is '*' or '+': S.add(c<43? // If the character is '*': S.pop()*S.pop() // Pop the top two values, and add their product to the stack : // Else (the character is '+'): S.pop()+S.pop());// Pop the top two values, and add their sum to the stack if(c==46) // If the character is '.' System.out.println(t); // Print `t` with trailing newline if(c>57) // If the character is ':' or '~': S.add(c>99? // If the character is '~': (int)(Math.random()*1e5) // Add a random integer in the range [0,100000) to the stack : // Else (the character is ':') t); // Add `t` to the stack if(c==45|c==47){ // If the character is '-' or '/': t=S.pop(); // Pop the top value of the stack (saving it in `t`) S.add(c<46? // If the character is '-': S.pop()-t // Pop and subtract the top two values, and add it to the stack : // Else (the character is '/'): S.pop()/t);}} // Pop and integer-divide the top two values, and add it to the stack return t;} // Mandatory return-statement 
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394