Home > Back-end >  Novice, how to enter a formula to Java to output the result of it
Novice, how to enter a formula to Java to output the result of it

Time:09-29


If I wrote it complains

CodePudding user response:

 
Package stack;

/* *
* Created with IntelliJ IDEA.
*
* @ author: heiye
* @ Date: 2020/3/23
* @ Time:
then* Description: No Description
*/
Public class OperationStackDemo {
Public static void main (String [] args) {
//infix expression
String expression="3 * 7 + 1 to 25 + 4/2";
//define the number and symbol stack
OperStack numStack=new OperStack (10);
OperStack OperStack=new OperStack (10);

//define variables operation need
int index=0;
char ch;
Int res;
String keepNum="";
While (true) {
//traversal access to every single character in the string
Ch=expression. The substring (index, + + index). The charAt (0);
//it is take the character of digital or operator
{if (operStack. IsOper (ch))
//if the operator, you will need to determine whether the current symbol stack exists operator
if (! OperStack. IsEmpty ()) {
//if there is need to determine whether the current operator operator then smaller than that in the stack operator
Int oper=operStack. Peek ();
If (operStack. OperFlag (ch) & lt;=operStack. OperFlag (oper)) {
//if less than you need to pop up the stack operations operator
Operation (operStack numStack);
//the stack will be added to the current operator symbol
OperStack. Push (ch);
} else {
//if the is equal to or greater than the direct deposit can
OperStack. Push (ch);
}
} else {
//if the null is directly inserted into the
OperStack. Push (ch);
}
} else {
//if the digital direct deposit to the number of stack
//implementation a number of digital computing
KeepNum +=ch;
If (index & gt;=expression. The length ()) {
NumStack. Push (Integer. ParseInt (keepNum));
} else {
If (operStack. IsOper (expression. CharAt (index))) {
NumStack. Push (Integer. ParseInt (keepNum));
KeepNum="";
}
}
}

//determine whether intercept out
If (index & gt;=expression. The length ()) {
break;
}
}

//pop-up number and symbol stack respectively calculated
While (true) {
//if the stack is empty, the number of the stack is the result of final number
if (! OperStack. IsEmpty ()) {
//calculate
Operation (operStack numStack);
} else {
//is empty is getting the results
Res=numStack. Prop ();
break;
}
}

System. The out. Println (" the final computation results: "+ res);
}

Public static void operation (OperStack OperStack, OperStack numStack) {
Int oper=operStack. Prop ();
Int num1=numStack. Prop ();
Int num2=numStack. Prop ();

//will result in the stack in the
Int res=operStack. Operation (num1, num2, oper);
NumStack. Push (res);
}
}

The class OperStack {
Private int maxSize;
Private int [] stack;
Private int top;

Public OperStack (int maxSize) {
this.maxSize=maxSize;
Stack=new int [this maxSize];
Top=1;
}

/* *
* determine whether stack empty
*
* @ return
*/
Public Boolean isEmpty () {
Return the top==1;
}

/* *
* stack full
*
* @ return
*/
Public Boolean isFull () {
Return the top==this. MaxSize - 1;
}

/* *
* data is added to the stack
*
* @ param data
*/
Public void push (int data) {
//determine whether full before you add
If (isFull ()) {
System. Out.println (" stack is full!" );
return;
}
Top++;
Stack [top]=data;
}

/* *
* flare stack
*
* @ return
*/
Public int prop () {
//determine whether empty before you play
If (isEmpty ()) {
System. Out.println (" stack is empty!" );
return 0;
}
The int value=https://bbs.csdn.net/topics/stack [top];
Top -;
return value;
}

/* *
* view the stack
*/
Public void show () {
If (isEmpty ()) {
System. Out.println (" stack is empty!" );
return;
}
Int I=top;
While (I & gt; 1) {
System. The out. Println (" stack data: "+ stack [I]);
I -;
}
}

/* *
* determine whether symbol
*
* @ param ch
*/
Public Boolean isOper (char ch) {
Return ch=='*' | | ch=='/' | | ch=='+' | | ch=='-';
}

/* *

* arithmetic method*
* @ param num1
* @ param num2
* @ param oper
* @ return
*/
Public int operation (int num1, int num2, int oper) {
int res=0;
The switch (oper) {
Case: '*'
Res=num1 * num2;
break;
Case '/' :
Res=num2/num1;
break;
Case: '+'
Res=num1 + num2;
break;
In case the '-' :
Res=num2 - num1;
break;
Default:
System. The out. Println (" temporarily does not support the operator ~ ");
break;
}
return res;
}

/* *
* judgment symbol priority
*
* @ param ch
* @ return
*/
Public int operFlag int (ch) {
Char [] chars={' + ', '-' and '*', '/'};
Int ZERO equals ZERO, ONE,=1, TWO=2, THREE=3;
If (ch==chars [TWO] | | ch==chars [THREE]) {
return 1;
} else if (ch==chars (ZERO) | | ch==chars [ONE]) {
return 0;
} else {
return -1;
}
}

/* *
* access to the first element on the top of the stack
*
* @ return
*/
Public int peek () {
The return stack [top];
}
}


Just before you have written a infix expression calculator small case

CodePudding user response:

The string to replace the keyboard