final parser and adding of iterations number

This commit is contained in:
Arthur 2021-02-03 10:05:31 +01:00
parent 049e3aeda7
commit cab689070c
3 changed files with 45 additions and 36 deletions

6
.gitignore vendored
View File

@ -1,2 +1,6 @@
.idea/
out/
out/
/bin/
.project
.classpath
.bin

View File

@ -22,6 +22,8 @@ public class Main {
rules.add(scanner.next());
}
rules.remove(rules.size() - 1);
System.out.println("Nombre d'itérations: ");
nbIterations = scanner.nextInt();
parser = new Parser(axiom, rules,nbIterations);
}

View File

@ -7,7 +7,7 @@ public class Parser {
private final String axiom;
private final List<String> rules;
private final int nbIterations;
private String[] validStrings = {"]","[",".","+","-","X","Y","Z","x","y","z","0","1","2","3","4","5","6","7","8","9"};
private char[] validChars = {'=',']','[','.','+','-','X','Y','Z','x','y','z','0','1','2','3','4','5','6','7','8','9',' '};
public Parser(String axiom, List<String> rules,int nbIterations) {
this.axiom = axiom;
@ -16,40 +16,43 @@ public class Parser {
}
public boolean isCorrect(){
return (rulesCorrect() || axiomCorrect());
}
private boolean axiomCorrect() {
return false;
}
public boolean rulesCorrect() {
for(String r : this.rules) {
String[] stringsplitted = r.split("");
String old = null;
int bracket = 0;
for (String rules : stringsplitted){
for(String string : validStrings){
if (rules == "[")
bracket += 1;
if(rules =="]")
bracket -=1;
if(old == "."){
for(int i = 0;i<10;i++){
if(rules == validStrings[i])
return false;
}
}
old = rules;
if(rules == string)
break;
if(string == "9" && rules != string)
return false;
}
}
if (bracket != 0)
return false;
if (nbIterations == 0) {
System.out.println("Erreur, nombre d'itérations inssufissant (plus petit que 1)");
return false;
}
return true;
boolean bl = isCorrect(axiom);
for(String rule : this.rules){
bl = bl && isCorrect(rule);
}
return bl;
}
private boolean isCorrect(String stringToCheck) {
char old = ' ';
int bracket = 0;
for (int i =0; i>stringToCheck.length();i++){
char temp = stringToCheck.charAt(i);
for(char validChar : validChars){
if (temp == '[')
bracket += 1;
if(temp ==']')
bracket -=1;
if(old == '.'){
for(int y = 0;y<12;y++){
if(temp == validChars[y])
return false;
}
}
old = temp;
if(temp == validChar)
break;
if(validChar == ' ' && temp != validChar)
return false;
}
}
if (bracket != 0)
return false;
return true;
}
}