Import Java.util.Arrays;
Import Java.util.Scanner; Simple calculator/infix expression suffix expression, derived results for reverse Polish number output//e.g. input: (11+2.2) *3.1+ (30*45)-((1.1+23) *3.3)/5.2//Convert to suffix expression: 11 2.2 + 3.1 * 30 45
* + 1.1 23 + 3.3 * 5.2////Output Result: 1375.6257692307693 public class Turnrpn {public static void main (string[] args) {
Char[] s = GetChar ();
String expression = getexpression (s);
System.out.print ("Convert to suffix expression:");
SYSTEM.OUT.PRINTLN (expression);
Converts a string to a character array char[] s2 = Expression.tochararray ();
Arrstack as = RPN (S2);
System.out.print ("Output result:");
As.liststack ();
//Converts the input string to a character array public static char[] GetChar () {Scanner sc = new Scanner (system.in);
Converts the input string into a character array System.out.println ("Enter infix expression (do not enter a space):");
Char[] s = Sc.nextline (). ToCharArray ();
return s;
//infix expression suffix expression public static String GetExpression (char[] s) {Arrstack as = new Arrstack ();
Creates a string storage suffix expression string arr = ""; for (int i = 0; i < s.length i++) {//if a number or. directly into the string if (Isnumber (s[i)) | | S[i] = = '. ')
{arr = arr + s[i]; Determine if the number is fully entered and, if all is entered, then add a space if (I!= s.length-1) {if (!isnumber (s[i + 1]) && s[i + 1]!= '. ')
{arr = arr + "";
} else {//if the last digit of the character is a number, then add a space directly (distinguishing the operator character from the last stack) arr = arr + ""; //If it is an operator character, then to judge after the stack} else if (s[i] = = ' + ' | | | s[i] = = '-' | | | s[i] = = ' * ' | | | s[i] = = '/') {//If yes =-to determine whether the top element of the stack is */If yes, first stack the top elements out of the stack and then into the stack otherwise, directly into the stack if (s[i] = = ' + ' | | s[i] = = ') {if (!as.isempty ()) {//Determine whether stack is empty char r = AS
. GetTop ();
while (r = = ' + ' | | = = = ' | | | = = = ' * ' | | = r = = '/') {R = As.stackpop ();
arr = arr + R + "";
If the stack is empty, jump directly out of the loop if (!as.isempty ()) {R = As.gettop ();
} else {break;
}} as.stackpush (S[i]);
else {As.stackpush (s[i]);
//If the word Fu Shi (directly into the stack, if) then the stack top element is sequentially out of the stack until the stack element is (so} else if (s[i] = = ' (') {As.stackpush (s[i]);
else if (s[i] = = ") {char R = as.stackpop (); while (R!= ' (') {arr = arr + R + ' ";
R = As.stackpop ();
The remaining elements in the stack are sequentially stored in the string while (!as.isempty ()) {char R = as.stackpop ();
arr = arr + R + "";
return arr;
}//Inverse Polish expression evaluation public static Arrstack RPN (char[] s) {Arrstack as = new Arrstack ();
Double num = 0;
Define an empty string to be stored in character string arr = "";
Double A = 0;
Double b = 0;
Double result = 0; for (int i = 0; i < s.length i++) {//if a number or. Then push into the stack if (Isnumber (s[i)) | | s[i] = = '. ')
{arr = arr + s[i];
else if (s[i] = = = "{//encounters a space, the data is removed into a double type push into the stack if (arr = =" ") {continue;
else {num = double.valueof (arr);
As.stackpush (num);
string null arr = "";
} else {//encountered operator characters, the top two number of pop-up stack before pressing into the stack switch (S[i]) {case ' + ': a = As.stackpop ();
b = As.stackpop ();
result = B + A;
As.stackpush (result);
Break
Case '-': a = As.stackpop ();
b = As.stackpop ();
result = B-a; AS.stackpush (result);
Break
Case ' * ': a = As.stackpop ();
b = As.stackpop ();
result = b * A;
As.stackpush (result);
Break
Case '/': a = As.stackpop ();
b = As.stackpop ();
if (a = = 0) {System.out.println ("divisor cannot be 0!");
Break
result = b/a;
As.stackpush (result);
Break
}} return as;
}//Determine if the character is a digital public static Boolean isnumber (char c) {if (c >= && c <=) {return true;
return false;
}///create order stack public static class Arrstack {//define array save element private object[] Elementdata;
Define the current length of the stack private int size;
Defines the current capacity of the stack private int capacity;
Defines the top of the stack public int;
Initialize, the default is to set up an array of 50 arrstack () {elementdata = new object[50];
capacity = 50;
}//initialization, custom capacity array public arrstack (int n) {elementdata = new object[n];
capacity = N; //into stack public <T> void Stackpush (T data) {//If the capacity is full, create an array to expand the capacity if (SIZe >= capacity) {int newlength = (capacity * 3/2) + 1;
Elementdata = arrays.copyof (Elementdata, newlength);
capacity = Newlength;
} Elementdata[top] = data;
top++;
size++;
}//Out Stack public <T> T Stackpop () {if (size = = 0) {System.out.println ("currently empty stack");
return null;
else {T data = (t) this.elementdata[top-1];
top--;
size--;
return data;
}///traversal stack public void Liststack () {if (size = = 0) {System.out.println ("empty stack");
else if (size = = 1) {System.out.println (elementdata[top-1]);
System.out.println ("Length of stack" + size);
else {for (int i = 0; i < size; i++) {System.out.print (Elementdata[i] + "");
} System.out.println ("Length of stack" + size);
}//Returns the current capacity public void capacity () {System.out.println ("current maximum capacity" + capacity);
///Returns the value of the current top of the stack public <T> T GetTop () {T data = (t) elementdata[top-1];
return data; }///null public boolean ISempty () {return size = = 0;
}
}
}