Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use simple Calculator with Stack in Java

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Java how to use the stack to use the simple calculator", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use the stack in Java to use simple calculator" this article.

Topic: use the stack to calculate the result of a similar expression: 5-2-3-2

Tip: the simple calculator operation symbol is limited to the calculation of +, -, *, /

Analysis ideas:

1. Create a number stack and a symbol stack, the number stack is used to store numbers, and the symbol stack is used to store symbols

2. Create an index index for traversing expressions

3. Scan the expression, if the number enters the stack directly, if it is a symbol, it needs to be judged. There are two cases, one is when the symbol stack is empty, the symbol is directly put into the stack. Second, it is not empty, first compare the priority of the symbol at the top of the current stack with the symbol to be entered into the stack. If the priority of the operator to be entered into the stack is small, the two numbers of the number stack will be popped up, and one of the operator of the symbol stack will be popped up, and the calculation will be carried out. The result of the calculation goes directly into the number stack, and if the priority is high, it goes directly into the stack.

4. After scanning the expression, the corresponding numbers and operators are popped up sequentially from the number stack and symbol stack, and calculated.

5. When the symbol stack is empty, it means that the calculation is done, and there is only one number left in the stack, which is the result of the expression calculation.

Code implementation

Package cn.mrlij.stack; import java.util.Arrays;import java.util.Scanner; / * uses an array to implement the stack * * @ author dreamer * * / public class ArrayStackDemo {public static void main (String [] args) {String express = "5011 alternate 2 steps 3-2"; int index = 0 int num2 / defines an index value for traversing the expression int num1 = 0; int num2 = 0; int res = 0 / / calculation result char ch =''; int oper = 0; String keepNum = ""; ArrayStack numStack = new ArrayStack (10); / / create a stack ArrayStack operStack = new ArrayStack (10); / / create a symbol stack while (true) {ch = express.substring (index,index+1) .charat (0) / / the traversal operator / / determines whether it is the operator if (operStack.isOper (ch)) {/ / determines whether there is a symbol in the current symbol stack, if (! operStack.isEmpty ()) {/ / is not empty, determines the priority if (operStack.priority (ch) = express.length ()) {break After scanning, while (true) {if (operStack.isEmpty ()) {break;} num1 = numStack.pop (); num2 = numStack.pop (); oper = operStack.pop (); res = operStack.cal (num1,num2,oper); numStack.push (res) } System.out.println ("expression:" + express+ "=" + numStack.pop ());}} class ArrayStack {private int MaxSize;// defines the maximum length of the array private int [] arr;// defines the array, and the data is placed at the top of the private int top =-1 int maxSize / definition stack with initialization data of-1 public ArrayStack (int maxSize) {this.MaxSize = maxSize; arr = new int [MaxSize]. } / / determine whether the array is empty public boolean isEmpty () {return top = =-1;} / / determine whether the array is full of public boolean isFull () {/ / System.out.println ("top of stack:" + top + "maximum length:" + MaxSize); return top = = MaxSize-1;} / / take out public int peek () {return arr [top] } / / public void push (int val) {/ / check whether the stack is full first. If it is full, you cannot add if (isFull ()) {System.out.println ("stack is full ~"); return;} top++; arr [top] = val } / / destack public int pop () {/ / first determine whether the stack is empty if (isEmpty ()) {throw new RuntimeException ("stack is empty, cannot exit stack!") ;} int val = arr [top]; top--; return val;} public void show () {if (isEmpty ()) {System.out.println ("No data"); return;} for (int I = top; I > = 0; iMel -) {System.out.print (arr [I] + "\ t");} System.out.println () } / * determine whether it is an operator * @ param oper incoming character * @ return if the operator returns true, otherwise it returns false * / public boolean isOper (char oper) {return oper = ='+'| oper = ='-'| | oper = ='*'| oper = ='/' } / * determine the priority of the operator * @ param oper the priority passed in * @ return is 1 return * / public int priority (int oper) {if (oper = ='*'| | oper = ='/') {return 1;} else if (oper ='+'| | oper = ='-') {return 0;} else {return-1 }} / / calculation method public int cal (int num1,int num2,int oper) {int res = 0; switch (oper) {case'+': res = num1 + num2; break; case'-': res = num2-num1; break; case'*': res = num1 * num2; break; case'/': res = num2 / num1;} return res }} these are all the contents of this article entitled "how to use simple Calculator in Java Stack". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 299

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report