Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Brainfuck/Musterloesung

Hinweis: Die Musterlösung kann von eurer Lösung abweichen, da es immer mehrere Varianten gibt ein Problem zu lösen. Im Zweifelsfall Fragt einen Tutor.

Lösung

/**
 * @author Andy Gunschl (Freitagsrunde)
 *
 */
public class Brainfuck {
	
	private static char[] workingTape ={'a','a','a','a','a','a','a','a','a','a'};
	private static int head = 0;

	public static void main(String[] args) {
		char[] sourceCode = {'#', '+', '#', '+', '#'};
		
		for(int i = 0;i<sourceCode.length;i++){
			progress(sourceCode[i]);
		}
	}

	private static void progress(char command){
		switch(command){
			case '>': head = (head+1)%9; 
				break;
			case '<': head = (head-1)%9;
				break;
			case '+': workingTape[head]=(char) (workingTape[head]+1);
				break;
			case '-': workingTape[head]=(char) (workingTape[head]-1);
				break;
			case '#': System.out.print(workingTape[head]);
				break;
			default: System.out.println("Befehl wurde nicht erkannt bitte Benutze < | > | + | - | #");
		}
	}
}


Loesung (Alternative)

/**
 * @author: Denis Lobo
 */
public class TuringMachine {
       //Arbeitsband der TuringMaschine
       private char[] workingTape = new char[10];
       private int workingTapeIndex = 0;
       //char[] sourceCode = {'#', '+', '#', '+', '#'};
       // Quellcode für Ausgabe von "hallo_welt"
       char[] sourceCode = {'+','+','+','+','+','+','+', '#', '>', '#', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '#', '+', '+', '+', '#','>','-','-','#','>','+', '+', '+', '+', '+','+', '+', '+', '+', '+','+', '+', '+', '+', '+','+', '+', '+', '+', '+','+', '+','#','>','+', '+', '+', '+','#','>', '+', '+', '+','+', '+', '+', '+', '+','+', '+','+','#','>', '+', '+', '+','+', '+','+', '+', '+','+', '+','+', '+', '+','+', '+', '+', '+', '+', '+','#'}; 
       
       //Konstruktor
       public TuringMachine() {
               initWorkingTape();
               work(sourceCode);
       }
       // initialisiert Arbeitsband mit 'a'
       private void initWorkingTape() {
               for (int i = 0; i < workingTape.length; i++) {
                       workingTape[i] = 'a';
               }
       }        
       //Rechtsshift des Kopfes
       private void shiftRight() {
               if (workingTapeIndex > 10)
                       workingTapeIndex = 0;
               else 
                       workingTapeIndex++;
       }
       //Linksshift des Kopfes
       private void shiftLeft() {
               if (workingTapeIndex <=0) 
                       workingTapeIndex = workingTape.length-1;
               else 
                       workingTapeIndex--;
       }	
       // Zeichen "inkrementieren"
       private void plus() {
               workingTape[workingTapeIndex]++;
       }
       // Zeichen "dekrementieren"
       private void minus() {
               workingTape[workingTapeIndex]--;
       }
       //Ausgabe des aktuellen Zeichens
       private void print() {
               System.out.println(workingTape[workingTapeIndex]);
       }
       
       // Algorithmus der TuringMaschine zur Abarbeitung des Codes
       private void work(char [] sourceCode) {
          	for (char c: sourceCode) {
                       switch (c) {
                               case '#': print();
                                         break;
                               case '<': shiftLeft();
                                         break;
                               case '>': shiftRight();
                                         break;
                               case '+': plus();
                                         break;
                               case '-': minus();
                                         break;
                               default: System.out.println("Code "+c+" ist nicht zulaessig!");
                       }
               }
       }
       //main-methode (Startpunkt des Programms)
       public static void main (String [] args) {
               new TuringMachine();
       }
}


Kommentare

Wenn du Anmerkungen zur Aufgabe hast oder Lob und Kritik loswerden möchtest, ist hier die richtige Stelle dafür. Klicke einfach ganz rechts auf "bearbeiten" und schreibe deinen Kommentar direkt ins Wiki. Keine Scheu, es geht nichts kaputt ;)