Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Help
Spring Boot - Evaluating a Math Expression in Java
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="EKGuest" data-source="post: 29001583" data-attributes="member: 582168"><p>Formula එක මොන වගේද? උදාහරණ කීපයක් දෙන්න බලන්න.</p><p></p><p>නෙට් එකේ තියෙන කෝඩ් හරියන්නෙ නැද්ද වැඩේට? මේ GeeksForGeeks ජාවා code එකෙන් 100 * ( 2 + 12 ) / 14 වගේ expressions evaluate කරන්න පුලුවන්.</p><p></p><p><a href="https://www.geeksforgeeks.org/expression-evaluation/" target="_blank">https://www.geeksforgeeks.org/expression-evaluation/</a></p><p></p><p>[CODE=java]/* A Java program to evaluate a</p><p>given expression where tokens</p><p>are separated by space.</p><p>*/</p><p>import java.util.Stack;</p><p></p><p>public class EvaluateString</p><p>{</p><p> public static int evaluate(String expression)</p><p> {</p><p> char[] tokens = expression.toCharArray();</p><p></p><p> // Stack for numbers: 'values'</p><p> Stack<Integer> values = new</p><p> Stack<Integer>();</p><p></p><p> // Stack for Operators: 'ops'</p><p> Stack<Character> ops = new</p><p> Stack<Character>();</p><p></p><p> for (int i = 0; i < tokens.length; i++)</p><p> {</p><p> </p><p> // Current token is a</p><p> // whitespace, skip it</p><p> if (tokens[i] == ' ')</p><p> continue;</p><p></p><p> // Current token is a number,</p><p> // push it to stack for numbers</p><p> if (tokens[i] >= '0' &&</p><p> tokens[i] <= '9')</p><p> {</p><p> StringBuffer sbuf = new</p><p> StringBuffer();</p><p> </p><p> // There may be more than one</p><p> // digits in number</p><p> while (i < tokens.length &&</p><p> tokens[i] >= '0' &&</p><p> tokens[i] <= '9')</p><p> sbuf.append(tokens[i++]);</p><p> values.push(Integer.parseInt(sbuf.</p><p> toString()));</p><p> </p><p> // right now the i points to</p><p> // the character next to the digit,</p><p> // since the for loop also increases</p><p> // the i, we would skip one</p><p> // token position; we need to</p><p> // decrease the value of i by 1 to</p><p> // correct the offset.</p><p> i--;</p><p> }</p><p></p><p> // Current token is an opening brace,</p><p> // push it to 'ops'</p><p> else if (tokens[i] == '(')</p><p> ops.push(tokens[i]);</p><p></p><p> // Closing brace encountered,</p><p> // solve entire brace</p><p> else if (tokens[i] == ')')</p><p> {</p><p> while (ops.peek() != '(')</p><p> values.push(applyOp(ops.pop(),</p><p> values.pop(),</p><p> values.pop()));</p><p> ops.pop();</p><p> }</p><p></p><p> // Current token is an operator.</p><p> else if (tokens[i] == '+' ||</p><p> tokens[i] == '-' ||</p><p> tokens[i] == '*' ||</p><p> tokens[i] == '/')</p><p> {</p><p> // While top of 'ops' has same</p><p> // or greater precedence to current</p><p> // token, which is an operator.</p><p> // Apply operator on top of 'ops'</p><p> // to top two elements in values stack</p><p> while (!ops.empty() &&</p><p> hasPrecedence(tokens[i],</p><p> ops.peek()))</p><p> values.push(applyOp(ops.pop(),</p><p> values.pop(),</p><p> values.pop()));</p><p></p><p> // Push current token to 'ops'.</p><p> ops.push(tokens[i]);</p><p> }</p><p> }</p><p></p><p> // Entire expression has been</p><p> // parsed at this point, apply remaining</p><p> // ops to remaining values</p><p> while (!ops.empty())</p><p> values.push(applyOp(ops.pop(),</p><p> values.pop(),</p><p> values.pop()));</p><p></p><p> // Top of 'values' contains</p><p> // result, return it</p><p> return values.pop();</p><p> }</p><p></p><p> // Returns true if 'op2' has higher</p><p> // or same precedence as 'op1',</p><p> // otherwise returns false.</p><p> public static boolean hasPrecedence(</p><p> char op1, char op2)</p><p> {</p><p> if (op2 == '(' || op2 == ')')</p><p> return false;</p><p> if ((op1 == '*' || op1 == '/') &&</p><p> (op2 == '+' || op2 == '-'))</p><p> return false;</p><p> else</p><p> return true;</p><p> }</p><p></p><p> // A utility method to apply an</p><p> // operator 'op' on operands 'a'</p><p> // and 'b'. Return the result.</p><p> public static int applyOp(char op,</p><p> int b, int a)</p><p> {</p><p> switch (op)</p><p> {</p><p> case '+':</p><p> return a + b;</p><p> case '-':</p><p> return a - b;</p><p> case '*':</p><p> return a * b;</p><p> case '/':</p><p> if (b == 0)</p><p> throw new</p><p> UnsupportedOperationException(</p><p> "Cannot divide by zero");</p><p> return a / b;</p><p> }</p><p> return 0;</p><p> }</p><p></p><p> // Driver method to test above methods</p><p> public static void main(String[] args)</p><p> {</p><p> System.out.println(EvaluateString.</p><p> evaluate("10 + 2 * 6"));</p><p> System.out.println(EvaluateString.</p><p> evaluate("100 * 2 + 12"));</p><p> System.out.println(EvaluateString.</p><p> evaluate("100 * ( 2 + 12 )"));</p><p> System.out.println(EvaluateString.</p><p> evaluate("100 * ( 2 + 12 ) / 14"));</p><p> }</p><p>}</p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="EKGuest, post: 29001583, member: 582168"] Formula එක මොන වගේද? උදාහරණ කීපයක් දෙන්න බලන්න. නෙට් එකේ තියෙන කෝඩ් හරියන්නෙ නැද්ද වැඩේට? මේ GeeksForGeeks ජාවා code එකෙන් 100 * ( 2 + 12 ) / 14 වගේ expressions evaluate කරන්න පුලුවන්. [URL]https://www.geeksforgeeks.org/expression-evaluation/[/URL] [CODE=java]/* A Java program to evaluate a given expression where tokens are separated by space. */ import java.util.Stack; public class EvaluateString { public static int evaluate(String expression) { char[] tokens = expression.toCharArray(); // Stack for numbers: 'values' Stack<Integer> values = new Stack<Integer>(); // Stack for Operators: 'ops' Stack<Character> ops = new Stack<Character>(); for (int i = 0; i < tokens.length; i++) { // Current token is a // whitespace, skip it if (tokens[i] == ' ') continue; // Current token is a number, // push it to stack for numbers if (tokens[i] >= '0' && tokens[i] <= '9') { StringBuffer sbuf = new StringBuffer(); // There may be more than one // digits in number while (i < tokens.length && tokens[i] >= '0' && tokens[i] <= '9') sbuf.append(tokens[i++]); values.push(Integer.parseInt(sbuf. toString())); // right now the i points to // the character next to the digit, // since the for loop also increases // the i, we would skip one // token position; we need to // decrease the value of i by 1 to // correct the offset. i--; } // Current token is an opening brace, // push it to 'ops' else if (tokens[i] == '(') ops.push(tokens[i]); // Closing brace encountered, // solve entire brace else if (tokens[i] == ')') { while (ops.peek() != '(') values.push(applyOp(ops.pop(), values.pop(), values.pop())); ops.pop(); } // Current token is an operator. else if (tokens[i] == '+' || tokens[i] == '-' || tokens[i] == '*' || tokens[i] == '/') { // While top of 'ops' has same // or greater precedence to current // token, which is an operator. // Apply operator on top of 'ops' // to top two elements in values stack while (!ops.empty() && hasPrecedence(tokens[i], ops.peek())) values.push(applyOp(ops.pop(), values.pop(), values.pop())); // Push current token to 'ops'. ops.push(tokens[i]); } } // Entire expression has been // parsed at this point, apply remaining // ops to remaining values while (!ops.empty()) values.push(applyOp(ops.pop(), values.pop(), values.pop())); // Top of 'values' contains // result, return it return values.pop(); } // Returns true if 'op2' has higher // or same precedence as 'op1', // otherwise returns false. public static boolean hasPrecedence( char op1, char op2) { if (op2 == '(' || op2 == ')') return false; if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) return false; else return true; } // A utility method to apply an // operator 'op' on operands 'a' // and 'b'. Return the result. public static int applyOp(char op, int b, int a) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (b == 0) throw new UnsupportedOperationException( "Cannot divide by zero"); return a / b; } return 0; } // Driver method to test above methods public static void main(String[] args) { System.out.println(EvaluateString. evaluate("10 + 2 * 6")); System.out.println(EvaluateString. evaluate("100 * 2 + 12")); System.out.println(EvaluateString. evaluate("100 * ( 2 + 12 )")); System.out.println(EvaluateString. evaluate("100 * ( 2 + 12 ) / 14")); } } [/CODE] [/QUOTE]
Insert quotes…
Verification
Haya warak paha keeyada? (haya wadi kireema paha)
Post reply
Top
Bottom