11070 - The Good Old Times
Posted: Sat Aug 12, 2006 2:54 pm

Code: Select all
--9
-1-1+9/3
---9
-9--+-9
Yes, it is.ivan.cu wrote:it this correct?
How does the expresion '5*-2' make no sense? I don't understand.trulo17 wrote:i guess most of the input given here don't appear in judge's input, i got ac withouth considering such cases(in fact, i consider unary operators will appear only at th beginning of a line, after all expresions like 5*-2 doesn't make sense)
Code: Select all
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
class Main {
public static void main(String[] args) {
// try {
// System.setIn(new FileInputStream("input.txt"));
// } catch (FileNotFoundException e1) {
// }
boolean end = false;
String n = "";
try {
n = Consola.readLine();
} catch (Exception e) {
end = true;
}
Main m = new Main();
while (!end) {
System.out.println(m.doIt(n));
try {
n = Consola.readLine();
} catch (Exception e) {
end = true;
}
}
}
private String doIt(String c) {
Expr t = new Expr(c);
return format("" + t.eval());
}
private String format(String a) {
String r = "";
int i = 0, l = a.length();
boolean decimal = false;
while (i < l) {
r += a.charAt(i);
if (a.charAt(i) == '.' || a.charAt(i) == ',') {
decimal = true;
if (i + 1 < l)
r += a.charAt(i + 1);
else
r += "0";
if (i + 2 < l)
r += a.charAt(i + 2);
else
r += "0";
if (i + 3 < l)
r += a.charAt(i + 3);
else
r += "0";
break;
}
i++;
}
if (!decimal)
r += ".000";
return r;
}
}
class Expr {
char o;
double v;
Expr r, d;
public double valueOf(String n) {
int l = n.length();
int i = 0;
int a = 0;
int d = 0;
int cd = 1;
boolean dc = false;
while (i < l) {
if (n.charAt(i) == '.') {
dc = true;
} else {
if (dc) {
a = a * 10 + (n.charAt(i) - '0');
cd *= 10;
} else {
d = d * 10 + (n.charAt(i) - '0');
}
}
i++;
}
if (dc)
return 1.0d * d + 1.0 * a / cd;
else
return 1.0d * d;
}
public Expr(String c) {
int i = c.length() - 1;
while (i >= 0 && c.charAt(i) != '+' && c.charAt(i) != '-') {
i--;
do {
i--;
} while (i >= 0
&& (c.charAt(i) == '+' || c.charAt(i) == '-'
|| c.charAt(i) == '*' || c.charAt(i) == '/'));
i++;
}
if (i > 0 && (c.charAt(i) == '+' || c.charAt(i) == '-')) {
o = c.charAt(i);
r = new Expr(c.substring(0, i));
d = new Expr(c.substring(i + 1));
} else {
i = c.length() - 1;
while (i >= 0 && c.charAt(i) != '*' && c.charAt(i) != '/')
i--;
if (i >= 0 && (c.charAt(i) == '*' || c.charAt(i) == '/')) {
o = c.charAt(i);
r = new Expr(c.substring(0, i));
d = new Expr(c.substring(i + 1));
} else {
o = 'v';
char in = c.charAt(0);
int p = 1;
while (in == '-' || in == '+') {
c = c.substring(1);
p *= in == '-' ? -1 : 1;
in = c.charAt(0);
}
v = p * Double.valueOf(c).doubleValue();
}
}
}
public double eval() {
switch (o) {
case 'v':
return v;
case '+':{
double aa = r.eval();
double bb = d.eval();
return aa + bb;
}
case '-':
return r.eval() - d.eval();
case '*':
return r.eval() * d.eval();
case '/':
return r.eval() / d.eval();
}
return -1;
}
}
class Consola {
private static int maxLongitud = 256;
private static byte[] lin = new byte[maxLongitud];
public static String readLine() throws Exception {
int lg = 0, car = -1;
try {
while (true) {
car = System.in.read();
if ((car < 0 && lg == 0)
|| ((car < 0) || (car == '\r') || (car == '\n'))
&& lg != 0) {
break;
} else if (!((car < 0) || (car == '\r') || (car == '\n'))) {
lin[lg++] = (byte) car;
}
}
} catch (Exception e) {
throw (null);
}
if ((car < 0) && (lg == 0))
throw (null); // eof
return (new String(lin, 0, lg));
}
public static String readWord() throws Exception {
int lg = 0, car = -1;
try {
while (true) {
car = System.in.read();
if ((car < 0 && lg == 0) || (car <= 32 && lg != 0)) {
break;
} else if (!(car <= 32)) {
lin[lg++] = (byte) car;
}
}
} catch (Exception e) {
throw null;
}
if ((car < 0) && (lg == 0)) {
throw null; // eof
}
lin[lg] = '\0';
return (new String(lin, 0, lg));
}
public static int readInt() throws Exception {
String read = readWord();
if (read.equals(null)) {
throw null;
}
return Integer.parseInt(read);
}
}
Try:ivan.cu wrote:Code: Select all
//import java.io.FileInputStream; //import java.io.FileNotFoundException; ...
Code: Select all
-10*0
Code: Select all
0.000
Code: Select all
-0.000