Last active
March 21, 2016 22:01
-
-
Save alexejVasko/5c92ce7747e571547d6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package calculator; | |
import sun.applet.Main; | |
import sun.rmi.runtime.Log; | |
import java.io.*; | |
public class New { | |
public static final String OUT_FILE_PATH = "./output.txt"; | |
public static void main(String[] args) { | |
MyScanner scanner = new MyScanner(); | |
while (true) { | |
System.out.println("Please, enter Your choice 1(read from file) or 2(manual input #x + y#): "); | |
String num1 = scanner.scanString(); | |
if (num1.equals("2")) { | |
System.out.println("Please, enter the whole operation "); | |
String num2 = scanner.scanString(); | |
precondition(num2); | |
} else if (num1.equals("1")) { | |
String line = null; | |
try { | |
BufferedReader reader = new BufferedReader( | |
new FileReader(OUT_FILE_PATH)); | |
line = reader.readLine(); | |
reader.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
System.out.println(" "); | |
} | |
precondition(line); | |
} else { | |
continue; | |
} | |
} | |
} | |
public enum Actions { | |
SUM, | |
SUBTRACTION, | |
MULTIPLY, | |
DIVIDE | |
} | |
public static char getActionSymbol(Actions action) { | |
switch (action) { | |
case SUM: | |
return '+'; | |
case SUBTRACTION: | |
return '-'; | |
case MULTIPLY: | |
return '*'; | |
case DIVIDE: | |
return '/'; | |
} | |
return ' '; | |
} | |
public static double precondition(String valueToEvaluate) { | |
try { | |
if (valueToEvaluate.contains("*") || valueToEvaluate.contains("/") || | |
valueToEvaluate.contains("+") || valueToEvaluate.contains("-")) { | |
Actions currentAction = null; | |
int indexOfMultiply = valueToEvaluate.indexOf("*"); | |
int indexOfDivide = valueToEvaluate.indexOf("/"); | |
int indexOfSum = valueToEvaluate.indexOf("+"); | |
int indexOfSubtraction = valueToEvaluate.indexOf("-"); | |
if (indexOfMultiply > 0) { | |
currentAction = Actions.MULTIPLY; | |
} else if (indexOfDivide > 0) { | |
currentAction = Actions.DIVIDE; | |
} else if (indexOfSum > 0) { | |
currentAction = Actions.SUM; | |
} else if (indexOfSubtraction > 0) { | |
currentAction = Actions.SUBTRACTION; | |
} else { | |
System.err.println(""); | |
} | |
char currentActionChar = getActionSymbol(currentAction); | |
int indexOfAction = valueToEvaluate.indexOf(currentActionChar); | |
String firstPart = valueToEvaluate.substring(0, indexOfAction).trim(); | |
String secondPart = valueToEvaluate.substring(indexOfAction + 1).trim(); | |
System.out.println("FIRST: " + currentActionChar + "|" + firstPart + "|" + currentAction); | |
System.out.println("SECOND: |" + secondPart + "|"); | |
System.out.println("---------------------------------------"); | |
double value1 = Double.parseDouble(firstPart); | |
double value2 = Double.parseDouble(secondPart); | |
double result = 0; | |
if ('*' == currentActionChar) { | |
result = value1 * value2; | |
} | |
if ('/' == currentActionChar) { | |
result = value1 / value2; | |
} | |
if ('+' == currentActionChar) { | |
result = value1 + value2; | |
} | |
if ('-' == currentActionChar) { | |
result = value1 - value2; | |
} | |
System.out.println("RESULT: " + result); | |
System.out.println(); | |
return result; | |
} else { | |
System.err.println("Cannot recognize expression. Please try again(press ENTER)."); | |
} | |
} catch (Exception e) { | |
System.out.println("Перехвачено исключение # некорректный ввод выражения #"); | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
package calculator;
import java.util.Scanner;
public class MyScanner {