Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first commit add out file #11

Open
wants to merge 1 commit into
base: java
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions Project/.idea/.gitignore

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

6 changes: 6 additions & 0 deletions Project/.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 Project/.idea/modules.xml

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

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

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

11 changes: 11 additions & 0 deletions Project/Project.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
93 changes: 93 additions & 0 deletions Project/src/Main.java
Original file line number Diff line number Diff line change
@@ -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<String> tempStack = new Stack<>();//Store number or operator
Stack<Character> 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<String> 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();
}
}