[Daily Blue Bridge] 6. The real question of "Reverse Polish Expression" in the Java Group in the 13th Provincial Competition

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Reverse Polish Expression

Normal expressions are called infix expressions. The operator is in the middle, which is mainly for human reading, and it is not convenient to solve by machine.

For example: 3 + 5 * (2 + 6)-1

And often need to use parentheses to the order of the table

On the contrary, if the reverse Polish expression (prefix expression) is used, the above formula is:

- + 3 * 5 + 2 6 1

The brackets are no longer needed, and the machine can use the recursive method to solve it easily

For simplicity, we assume:

1. Only +-* three operators

2. Each operand is a non-negative integer less than 10

The following program evaluates an inverse Polish representation string,

The return value is a piece of data: the first element represents the evaluation result, and the second element represents the number of characters it has parsed

 

static int [] evaluste(String x) {

if (x.length()==0) {

return new int[] {0,0};

}

 

char  c =x.charAt (0);

if (c>='0'&&c<='9') {

return new int[] { c-'0',1};

}

int[] v1 = evaluste(x.substring(1));

int []v2 =_________________________________; //fill in the blanks

 

int v = Integer.MAX_VALUE;

if(c=='+') v = v1[0] + v2[0];

if(c=='*') v = v1[0] * v2[0];

if(c=='-') v = v1[0] - v2[0];

 

return new int[] { v,1 + v1[1] + v2[1]};

}

 

Please analyze the code logic and guess the code at the underline, and submit it through the web

Note: Only use the missing code as the answer, do not fill in extra codes, symbols or explanatory text! ! !

Problem-solving ideas:

The key to solving this problem is to read through the source code in the stem, understand the specific functions to be implemented in each line, and then understand the main idea of ​​the program design, and fill in the blanks according to the requirements in the stem.

For a specific explanation of the source code, see the answer below.

Answer source code:

package 一三年省赛真题;

public class Year2013_t6 {

	public static void main(String[] args) {
		
		//原式子:3+5*(2+6)-1=42
		int a[] = evaluste("-+3*5+261");
		System.out.println(a[0]);
	}
	
	/**
	 * 逆波兰表达式函数
	 * -+3*5+261
	 * @param x 传入的逆波兰表达式
	 * */
	static int [] evaluste(String x) {
		//如果传入的字符串长度为0,说明该字符串无效
		if (x.length()==0) {
			return new int[] {0,0};
		}
		
		char c = x.charAt(0);	//获取到字符串的第一个字符
		if (c>='0'&&c<='9') {	//判断该字符是不是数字
			return new int[] {c-'0',1};	//如果是数字,返回该数字和处理的字符数
		}
		int[] v1 = evaluste(x.substring(1));	//如果第一个字符是运算符,截取后面的部分
		int[] v2 = evaluste(x.substring(1+v1[1]));		//填空位置   截取上一步没有截取到的部分
		
		int v = Integer.MAX_VALUE;	
		
		//根据第一个字符的类型。对后面返回的结果进行处理
		if(c=='+') v = v1[0] + v2[0];
		if(c=='*') v = v1[0] * v2[0];
		if(c=='-') v = v1[0] - v2[0];
		
		return new int[] {v,1 + v1[1] + v2[1]};		//返回处理结果
	}

}

 

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

 

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112678377