package javaapplication1 import java util class Parse private final Ma

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package javaapplication1;
import java.util.*;
class Parse
{
private final Map <String, Integer> vars;
private Deque <String> lex;
Parse(Map <String, Integer> q, Deque <String> e)
{
vars = q;
lex = e;
}
private int parseTerm() throws Exception
{
int finish = parse2();
String analys;
String value1 = lex.peek();
while (lex.size() > 0)
{
if ((analys = value1).equals("*"))
{
lex.poll();
finish *= parse2();
}
if (analys.equals("/"))
{
lex.poll();
finish /= parse2();
}
else break;
}
return finish;
}
private int parse2() throws Exception
{
String analys = lex.poll();
String value2 = lex.peek();
if (Character.isDigit(analys.charAt(0))) return Integer.parseInt(analys);
else if (Character.isLetter(analys.charAt(0))) return vars.get(analys);
else if (analys.equals("-")) return -parse2();
else if (analys.equals("("))
{
int finish = parseTerm();
while (lex.size() > 1)
{
if ((analys = value2).equals("+"))
{
lex.poll();
finish += parseTerm();
}
if (analys.equals("-"))
{
lex.poll();
finish -= parseTerm();
}
if (Character.isLetterOrDigit(analys.charAt(0)) || analys.equals("(")) throw new Exception("Expected operator. Found: " + analys);
else break;
}
if (!lex.poll().equals(")")) throw new Exception("Expected ')'");
else return finish;
}
else throw new Exception("Bad lexem: " + analys);
}
int operations() throws Exception
{
int finish = parseTerm();
String analys;
while (lex.size() > 1)
{
if ((analys = lex.peek()).equals("+"))
{
lex.poll();
finish += parseTerm();
}
if (analys.equals("-"))
{
lex.poll();
finish -= parseTerm();
}
if (Character.isLetterOrDigit(analys.charAt(0))) throw new Exception("Expected operator. Found: " + analys);
if (analys.equals("(")) throw new Exception("Expected operator. Found: " + analys);
else break;
}
return finish;
}
}
public class Calc
{
private static Map <String, Integer> vars = new HashMap<>();
private static Scanner in = new Scanner(System.in);
private static Deque <String> lexer(String s)
{
Deque <String> lexems = new ArrayDeque<>();
int i = 0;
while (i < s.length() - 1)
{
char a = s.charAt(i);
if (a == ' ') continue;
else if (Character.isDigit(a))
{
String token = new String();
while (Character.isDigit(a))
{
token += (a + "");
i++;
a = s.charAt(i);
}
lexems.add(token);
i--;
}
else if (Character.isLetter(a))
{
String token = new String();
while (Character.isLetterOrDigit(a))
{
token += (a + "");
i++;
a = s.charAt(i);
}
if (!vars.containsKey(token)) vars.put(token, in.nextInt());
lexems.add(token);
i--;
}
else lexems.add(Character.toString(a));
i++;
}
return lexems;
}
public static void main(String args[])
{
String s = in.nextLine() + " ";
Deque <String> lexems = lexer(s);
try { System.out.println((new Parse(vars, lexems)).operations()); }
catch (Exception Exc) {}
}
}
class inFinal //не используется
{
private boolean error = false;
private int value;
public int getValue()
{
return value;
}
public String toString()
{
return "" + value;
}
}