Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions .idea/Calculator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/dictionaries/v_zhihu.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_jetbrains_annotations_java5_17_0_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_python_jython_2_7_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.huangzp.Calculator</groupId>
<artifactId>Calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.python/jython -->
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>


</project>
36 changes: 22 additions & 14 deletions src/Main.java → src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.NotNull;

import java.util.Stack;

public class Main {
Expand All @@ -10,6 +12,7 @@ public static void main(String[] args) {
System.out.println(ret);
}

@org.jetbrains.annotations.NotNull
public static String MakeFormula(){
StringBuilder build = new StringBuilder();
int count = (int) (Math.random() * 2) + 1; // generate random count
Expand All @@ -25,9 +28,10 @@ public static String MakeFormula(){
return build.toString();
}

@NotNull
public static String Solve(String formula){
Stack<String> tempStack = new Stack<>();//Store number or operator
Stack<Character> operatorStack = new Stack<>();//Store operator
Stack<String> tempStack = new Stack<String>(); // Store number or operator
Stack<Character> operatorStack = new Stack<Character>(); // Store operator
int len = formula.length();
int k = 0;
for(int j = -1; j < len - 1; j++){
Expand All @@ -37,19 +41,23 @@ public static String Solve(String formula){
tempStack.push(formula.substring(k));
}
else {
if(k < j){
// v-zhihu
if (k <= j) {
tempStack.push(formula.substring(k, j + 1));
}
if(operatorStack.empty()){
operatorStack.push(formulaChar); //if operatorStack is empty, store it
}else{
char stackChar = operatorStack.peek();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')){
operatorStack.push(formulaChar);
}else {
tempStack.push(operatorStack.pop().toString());
operatorStack.push(formulaChar);
while (true) {
if (operatorStack.empty()) {
operatorStack.push(formulaChar); //if operatorStack is empty, store it
break;
} else {
char stackChar = operatorStack.peek();
if ((stackChar == '+' || stackChar == '-')
&& (formulaChar == '*' || formulaChar == '/')) {
operatorStack.push(formulaChar);
break;
} else {
tempStack.push(operatorStack.pop().toString());
}
}
}
}
Expand All @@ -59,7 +67,7 @@ public static String Solve(String formula){
while (!operatorStack.empty()){ // Append remaining operators
tempStack.push(operatorStack.pop().toString());
}
Stack<String> calcStack = new Stack<>();
Stack<String> calcStack = new Stack<String>();
for(String peekChar : tempStack){ // Reverse traversing of stack
if(!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) {
calcStack.push(peekChar); // Push number to stack
Expand Down
Loading