Консольный ввод/вывод
parse из main, но возникает проблема того что main и parse не могут быть статичны и не статичны одновременно.
Помогите пожалуйста решить возникшие у меня вопросы.
Код:
import java.util.Collections;
import java.util.Stack;
import java.util.StringTokenizer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.LinkedList;
import java.text.ParseException;
public class Main {
private final String[] FUNCTIONS = {"abs", "acos", "arg", "asin", "atan", "conj", "cos", "cosh", "exp", "imag", "log", "neg", "pow", "real", "sin", "sinh", "sqrt", "tan", "tanh"};
private final String OPERATORS = "+-*/";
private final String SEPARATOR = ",";
private final String IMAGINARY = "I";
private final String VARIABLE = "var";
private Stack<String> stackOperations = new Stack<String>();
private Stack<String> stackRPN = new Stack<String>();
private Stack<String> stackAnswer = new Stack<String>();
public static void main(String[] args)
{
System.out.print("Результат");
// System.out.print();
}
private boolean isNumber(String token) {
try {
Double.parseDouble(token);
} catch (Exception e) {
if (token.contains(IMAGINARY) || token.equals(VARIABLE)) {
return true;
}
return false;
}
return true;
}
private boolean isFunction(String token) {
for (String item : FUNCTIONS) {
if (item.equals(token)) {
return true;
}
}
return false;
}
private boolean isSeparator(String token) {
return token.equals(SEPARATOR);
}
private boolean isOpenBracket(String token) {
return token.equals("(");
}
private boolean isCloseBracket(String token) {
return token.equals(")");
}
private boolean isOperator(String token) {
return OPERATORS.contains(token);
}
private byte getPrecedence(String token) {
if (token.equals("+") || token.equals("-")) {
return 1;
}
return 2;
}
public void parse(String expression) throws ParseException{
stackOperations.clear();
stackRPN.clear();
expression = expression.replace(" ", "").replace("(-", "(0-")
.replace(",-", ",0-");
if (expression.charAt(0) == '-') {
expression = "0" + expression;
}
StringTokenizer stringTokenizer = new StringTokenizer(expression,
OPERATORS + SEPARATOR + "()", true);
while (stringTokenizer.hasMoreTokens()) {
String token = stringTokenizer.nextToken();
if (isSeparator(token)) {
while (!stackOperations.isEmpty()
&& !isOpenBracket(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
} else if (isOpenBracket(token)) {
stackOperations.push(token);
} else if (isCloseBracket(token)) {
while (!stackOperations.isEmpty()
&& !isOpenBracket(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
stackOperations.pop();
if (!stackOperations.isEmpty()
&& isFunction(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
} else if (isOperator(token)) {
while (!stackOperations.isEmpty()
&& isOperator(stackOperations.lastElement())
&& getPrecedence(token) <= getPrecedence(stackOperations
.lastElement())) {
stackRPN.push(stackOperations.pop());
}
stackOperations.push(token);
} else if (isFunction(token)) {
stackOperations.push(token);
}
}
while (!stackOperations.isEmpty()) {
stackRPN.push(stackOperations.pop());
}
Collections.reverseOrder();
}
}
import java.util.Stack;
import java.util.StringTokenizer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.LinkedList;
import java.text.ParseException;
public class Main {
private final String[] FUNCTIONS = {"abs", "acos", "arg", "asin", "atan", "conj", "cos", "cosh", "exp", "imag", "log", "neg", "pow", "real", "sin", "sinh", "sqrt", "tan", "tanh"};
private final String OPERATORS = "+-*/";
private final String SEPARATOR = ",";
private final String IMAGINARY = "I";
private final String VARIABLE = "var";
private Stack<String> stackOperations = new Stack<String>();
private Stack<String> stackRPN = new Stack<String>();
private Stack<String> stackAnswer = new Stack<String>();
public static void main(String[] args)
{
System.out.print("Результат");
// System.out.print();
}
private boolean isNumber(String token) {
try {
Double.parseDouble(token);
} catch (Exception e) {
if (token.contains(IMAGINARY) || token.equals(VARIABLE)) {
return true;
}
return false;
}
return true;
}
private boolean isFunction(String token) {
for (String item : FUNCTIONS) {
if (item.equals(token)) {
return true;
}
}
return false;
}
private boolean isSeparator(String token) {
return token.equals(SEPARATOR);
}
private boolean isOpenBracket(String token) {
return token.equals("(");
}
private boolean isCloseBracket(String token) {
return token.equals(")");
}
private boolean isOperator(String token) {
return OPERATORS.contains(token);
}
private byte getPrecedence(String token) {
if (token.equals("+") || token.equals("-")) {
return 1;
}
return 2;
}
public void parse(String expression) throws ParseException{
stackOperations.clear();
stackRPN.clear();
expression = expression.replace(" ", "").replace("(-", "(0-")
.replace(",-", ",0-");
if (expression.charAt(0) == '-') {
expression = "0" + expression;
}
StringTokenizer stringTokenizer = new StringTokenizer(expression,
OPERATORS + SEPARATOR + "()", true);
while (stringTokenizer.hasMoreTokens()) {
String token = stringTokenizer.nextToken();
if (isSeparator(token)) {
while (!stackOperations.isEmpty()
&& !isOpenBracket(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
} else if (isOpenBracket(token)) {
stackOperations.push(token);
} else if (isCloseBracket(token)) {
while (!stackOperations.isEmpty()
&& !isOpenBracket(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
stackOperations.pop();
if (!stackOperations.isEmpty()
&& isFunction(stackOperations.lastElement())) {
stackRPN.push(stackOperations.pop());
}
} else if (isOperator(token)) {
while (!stackOperations.isEmpty()
&& isOperator(stackOperations.lastElement())
&& getPrecedence(token) <= getPrecedence(stackOperations
.lastElement())) {
stackRPN.push(stackOperations.pop());
}
stackOperations.push(token);
} else if (isFunction(token)) {
stackOperations.push(token);
}
}
while (!stackOperations.isEmpty()) {
stackRPN.push(stackOperations.pop());
}
Collections.reverseOrder();
}
}