| Signatur: | boolean bedroht(int zeile, int spalte) |
| Voraussetzung: | -- |
| Effekt: | -- |
| Ergebnis: | Aussage, ob das Feld (zeile,
spalte) der Matrix a bedroht ist, wie gegeben durch bedroht a zeile spalte: |
bedroht :: [[Bool]] -> Int -> Int -> Bool
bedroht a z s =
any (\i -> any (\j -> a !! i !! j && (i==z || j==s || abs(z-i)==abs(s-j)))
-- Feld besetzt Zeile Spalte Diagonale
[1..n] )
[1..n]
where n = length a - 1
Aufgabe 2
import java.io.*;
public class ParserExtended {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static char[] t;
static int pos = 0;
public static void main(String[] args) {
try {
System.out.print("Ausdruck eingeben (mit '.' terminiert): ");
t = br.readLine().toCharArray();
System.out.println("Wert: " + expression());
} catch (Exception e) {
System.out.println("Ein Fehler ist aufgetreten: " + e);
}
}
static double expression() throws Exception {
double result = term();
for (;;) {
switch (current()) {
case '+':
next();
result += term();
break;
case '-':
next();
result -= term();
break;
default:
return result;
}
}
}
static double term() throws Exception {
double result = factor();
for (;;) {
switch (current()) {
case '*':
next();
result *= factor();
break;
case '/':
next();
result /= factor();
break;
default:
return result;
}
}
}
static double factor() throws Exception {
double result = power();
if (current() == '^') {
next();
return Math.pow(result, factor());
} else
return result;
}
static double power() throws Exception {
double result = 0;
if (current() == '(') {
next();
result = expression();
if (current() == ')') {
next();
return result;
} else
throw new Exception("parse error (power)");
} else
return number();
}
static int number() throws Exception {
if (current() < '0' || '9' < current())
throw new Exception("parse error (number)");
int result = 0;
do {
result *= 10;
result += current() - '0';
next();
} while ('0' <= current() && current() <= '9');
return result;
}
static char current() {
return t[pos];
}
static void next() {
pos++;
}
}