DEA Eingabewort

qwertz123

Mitglied
Hallo zusammen ich habe mal eine Frage bei einem DEA soll es drei Zustände geben anfang akzeptiert und fehler. Die Eingabezeichen am Anfang sind A,E,1,0 bei A springt er zu akzeptiert und bei den drei anderen zum fehler. An den jeweiligen neuen Zuständen wird dann immer wieder abgefragt. Bei Akzeptiert bleibt der zustand bei 0,1 bestehen bei A,E springt er zum Fehler. Beim Fehler ist er immer in einer endlosschleife also egal was man dann eingibt es bleibt beim fehler!

Das sollen man nun in java Programmieren ich würde da erstmal eine if-abfrage machen und wenn die eingabe = a ist dann ein system.out machen mit akzeptiert oder else hat fehler. Nur wie kann man jetzt Buchstaben und zahlen erst mal einlesen ?

wie es dann weiter geht muss ich mir dann auch noch mal genau überlegen!
Würde gern wissen wie ihr das machen würdet und wenn die idee mit der if abfrage richtig ist wie ihr das dann einlesen würdet??
Vielen dank im Vorraus
 
G

Gast2

Gast
Fang mit einem Scanner an

Java:
public class DEA{
  public static void main(String[] args){
    		Scanner scanner  = new Scanner(System.in);
		System.out.print("Inmput: ");
		String input = scanner.next();
  }

}
 
G

Gast2

Gast
Was spricht gegen

Java:
if (eingabe.equals("a")){

Ob du jetzt den char oder einen String nimmst ist da egal, sonst kannst du auch einen char im Scanner lesen
 
G

Gast2

Gast
Ich hab mal ein Beispiel (reine langeweile, und hatte ähnliche Aufgaben mal) zusammen geknipst, wenn du überhaupt nicht weiterweißt guck es dir an, aber probier es erstmal selber

Java:
import java.util.Scanner;

/**
 * DEA accepting the symbols A,E,1,0
 */
public class DEA {

	public static final String START = "START";
	public static final String ERROR = "ERROR";
	public static final String ACCEPTED = "ACCEPTED";
	public static final String UNDEFINED = "UNDEFINED";
	
	private String state = START;

	/**
	 * Reads a sequence of symbols from the stdin/console
	 * @return array of symbols
	 */
	private char[] getInput() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input: ");
		String input = scanner.next();
		char[] chars = input.toCharArray();
		return chars;
	}

	/**
	 * Processes a sequence of input symbols
	 * @param input the sequence of input symbols
	 * @return true if the DEA is in the ACCEPTED state after consuming all symbols, false otherwise
	 */
	private boolean accepted(char[] input) {
		boolean accepted = true;
		for (char c : input) {
			String currentState = this.state;
			try {
				if (consume(c)) {
					System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
				} else {
					System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
					// now we are in the error state, since we can not
					// leave again, just return that we are not happy
					// with the input, if all symbols should be checked 
					// only set accepted = false;
					//return false; // skip further error transitions
					accepted = false; // yes, i need to see all of them
				}
			} catch (TokenNotAcceptedException e) {
				System.out.println("Consumed symbol "+c+" -> transition from "+currentState+" to "+this.state);
				// undefined symbol used - not part of our alphabet,
				// so this is not accepted and crashes our DEA
				return false;
			}
		}
		return accepted;
	}

	/**
	 * Consumes a single symbol
	 * @param c the symbol to consume
	 * @return true if the consuming the symbol does not lead to an ERROR state
	 * @throws TokenNotAcceptedException if the symbol is not part of the alphabet of this DEA
	 */
	private boolean consume(char c) throws TokenNotAcceptedException {
		if (c != 'A' && c != 'E' && c != '1' && c != '0') {
			this.state = UNDEFINED;
			throw new TokenNotAcceptedException("Token " + c
					+ " not accepted by this DEA");
		}
		if (this.state.equals(START)) {
			if (c == 'A' || c == '1' || c == '0') {
				this.state = ACCEPTED;
				return true;
			} else {
				// if (c == 'E') 
			  	this.state = ERROR;
            	return false;
			}
		} else if (this.state.equals(ERROR)) {
			this.state = ERROR; // no need to check anymore, we are in an error
								// state and will stay here until the end;
			return false;
		} else if (this.state.equals(ACCEPTED)) {
            if (c == 'E') {
            	this.state = ERROR;
            	return false;
            } else {
            	// nothing to do, A, 0, 1 are ok
            	return true;
            }
		}
		return false;
	}

	/**
	 * starts this DEA
	 */
	private void start() {
		char[] chars = getInput();
		boolean accepted = accepted(chars);
		System.out.println("Input " + (accepted ? "accepted" : "not accepted")+" -> final State is " + this.state);
	}

	
	public static void main(String[] args) {
		DEA dea = new DEA();
		dea.start();
	}
	
	/**
	 * Exception used if a symbol is not part of the alphabet
	 */
	static class TokenNotAcceptedException extends Exception {
		public TokenNotAcceptedException(String message) {
			super(message);
		}
	}
}
 
G

Gast2

Gast
Kannst du auch beliebig kürzen. Der DEA hier ist halt schon so aufgebaut das du ihn fast für alle beliebigen DEAs nutzen kannst. Musst nur die consume Methode umbauen. Der rest bleibt mehr oder minder gleich.

Hat ungefähr 15 minuten gedauert inkl den Kommentaren, im Endeffekt sind es ja nur ~50 Zeilen Code. So gesehn passt das ganz gut in die klausur rein ;)
 

qwertz123

Mitglied
also ich hatte das mal so versucht aber der springt garnicht erst in die for-schleife hab mit dem programmieren im allg. noch voll die probleme.


import java.util.*;
public class Aufgabe_II_18_9_07 {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.print("Input: ");
String input = scanner.next();
if (input.equals("a")){
System.out.println("Akzeptiert");

String input2="b";

for(; input2==("a")| input2==("e");)
{
System.out.println("Neue eingabe");
input2 = scanner.next();
System.out.println(input2);
}
System.out.println("Fehler");
}
else
System.out.println("Fehler");
}
}
 

Oben