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

Added solution for Google-SimpleCalculator #7

Merged
merged 1 commit into from
Oct 31, 2020
Merged
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Maximum In A Stack | Twitter | [MaximumInAStack.java](https://github.com/emafazi
Number of Ways to Climb Stairs | LinkedIn | [ClimbingStairs.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/linkedin/ClimbingStairs.java) | Done |
Find Pythagorean Triplets | Uber | [FindPythagoreanTriplets.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/uber/FindPythagoreanTriplets.java) | Done |
Edit Distance | Airbnb | [EditDistance.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/airbnb/EditDistance.java) | To Do |
Create a Simple Calculator | Google | [CreateASimpleCalculator.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/google/CreateASimpleCalculator.java) | To Do |
Create a Simple Calculator | Google | [CreateASimpleCalculator.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/google/CreateASimpleCalculator.java) | Done |
Longest Sequence with Two Unique Numbers | Facebook | [LongestSequenceWithTwoUniqueNumbers.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/facebook/LongestSequenceWithTwoUniqueNumbers.java) | To Do |
Find Cycles in a Graph | Facebook | [FindCyclesInAGraph.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/facebook/FindCyclesInAGraph.java) | To Do |
Word Search | Amazon | [WordSearch.java](https://github.com/emafazillah/DailyInterviewProJava/blob/master/src/main/java/com/dailyinterviewprojava/amazon/WordSearch.java) | Done |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.dailyinterviewprojava.google;
import java.util.Stack;
import java.util.Scanner;

/**
*
Expand All @@ -11,6 +13,116 @@
* Output: -4
*
*/

public class CreateASimpleCalculator {

static Stack<Integer> numbersStack = new Stack<>();
static Stack<String> operationStack = new Stack<>();

public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter expression");
String userExpression = myObj.nextLine(); // Read user input
System.out.println("Expression is: " + userExpression);
System.out.println(eval(userExpression));
}

private static int eval(String expression) {
String[] splittedArr = expression.split(" ");
for (int i = 0; i < splittedArr.length; i++) {
String val = splittedArr[i];
if (isNum(val)) {
numbersStack.push(Integer.valueOf(val));
} else if (val.equals("(")) {
operationStack.push(val);
} else if (val.equals(")")) {
while (operationStack.size() > 0 && !operationStack.get(operationStack.size() - 1).equals("(")) {
doOperation();
}
operationStack.pop();
} else if (isOperator(val)) {
if (operationStack.size() > 0 && priority(operationStack.get(operationStack.size() - 1)) > priority(val)) {
doOperation();
}
operationStack.push(val);
}

}

// here we done with operations between ( )
if (numbersStack.size() > 1) {
while (operationStack.size() > 0) {
doOperation();
}
} else if (numbersStack.size() == 1) {
doOperationSingleNum();

}
return numbersStack.pop();
}

private static void doOperationSingleNum() {
String operator = operationStack.pop();
Integer num = numbersStack.pop();

if (operator.equals("-")) {
num = -num;
}
numbersStack.push(num);
}

private static int priority(String val) {
int priority;
switch (val) {
case "+":
case "-":
priority = 1;
break;
case "*":
case "/":
priority = 2;
break;
default:
priority = 0;
break;
}
return priority;
}

private static boolean isOperator(String val) {
return val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/");
}

private static void doOperation() {
if (numbersStack.size() == 1) {
doOperationSingleNum();
} else {
Integer firstNum = numbersStack.pop();
Integer lastNum = numbersStack.pop();
String operator = operationStack.pop();

int result = 0;
switch (operator) {
case "+":
result = lastNum + firstNum;
break;
case "-":
result = lastNum - firstNum;
break;
case "*":
result = lastNum * firstNum;
break;
case "/":
result = lastNum / firstNum;
break;
}
numbersStack.push(result);
}

}

private static boolean isNum(String val) {
return "0123456789".contains(val);
}
}