/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
class steck {
private int size;
private int capacity;
public steck(){
capacity = 2;
}
public steck(int x){
capacity = x;
}
public boolean isEmpty(){
return size == 0;
}
private void resize(int x){
System.
arraycopy(date,
0, newDate,
0, size
); date = null;
date = newDate;
}
if(size == capacity){
resize(2*capacity);
}
date[size++] = x;
}
if(isEmpty()){
return null;
}
if(size < capacity/2){
resize(capacity/2);
}
size = size -1;
return temp;
}
if(isEmpty()) return null;
return date[size-1];
}
public void clear(){
size = 0;
}
public int size(){
return size;
}
public int getCapacity(){
return size;
}
}
/**
* Created by green on 20.12.15.
*/
private String operators
= "u+-*/"; private String delimiters
= operators
+ "( )"; public boolean correct = true;
private boolean isOperator
(String x
){ if(x.length() != 1) return false;
for(int i = 0; i < operators.length(); i++){
if(x.charAt(0) == operators.charAt(i)) return true;
}
return false;
}
private boolean isDelimiter
(String x
){ if(x.length() != 1) return false;
for(int i = 0; i < delimiters.length(); i++){
if(x.charAt(0) == delimiters.charAt(i)) return true;
}
return false;
}
private int priority
(String x
) { if (x.equals("(")) return 1;
if (x.equals("+") || x.equals("-")) return 2;
if (x.equals("*") || x.equals("/")) return 3;
return 4;
}
steck stack = new steck();
while (tokenizer.hasMoreTokens()) {
curr = tokenizer.nextToken();
if (!tokenizer.hasMoreTokens() && isOperator(curr)) {
postfix = ("Некорректное выражение.");
correct = false;
return postfix;
}
if (curr.equals(" ")) continue;
if (isDelimiter(curr)) {
if (curr.equals("(")) stack.push(curr);
else if (curr.equals(")")) {
while (!stack.peek().equals("(")) {
postfix += stack.pop().toString() + " ";
if (stack.isEmpty()) {
postfix = ("Скобки не согласованы.");
correct = false;
return postfix;
}
}
stack.pop();
}else {
if (curr.equals("-") && (prev.equals("") || (isDelimiter(prev) && !prev.equals(")")))) {
curr = "u";
}else{
while (!stack.isEmpty() && (priority(curr) <= priority(stack.peek().toString()))) {
postfix += stack.pop().toString() + " ";
}
}
stack.push(curr);
}
}else {
postfix += curr + " ";
}
prev = curr;
}
while (!stack.isEmpty()) {
if (isOperator(stack.peek().toString())) postfix += stack.pop().toString() + " ";
else {
postfix = "Скобки не согласованы.";
correct = false;
return postfix;
}
}
return postfix;
}
public int calc
(String postfix
) { if(!correct) return 0;
steck stack = new steck();
for (int i = 0; i < postfix.length() ; i++) {
if(postfix.charAt(i) == ' ') continue;
if (postfix.
charAt(i
) == '+') stack.
push(Integer.
valueOf(stack.
pop().
toString()) + Integer.
valueOf(stack.
pop().
toString())); else if (postfix.charAt(i) == '-') {
int b
= Integer.
valueOf(stack.
pop().
toString()), a
= Integer.
valueOf(stack.
pop().
toString()); stack.push(a - b);
}
else if (postfix.
charAt(i
) == '*') stack.
push(Integer.
valueOf(stack.
pop().
toString()) * Integer.
valueOf(stack.
pop().
toString())); else if (postfix.charAt(i) == '/') {
int b
= Integer.
valueOf(stack.
pop().
toString()), a
= Integer.
valueOf(stack.
pop().
toString()); stack.push(a / b);
}
else if (postfix.
charAt(i
) == 'u') stack.
push(-Integer.
valueOf(stack.
pop().
toString())); else{
int g = postfix.indexOf(' ',i);
stack.
push(Integer.
valueOf(postfix.
substring(i, g
))); i = g;
}
}
return Integer.
valueOf(stack.
pop().
toString()); }
}
class Ideone
{
{
Scanner in
= new Scanner
(System.
in); s = x.parse(s);
if(x.correct){
System.
out.
println(x.
calc(s
)); }else{
System.
out.
println("Ошибка:"+s
); }
}
}