Home>

Calculator implemented in java,See code comments for principle,The specific content is as follows

public class mycalculator {
 public static void main (string [] args) {
  string obj="6+ (8/2) + 6/3 + 1 * 8 + 30";
  arraylist<string>arraylist=strformat (obj);
  system.out.println (obj + "=" + calculator (arraylist));
 }
 /**
  * Calculate results using subsequent expressions
  * 1. When the current string is a number,Directly on the stack
  * 2. When the current string is an operator,Take out the first two calculations in the stack
  * 3. Put the calculation result on the stack again,The last remaining element in the stack is the required result
  * /
 private static int calculator (arraylist<string>obj) {
  arraylist<string>result=transform (obj);
  system.out.println (result);
  stack<integer>stack=new stack<>();
  for (int i=0;i<result.size ();i ++) {
   string symbol=result.get (i);
   if (isdigital (symbol)) {//Numbers are pushed directly onto the stack
    stack.push (integer.parseint (symbol));
   } else {//processing operator
    int num1, num2;
    num1=stack.pop ();//Remove two numbers
    num2=stack.pop ();
    switch (symbol) {
     case "+":
      stack.push (num2 + num1);
      break;
     case "-":
      stack.push (num2-num1);
      break;
     case "*":
      stack.push (num2 * num1);
      break;
     case "/":
      stack.push (num2/num1);
      break;
     default:
      break;
    }
   }
  }
  return stack.pop ();
 }
 /**
  * In-order traversal changed to subsequent traversal
  * /
 private static arraylist<string>transform (arraylist<string&arraylist) {
  stack<string>stack=new stack<>();
  arraylist<string>result=new arraylist<>();
  for (int index=0;index<arraylist.size ();index ++) {
   string symbol=arraylist.get (index);
   if (isdigital (symbol)) {//If it is a digital output
    result.add (symbol);
   } else if (symbol.equals (")")) {
    string tmp;
    while (! (tmp=stack.pop ()). equals ("(")) {//Stop after successful match
     result.add (tmp);
    }
   } else {
    if (stack.isempty ()) {
     stack.push (symbol);
     continue;
    }
    string tmp=stack.peek ();
    while (outpriority (symbol)<= inpriority (tmp)) {//priority is less than the priority in the stack,Out of the stack
     result.add (tmp);
     stack.pop ();
     if (stack.isempty ()) {
      break;
     }
     tmp=stack.peek ();
    }
    stack.push (symbol);
   }
  }
  //Put the rest out of the stack
  while (! stack.isempty ()) {
   result.add (stack.pop ());
  }
  return result;
 }
 /**
  * First format the string into an arraylist
  * @param src 3 * 5 + 8;
  * @return arraylist 3 * 5 + 8
  * /
 private static arraylist<string>strformat (string src) {
  if (src == null || src.equals ("")) {
   return null;
  }
  arraylist<string>arraylist=new arraylist<>();
  stringbuilder comchar=new stringbuilder ();
  for (int i=0;i<src.length ();i ++) {
   char ch=src.charat (i);
   if (ch == "") {
    continue;//Remove spaces
   }
   if (! character.isdigit (ch)) {
    if (! comchar.tostring (). trim (). equals ("")) {
     arraylist.add (comchar.tostring (). trim ());
     comchar.delete (0, comchar.length ());
    }
    arraylist.add (ch + "");
    continue;
   }
   comchar.append (ch);
  }
  if (! comchar.tostring (). trim (). equals ("")) {
   arraylist.add (comchar.tostring (). trim ());
  }
  return arraylist;
 }
 /**
  * Determine if it is a number
  * @param symbol 782 or +-* /
  * @return true or false
  * /
 private static boolean isdigital (string symbol) {
  return! symbol.equals ("+")&&! symbol.equals ("-")
    &&! Symbol.equals ("*")&&! Symbol.equals ("/")
    &&! Symbol.equals ("(")&&! Symbol.equals (")");
 }
 private static int inpriority (string ch) {
  switch (ch) {
   case "+":
   case "-":
    return 2;
   case "*":
   case "/":
    return 4;
   case ")":
    return 7;
   case "(":
    return 1;
   default:
    return 0;
  }
 }
 private static int outpriority (string ch) {
  switch (ch) {
   case "+":
   case "-":
    return 3;
   case "*":
   case "/":
    return 5;
   case ")":
    return 1;
   case "(":
    return 7;
   default:
    return 0;
  }
 }
}
  • Previous JFreeChart line chart generation method
  • Next django2 use iframe tag to complete the function of playing b-site video