Commit 1908db28 authored by Dominik Gruntz's avatar Dominik Gruntz
Browse files

04_State: float converter solutions

parent dd110d6d
package patterns.state.parser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FloatConverter2 {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
while(s != null && s.length() > 0) {
try {double d = parseFloat(s); System.out.println(d); }
catch(IllegalArgumentException e) { System.out.println("Illegal Format"); }
s = r.readLine();
}
}
static interface State {
default State handleDigit(FloatData data, int val) { return ERROR; }
default State handleE(FloatData data) { return ERROR; }
default State handleDot(FloatData data) { return ERROR; }
default State handlePlus(FloatData data) { return ERROR; }
default State handleMinus(FloatData data) { return ERROR; }
}
static class S0 implements State {
@Override public State handleDigit(FloatData data, int value) { data.m = value; return S1; }
@Override public State handleDot(FloatData data) { return S2; }
}
static class S1 implements State {
@Override public State handleDigit(FloatData data, int value) { data.m = 10*data.m + value; return S1; }
@Override public State handleDot(FloatData data) { return S3; }
@Override public State handleE(FloatData data) { return S4; }
}
static class S2 implements State {
@Override public State handleDigit(FloatData data, int value) { data.m = data.m + value/data.quo; data.quo = 10*data.quo; return S3; }
}
static class S3 implements State {
@Override public State handleDigit(FloatData data, int value) { data.m = data.m + value/data.quo; data.quo = 10*data.quo; return S3; }
@Override public State handleE(FloatData data) { return S4; }
}
static class S4 implements State {
@Override public State handlePlus(FloatData data) { return S5; }
@Override public State handleMinus(FloatData data) { data.exp_sign = -1; return S5; }
@Override public State handleDigit(FloatData data, int value) { data.exp = value; return S6; }
}
static class S5 implements State {
@Override public State handleDigit(FloatData data, int value) { data.exp = value; return S6; }
}
static class S6 implements State {
@Override public State handleDigit(FloatData data, int value) { data.exp = 10*data.exp + value; return S6; }
}
static class ErrorState implements State {}
private static final State ERROR = new ErrorState();
private static final State S0 = new S0();
private static final State S1 = new S1();
private static final State S2 = new S2();
private static final State S3 = new S3();
private static final State S4 = new S4();
private static final State S5 = new S5();
private static final State S6 = new S6();
static class FloatData {
double m = 0, quo=10;
int exp = 0, exp_sign = 1;
double getValue() { return m * Math.pow(10, exp_sign * exp); }
}
private static double parseFloat(String str) {
State s = S0;
FloatData data = new FloatData();
int pos = 0;
while(s != ERROR && pos < str.length()) {
char ch = str.charAt(pos++);
if(Character.isDigit(ch)) s = s.handleDigit(data, Character.getNumericValue(ch));
else if(ch == '.') s = s.handleDot(data);
else if(ch == '+') s = s.handlePlus(data);
else if(ch == '-') s = s.handleMinus(data);
else if(ch == 'E') s = s.handleE(data);
else if(ch == 'e') s = s.handleE(data);
else s = ERROR;
}
if (s == S3 || s == S6) {
return data.getValue();
} else {
throw new IllegalArgumentException();
}
}
}
package patterns.state.parser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FloatConverter3 {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
while(s != null && s.length() > 0){
try {double d = parseFloat(s); System.out.println(d);}
catch(IllegalArgumentException e){System.out.println("Illegal Format");}
s = r.readLine();
}
}
enum State {
S0() {
@Override public State handleDigit(FloatData data, int value) { data.m = value; return S1; }
@Override public State handleDot(FloatData data){return S2;}
},
S1() {
@Override public State handleDigit(FloatData data, int value) { data.m = 10*data.m + value; return S1; }
@Override public State handleDot(FloatData data) { return S3; }
@Override public State handleE(FloatData data) { return S4; }
},
S2() {
@Override public State handleDigit(FloatData data, int value) { data.m = data.m + value/data.quo; data.quo = 10*data.quo; return S3; }
},
S3() {
@Override public State handleDigit(FloatData data, int value) { data.m = data.m + value/data.quo; data.quo = 10*data.quo; return S3; }
@Override public State handleE(FloatData data){return S4;}
},
S4() {
@Override public State handlePlus(FloatData data) { return S5; }
@Override public State handleMinus(FloatData data) { data.exp_sign = -1; return S5; }
@Override public State handleDigit(FloatData data, int value) { data.exp = value; return S6; }
},
S5() {
@Override public State handleDigit(FloatData data, int value) { data.exp = value; return S6; }
},
S6() {
@Override public State handleDigit(FloatData data, int value) { data.exp = 10*data.exp + value; return S6; }
},
ERROR;
public State handleDigit(FloatData data, int value) { return ERROR; }
public State handleE(FloatData data) { return ERROR; }
public State handleDot(FloatData data) { return ERROR; }
public State handlePlus(FloatData data) { return ERROR; }
public State handleMinus(FloatData data) { return ERROR; }
}
static class FloatData {
double m = 0, quo=10;
int exp = 0, exp_sign = 1;
double getValue() { return m * Math.pow(10, exp_sign * exp); }
}
private static double parseFloat(String str) {
State s = State.S0;
FloatData data = new FloatData();
int pos = 0;
while(s != State.ERROR && pos < str.length()) {
char ch = str.charAt(pos++);
if(Character.isDigit(ch)) s = s.handleDigit(data, Character.getNumericValue(ch));
else if(ch == '.') s = s.handleDot(data);
else if(ch == '+') s = s.handlePlus(data);
else if(ch == '-') s = s.handleMinus(data);
else if(ch == 'E') s = s.handleE(data);
else if(ch == 'e') s = s.handleE(data);
else s = State.ERROR;
}
if(s == State.S3 || s == State.S6) {
return data.getValue();
}
else {
throw new IllegalArgumentException();
}
}
}
package patterns.state.parser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FloatConverter4 {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
while(s != null && s.length() > 0){
try {double d = parseFloat(s); System.out.println(d);}
catch(IllegalArgumentException e){System.out.println("Illegal Format");}
s = r.readLine();
}
}
private static interface Context {
void setState(State s);
FloatData getData();
}
private static class FloatData {
double m = 0, quo=10;
int exp = 0, exp_sign = 1;
double getValue(){return m * Math.pow(10, exp_sign * exp);}
}
private static interface State {
void handleDigit(int val);
void handleE();
void handleDot();
void handlePlus();
void handleMinus();
}
private static abstract class AbstractState implements State {
protected final Context c;
protected AbstractState(Context c) {
this.c = c;
}
@Override public void handleDigit(int value) { c.setState(ERROR); }
@Override public void handleE() { c.setState(ERROR); }
@Override public void handleDot() { c.setState(ERROR); }
@Override public void handlePlus() { c.setState(ERROR); }
@Override public void handleMinus() { c.setState(ERROR); }
}
private static class S0 extends AbstractState {
S0(Context c) { super(c); }
@Override public void handleDigit(int value) { c.getData().m = value; c.setState(S1); }
@Override public void handleDot() { c.setState(S2); }
}
private static class S1 extends AbstractState {
S1(Context c) { super(c); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.m = 10*data.m + value;}
@Override public void handleDot() { c.setState(S3);}
@Override public void handleE() { c.setState(S4);}
}
private static class S2 extends AbstractState {
S2(Context c) { super(c); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.m = data.m + value/data.quo; data.quo = 10*data.quo; c.setState(S3);}
}
private static class S3 extends AbstractState {
S3(Context c) { super(c); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.m = data.m + value/data.quo; data.quo = 10*data.quo; }
@Override public void handleE() { c.setState(S4);}
}
private static class S4 extends AbstractState {
S4(Context c) { super(c); }
@Override public void handlePlus() { c.setState(S5);}
@Override public void handleMinus() { FloatData data = c.getData(); data.exp_sign = -1; c.setState(S5); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.exp = value; c.setState(S6); }
}
private static class S5 extends AbstractState {
S5(Context c) { super(c); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.exp = value; c.setState(S6); }
}
private static class S6 extends AbstractState {
S6(Context c) { super(c); }
@Override public void handleDigit(int value) { FloatData data = c.getData(); data.exp = 10*data.exp + value; }
}
private static class ErrorState extends AbstractState {
ErrorState(Context c) { super(c); }
}
private static final FloatData data = new FloatData();
private static final Context c = new Context(){
@Override
public void setState(State s) {
FloatConverter4.s = s;
}
@Override
public FloatData getData() {
return data;
}
};
private static final State ERROR = new ErrorState(c);
private static final State S0 = new S0(c);
private static final State S1 = new S1(c);
private static final State S2 = new S2(c);
private static final State S3 = new S3(c);
private static final State S4 = new S4(c);
private static final State S5 = new S5(c);
private static final State S6 = new S6(c);
private static State s;
private static double parseFloat(String str) {
s = S0;
int pos = 0;
while(s != ERROR && pos < str.length()) {
char ch = str.charAt(pos++);
if(Character.isDigit(ch)) s.handleDigit(Character.getNumericValue(ch));
else if(ch == '.') s.handleDot();
else if(ch == '+') s.handlePlus();
else if(ch == '-') s.handleMinus();
else if(ch == 'E') s.handleE();
else if(ch == 'e') s.handleE();
else c.setState(ERROR);
}
if(s == S3 || s == S6) {
return data.getValue();
}
else {
throw new IllegalArgumentException();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment