,
Train of Thought Description: This problem is actually very simple. It is implemented by stack. It is a classic example when learning data structure. Create two stacks, the digital stack and the symbol stack, and judge the character of the string one by one. If the number is in the digital stack, it will first compare the priority with the symbol at the top of the symbol stack. If the priority is higher, it will enter the stack. If the priority is smaller or equal, it will first calculate the symbol in the stack out of the stack. Two numbers), and then the current symbol into the stack. Until the string discrimination is completed, the symbol stack is checked to see if it is empty. If it is not empty, it can be operated out of the stack at once (because the symbols in the symbol stack are arranged in priority order). Finally, there is a number left in the digital stack, that is, the result.
import java.util.*;
public class Main {
public static void main(String[] args) {
Stack od=new Stack();//Digital stack
Stack op=new Stack();//Symbol stack
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int index=0;//Record the number of symbols that have been executed
int length=str.length();
//System.out.println(length);
while(index<length)
{
//System.out.println("first"+index + "step+++++++++");
char c=str.charAt(index);//Take out the sign for this step.
if(c=='(')
{
//System.out.println(c+"------------------------------------------------------------------------------------------------------------------------
op.push(c);//If left parentheses are in the stack
}
//Otherwise, the priority should be judged first.
else if(c=='+' || c=='-' || c=='*'|| c=='/')
{
int currOplevel=getOplevel(c);//Priority of current symbols
while(true)
{
int stackOplevel=0;//Priority of top stack elements
if(op.isEmpty()==false)
{
Object obj=op.peek();
stackOplevel=getOplevel((char)obj);
}
//If the current element priority is higher than that of the top element, it is put on the stack.
if(currOplevel>stackOplevel)
{
// System.out.println(c+"------------------------------------------------------------------------------------------------------------------------
op.push(c);
break;//Until the symbols that are higher than their own priority are out of the stack and calculated, then put themselves on the stack.
}
else//Computing without stacking
{
try{
char optemp='0';
int odnum1=0;
int odnum2=0;
if(op.isEmpty()==false)
{
optemp=(char)op.pop();//Take out the symbol with the highest priority
}
if(od.isEmpty()==false)
{
odnum1=(int)od.pop();
odnum2=(int)od.pop();//Remove two numbers from the data stack
}
// System.out.println(optemp+" "+odnum1+" "+odnum2);
//System.out.println(cacuResult(optemp,odnum2,odnum1) +"----------------------------------------- stack");
od.push(cacuResult(optemp,odnum2,odnum1));//Put the calculated result data into the data stack again
}catch(Exception e){
//System.out.println("polynomial incorrect 1"+str+"+c);
e.printStackTrace();
}
}
}
}else if(c==')')//The right bracket returns to find the top element of the stack. The right bracket does not enter the stack.
{
while(true)
{
char theop=(char)op.pop();
if(theop=='(')
{
break;
}
else
{
try{
int odnum1=(int)od.pop();
int odnum2=(int)od.pop();
//System.out.println(" "+odnum1+" "+odnum2);
//System.out.println(cacuResult(theop,odnum2,odnum1) +"----------------------------------------- stack");
od.push(cacuResult(theop,odnum2,odnum1));//The contents in operation brackets
}catch(Exception e)
{
//System.out.println("polynomial incorrect 2"+str);
e.printStackTrace();
}
}
}
}else if(c>='0' && c<='9')
{
int tempindex=index+1;
while(tempindex<length)
{
char tempc=str.charAt(tempindex);//Take the next position in the current character in the string
if(tempc>='0' && tempc<='9')
{
tempindex++;//If it is a number, continue to retrieve it backwards.
}
else
{
break;//Completion of proof figures
}
}
String odstr=str.substring(index,tempindex);//Intercepting this string is a number between two symbols
//System.out.println("---------"+odstr+"------------");
try{
int odnum=Integer.parseInt(odstr);//Converting digits into integers for easy operation
//System.out.println(odnum+"------------------------------------------------------------------------------------------------------------------------
od.push(odnum);
index=tempindex-1;
}catch(Exception e)
{
//System.out.println("polynomial incorrect 3"+str);
e.printStackTrace();
}
}
index++;
}
//Check if the op stack is empty
while(true)
{
Object obj=null;
if(op.isEmpty()==false)
{
obj=op.pop();
}
if(obj==null)
{
break;//To prove that the operation has ended for null
}
else//Exit the stack without emptying
{
char optemp=(char)obj;
int odnum1=(int)od.pop();
int odnum2=(int)od.pop();
// System.out.println(cacuResult(optemp,odnum2,odnum1) +"----------------------------------------- stack");
od.push(cacuResult(optemp,odnum2,odnum1));
}
}
int result=0;
try{
result=(int)od.pop();
}catch(Exception e)
{
//System.out.println("polynomial incorrect 4"+str);
e.printStackTrace();
}
System.out.println(result);
}
//Calculating Addition, subtraction, Multiplication and Division Remains
public static int cacuResult(char op,int od1,int od2)
{
switch(op)
{
case '+':return od1+od2;
case '-':return od1-od2;
case '*':return od1*od2;
case '/':return od1/od2;
}
return 0;
}
//Returns symbol priority
public static int getOplevel(char op)
{
switch(op)
{
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
default:return 0;
}
}
}
Summary: This question is actually very simple, but in the process of writing, op.peek() is written as op.pop() when verifying the priority. When verifying, the former element should not be taken out, and the position of the two digits should be reversed when calculating. I didn't find the java version of the code when I searched for the answer to this question, so I made a note of it.