import java util ArrayList import java util class Var String string in

  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
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.ArrayList;
import java.util.*;
class Var{
String string;
int value;
Var(String string, int value){
this.string = string;
this.value = value;
}
@Override
public String toString() {
return string+" "+value;
}
}
public class Calc {
private static ArrayList<Const> lex = new ArrayList<Const>();
private static ArrayList<Integer> nums = new ArrayList<Integer>();
private static ArrayList<Var> vars = new ArrayList<Var>();
private static Scanner in = new Scanner(System.in);
private static int k = 0, m = 0, n = 0, result = 0;
private static ArrayList<String> help = new ArrayList<String>();
public static void main(String[] args) throws Exception{
String s = in.nextLine();
try{
lex_analyz(s);
}
catch (Exception e){
System.out.print("error");
return;
}
try{
System.out.println(parseExpression());
}
catch (Exception e){
System.out.print("error");
}
}
enum Const{
VAR, NUM, MUL, SUB, ADD, DIV, L_BRACKET, R_BRACKET;
}
public static void lex_analyz(String s) throws Exception{
int i, j, flag, len = s.length();
for(i = 0; i < len; i++){
if(!(s.charAt(i) == ' ')){
if(s.charAt(i) == '(') lex.add(Const.L_BRACKET);
else if(s.charAt(i) == ')') lex.add(Const.R_BRACKET);
else if(s.charAt(i) == '+') lex.add(Const.ADD);
else if(s.charAt(i) == '-') lex.add(Const.SUB);
else if(s.charAt(i) == '*') lex.add(Const.MUL);
else if(s.charAt(i) == '/') lex.add(Const.DIV);
else if((int)s.charAt(i) > 47 && (int)s.charAt(i) < 58){
String temp = "";
while(i < len && (int)s.charAt(i) > 47 && (int)s.charAt(i) < 58){
temp+= s.charAt(i);
i++;
}
i--;
lex.add(Const.NUM);
int temp1 = Integer.valueOf(temp);
nums.add(temp1);
}
else{
String temp = "";
while(((int)s.charAt(i) > 64 && (int)s.charAt(i) < 91) || (int)s.charAt(i) > 96 && (int)s.charAt(i) < 122 || (int)s.charAt(i) > 47 && (int)s.charAt(i) < 58){
temp+= s.charAt(i);
i++;
if(i == len) break;
}
i--;
lex.add(Const.VAR);
for(j = 0, flag = 0; j < vars.size(); j++){
if(vars.get(j).string.equals(temp)){
flag = 1;
}
}
if(flag == 0) vars.add(new Var(temp, in.nextInt()));
help.add(temp);
}
}
}
for(i = 1; i < lex.size(); i++){
if(lex.get(i).equals(lex.get(i-1))){
if (lex.get(i).equals(Const.VAR) || lex.get(i).equals(Const.MUL) || lex.get(i).equals(Const.DIV) || lex.get(i).equals(Const.ADD) || lex.get(i).equals(Const.NUM)){
throw new Exception();
}
}
}
//System.out.println(lex);
//System.out.println(vars);
//System.out.println(nums);
}
private static int parseExpression() throws Exception {
result = parseTerm();
result = parseExpression1();
return result;
}
private static int parseTerm() throws Exception {
result = parseFactor();
result = parseTerm1();
return result;
}
private static int parseFactor() throws Exception {
if(lex.get(k).equals(Const.NUM)){
k++;
return nums.get(m++);
}
else if(lex.get(k).equals(Const.VAR)){
k++;
for(int i = 0; i < vars.size(); i++){
if(help.get(n).equals(vars.get(i).string)){
n++;
return vars.get(i).value;
}
}
}
else if(lex.get(k).equals(Const.L_BRACKET)) {
k++;
int result1 = parseExpression();
if(!lex.get(k).equals(Const.R_BRACKET)) throw new Exception();
else k++;
return result1;
}
else if(lex.get(k).equals(Const.SUB)){
k++;
return -parseFactor();
}
else throw new Exception();
return result;
}
private static int parseExpression1 () throws Exception{
int result1 = result;
while(lex.size() != k && (lex.get(k).equals(Const.ADD) || lex.get(k).equals(Const.SUB))){
if(lex.get(k).equals(Const.ADD)){
k++;
result1 +=parseTerm();
}
else{
k++;
result1 -= parseTerm();
}
}
return result1;
}
private static int parseTerm1() throws Exception{
int result1 = result;
while(lex.size() != k && (lex.get(k).equals(Const.MUL) || lex.get(k).equals(Const.DIV))){
if(lex.get(k).equals(Const.MUL)){
k++;
result1 *= parseFactor();
}
else{
k++;
result1 /= parseFactor();
}
}
return result1;
}
}