Java implements the inverse Polish Calculator

Source: Internet
Author: User
Package COM. CZP; import Java. util. hashmap; import Java. util. stack; public class calculator {// operator priority Private Static hashmap <string, integer> opls; private string SRC; Public calculator (string SRC) {This. src = SRC; If (opls = NULL) {opls = new hashmap <string, integer> (6); opls. put ("+", 0); opls. put ("-", 0); opls. put ("*", 1); opls. put ("/", 1); opls. put ("%", 1); opls. put (")", 2) ;}// convert the infix expression to the suffix expression Public String torpn () {string [] TMP = Split (SRC); // suffix stack <string> RPN = new stack <string> (); // temporary stack <string> tmpsta = new stack <string> (); For (string STR: TMP) {If (isnum (STR) {// is the operand, directly press the result stack RPN. push ('+ STR +');} else {// operation symbol if (tmpsta. isempty () {// There is no tmpsta symbol. push (STR);} else {// judge the priority of the current symbol and the temporary stack top symbol if (ishigh (tmpsta. peek (), STR) {If (! Str. equals (")") {do {// 1 find the operator that is earlier than the current priority in the temporary stack // 2 press into the current read operator RPN. push (tmpsta. peek (); tmpsta. pop () ;}while (! Tmpsta. isempty () & (ishigh (tmpsta. peek (), STR); tmpsta. push (STR);} else {// yes) pop up the data of the temporary stack in sequence until (until the while (! Tmpsta. isempty ()&&! Tmpsta. Peek (). Equals ("(") {RPN. Push (tmpsta. Pop ();} If ((! Tmpsta. empty () & (tmpsta. peek (). equals ("(") {// pop-up (tmpsta. pop () ;}} else if (! Ishigh (tmpsta. Peek (), STR) {tmpsta. Push (STR) ;}}} while (! Tmpsta. empty () {// pops up the remaining operators in the stack in RPN. push (tmpsta. pop ();} stringbuilder ST = new stringbuilder (); For (string STR: RPN) {St. append (STR);} RPN. clear (); Return St. tostring ();} // split (56 + 4) 3*6 + 2 => (, 56, +, 4, private string [] Split (string SRC) {stringbuilder sb = new stringbuilder (SRC. length (); For (char CH: SRC. tochararray ()) {If (CH = '+' | CH = '-' | CH = '*' | CH = '/ '| CH =' ('| CH = ') '| CH =' % ') {sb. append ("," ); Sb. append (CH); sb. append (",");} else {sb. append (CH) ;}string string = sb. tostring (). replaceall (", {2,}", ","); Return string. split (",") ;}// comparison operator priority private Boolean ishigh (string pop, string Str) {If (Str. equals (")") return true; If (opls. get (POP) = NULL | opls. get (STR) = NULL) return false; return opls. get (POP)> = opls. get (STR) ;}// whether the number is public Boolean isnum (string Str) {for (char CH: Str. tochararray () {If (CH = '+' | CH = '-' | CH = '*' | CH = '/' | ch = '(' | CH = ') '| CH =' % ') return false;} return true;} // obtain the public double getres () {string RPN = torpn (); stack <double> res = new stack <double> (); stringbuilder sb = new stringbuilder (); For (char CH: RPN. tochararray () {If (CH = '(') {continue ;} else if (CH> = '0' & Ch <= '9' | CH = '. ') {sb. append (CH);} else if (CH = ') {res. push (double. valueof (sb. tostring (); sb = new stri Ngbuilder ();} else {If (! Res. empty () {Double X = res. pop (); Double Y = res. pop (); Switch (CH) {Case '+': Res. push (Y + x); break; Case '-': Res. push (Y-x); break; Case '*': Res. push (y * X); break; Case '%': Case '/': If (X! = 0) {double RSD = CH = '% '? Y % x: Y/X; Res. push (RSD);} else {system. out. println ("Zero Denominator"); Res. clear (); Return-1;} break; }}} double result = res. pop (); Res. clear (); return result;} public static void main (string [] ARGs) {string src2 = "156899 + 5*(36 + 5)-8% 3 "; calculator analyer = new calculator (src2); system. out. println (src2 + "=" + analyer. getres ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.