The array simulation implements a stack of 50 strings, and uses the stack to convert infix arithmetic expressions into postfix expressions.

1. Use array simulation to implement a stack of 50 strings, and make this stack have the following methods and parameters:

myStack: The name of the array. You cannot directly access this array when using it. The access operations are all done through pop() and push().
Implement pop(): pop up
implement push(): push in
implement isFull(): whether it is full
implement isEmpty(): whether it is empty
implement length(): how many elements are there

public class StackImiate {
    
     
		public int maxSize;
		public char[] a;
		public int top;
		StackImiate(int maxSize) {
    
    
			this.maxSize=maxSize;
			a=new char[maxSize];
			top=-1;
		}
		public void push(char str) {
    
    
			a[++top]=str;
		}	
		public char pop() {
    
    
			return a[top--];
		}
		public boolean isEmpty() {
    
    
			return(top==-1);
		}
		
		public boolean isFull() {
    
    
			return(top==maxSize);
		}
		public int length() 
		   {
    
    return top+1;}
		public char peek() {
    
    
			return a[top];
		}
		public char peekN(int n) {
    
    
			//System.out.println(a[n]);
			return a[n];
		}
		public void displayStack(String s) {
    
    
			System.out.print(s);
			System.out.print(" Stack (bottom-->top): ");
			for(int j=0;j<maxSize;j++) {
    
    
				System.out.print(peekN(j)+" ");
			}
			System.out.println();
	}
}

2. Use the stack to convert an infix arithmetic expression into a postfix expression.

A. There are only six operators +, -, ×, /, %, (,) in the expression
B. The variable name is an alphanumeric string beginning with an English letter
C. Unsigned constants can appear in the expression
D. Appropriately judge the grammatical error in the expression
E, do not calculate the result

public class InToPost {
    
    		
	public static void main(String[] args) {
    
    
		String strResult=new String();
		String  Input="a+b*c+(d*e+f)*g";
		strResult=strResult+doTrans(Input);
		System.out.println();
		System.out.println(strResult);
	}
			public static StringBuffer doTrans(String  Input) {
    
    //返回strResult的函数
				StackImiate  str=new StackImiate(50);
				StringBuffer strResultTemp=new StringBuffer();
				for (int i = 0; i <Input.length(); i++) {
    
    
					char ch = Input.charAt(i);
					str.displayStack("Get " + ch + " ");
					switch (ch) {
    
    
					case '+':
					case '-':
						gotOper(ch, 1,str,strResultTemp);				
						break;
					case '*':
					case '/':
					case '%':
						gotOper(ch, 2,str,strResultTemp);				
						break;
					case '(':					
						str.push(ch);
						break;
					case ')':
						gotParen(ch,str,strResultTemp);				
						break;
					default:					
						strResultTemp =strResultTemp.append(ch);
						break;
					}
				}
				while (!str.isEmpty()) {
    
    
					str.displayStack("out ");
					strResultTemp = strResultTemp.append(str.pop());
				}
				str.displayStack("End  ");				
				return strResultTemp;						
			}
			public static void gotParen(char ch,StackImiate  str,StringBuffer strResultTemp) {
    
    //对括号进行处理的函数
				while (!str.isEmpty()) {
    
    
					char chx = (char) str.pop();		
					if (chx == '(') {
    
    
						break;
					} else {
    
    
						strResultTemp =strResultTemp.append(chx);			
					}
				}
			}
			private static void gotOper(char ch, int i,StackImiate  str,StringBuffer strResultTemp) {
    
    //对运算符的优先级处理
				// TODO Auto-generated method stub
				while (!str.isEmpty()) {
    
    		
					char opTop = (char) str.pop();
					if (opTop == '(') {
    
    		
						str.push('(');
						break;
					} else {
    
    
						int k;
						if (opTop == '+' || opTop == '-') {
    
    
							k = 1;
						} else {
    
    
							k = 2;
						}
						if (k < i) {
    
    		
							str.push(opTop);
							break;
						} else {
    
    
							strResultTemp = strResultTemp.append(opTop);	
						}
					}
				}
				str.push(ch);				
			}
		 
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_45808700/article/details/118156915