From d96b98bd715f1ef5dce3ea4f696108d2b7d36d43 Mon Sep 17 00:00:00 2001
From: 2017166132 <2523833989@qq.com>
Date: Thu, 21 Nov 2019 23:11:36 +0800
Subject: [PATCH] first commit add out file
Signed-off-by: 2017166132 <2523833989@qq.com>
---
Project/.idea/.gitignore | 2 +
Project/.idea/misc.xml | 6 +++
Project/.idea/modules.xml | 8 ++++
Project/.idea/vcs.xml | 6 +++
Project/Project.iml | 11 +++++
Project/src/Main.java | 93 +++++++++++++++++++++++++++++++++++++++
6 files changed, 126 insertions(+)
create mode 100644 Project/.idea/.gitignore
create mode 100644 Project/.idea/misc.xml
create mode 100644 Project/.idea/modules.xml
create mode 100644 Project/.idea/vcs.xml
create mode 100644 Project/Project.iml
create mode 100644 Project/src/Main.java
diff --git a/Project/.idea/.gitignore b/Project/.idea/.gitignore
new file mode 100644
index 00000000..5c98b428
--- /dev/null
+++ b/Project/.idea/.gitignore
@@ -0,0 +1,2 @@
+# Default ignored files
+/workspace.xml
\ No newline at end of file
diff --git a/Project/.idea/misc.xml b/Project/.idea/misc.xml
new file mode 100644
index 00000000..05483570
--- /dev/null
+++ b/Project/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Project/.idea/modules.xml b/Project/.idea/modules.xml
new file mode 100644
index 00000000..2ff9c414
--- /dev/null
+++ b/Project/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Project/.idea/vcs.xml b/Project/.idea/vcs.xml
new file mode 100644
index 00000000..6c0b8635
--- /dev/null
+++ b/Project/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Project/Project.iml b/Project/Project.iml
new file mode 100644
index 00000000..c90834f2
--- /dev/null
+++ b/Project/Project.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Project/src/Main.java b/Project/src/Main.java
new file mode 100644
index 00000000..283eadf7
--- /dev/null
+++ b/Project/src/Main.java
@@ -0,0 +1,93 @@
+import java.util.Stack;
+
+public class Main {
+
+ private static String[] op = { "+", "-", "*", "/" };// Operation set
+ public static void main(String[] args) {
+ String question = MakeFormula();
+ System.out.println(question);
+ String ret = Solve(question);
+ System.out.println(ret);
+ }
+
+ public static String MakeFormula(){
+ StringBuilder build = new StringBuilder();
+ int count = (int) (Math.random() * 2) + 1; // generate random count
+ int start = 0;
+ int number1 = (int) (Math.random() * 99) + 1;
+ build.append(number1);
+ while (start <= count){
+ int operation = (int) (Math.random() * 3); // generate operator
+ int number2 = (int) (Math.random() * 99) + 1;
+ build.append(op[operation]).append(number2);
+ start ++;
+ }
+ return build.toString();
+ }
+
+ public static String Solve(String formula){
+ Stack tempStack = new Stack<>();//Store number or operator
+ Stack operatorStack = new Stack<>();//Store operator
+ int len = formula.length();
+ int k = 0;
+ for(int j = -1; j < len - 1; j++){
+ char formulaChar = formula.charAt(j + 1);
+ if(j == len - 2 || formulaChar == '+' || formulaChar == '-' || formulaChar == '/' || formulaChar == '*') {
+ if (j == len - 2) {
+ tempStack.push(formula.substring(k));
+ }
+ else {
+ 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);
+ }
+ }
+ }
+ k = j + 2;
+ }
+ }
+ while (!operatorStack.empty()){ // Append remaining operators
+ tempStack.push(operatorStack.pop().toString());
+ }
+ Stack calcStack = new Stack<>();
+ for(String peekChar : tempStack){ // Reverse traversing of stack
+ if(!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) {
+ calcStack.push(peekChar); // Push number to stack
+ }else{
+ int a1 = 0;
+ int b1 = 0;
+ if(!calcStack.empty()){
+ b1 = Integer.parseInt(calcStack.pop());
+ }
+ if(!calcStack.empty()){
+ a1 = Integer.parseInt(calcStack.pop());
+ }
+ switch (peekChar) {
+ case "+":
+ calcStack.push(String.valueOf(a1 + b1));
+ break;
+ case "-":
+ calcStack.push(String.valueOf(a1 - b1));
+ break;
+ case "*":
+ calcStack.push(String.valueOf(a1 * b1));
+ break;
+ default:
+ calcStack.push(String.valueOf(a1 / b1));
+ break;
+ }
+ }
+ }
+ return formula + "=" + calcStack.pop();
+ }
+}