Hackerboard Wiki HaboBlog
Hackerboard bei Facebook Hackerboard bei Google+ Hackerboard bei Twitter

[HaBo]

 
Programmieraufgaben Hier wird regelmäßig eine neue Programmieraufgabe gestellt, die dann gelöst werden soll und in Zusammenarbeit mit den Moderatoren auch besprochen werden kann.

Ook?Ook!

Diskussion: Ook?Ook! im Forum Programmieraufgaben, in der Kategorie Code Kitchen; Anzeige Eingereicht von Ook! : Zitat: Schreibe einen Ook!-Parser! Wikipedia dürfte die nötigen Erklärungen geben. Ook!-Code kann in Brainfuck-Code geparsed ...

Antwort
Alt 22.05.08, 22:32   #1 (permalink)
CDW
Moderator
 
Benutzerbild von CDW
 
Registriert seit: 20.07.05
CDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: Opteron
Likes: 202
Standard Ook?Ook!

Anzeige

Eingereicht von Ook! :

Zitat:
Schreibe einen Ook!-Parser!
Wikipedia dürfte die nötigen Erklärungen geben. Ook!-Code kann in Brainfuck-Code geparsed und dann in Plaintext. Ein weiteres Beispiel zum Thema "Teile und Herrsche" ...
Wer mag, kann natürlich direkt in Plaintext parsen, keine Frage!
http://de.wikipedia.org/wiki/Ook!
__________________
Noch mal, für alle Pseudo-Geeks: 1+1=0. -> 10 wäre Überlauf!
Selig, wer nichts zu sagen hat und trotzdem schweigt.
CDW ist offline   Mit Zitat antworten
Alt 23.05.08, 01:45   #2 (permalink)
 
Registriert seit: 27.04.07
Liar Leistung: Facit NTK
Liar eine Nachricht über ICQ schicken
Likes: 1
Standard

hi

edit: fehler ausgebessert, jetzt geht alles

C/C++   

Code:
/*
* Ook/Brainfuck Interpreter
* Usage:
* ookbf.exe -ook <ookfile> // Ook Interpreter(Ook to BF und dann interpretieren)
* ookbf.exe -bf <bffile> // BF Interpreter
* ookbf.exe -ooktobf <ookfile> <bffile/outputfile> // Ook to BF Converter
*/

#include <iostream>
#include <fstream>

extern "C" int getch();
extern "C" int putch(int);

using namespace std;

string loadfile(string FileName)
{
string filecontent="";
ifstream in;
in.open(FileName.c_str(),ios_base::in);
if(in.is_open())
{
do
{
filecontent+=in.get();
}
while(!in.eof());
}
in.close();
return filecontent;
}

string ooktobf(string ook)
{
string bf="";
string pre="";
string current="";
int i=0;
while(ook.length()>0)
{
if(ook.find("Ook?",0)==0)
{
ook.erase(0,((string)"Ook?").length());
pre=current;
current="Ook?";
i++;
}
else if(ook.find("Ook!",0)==0)
{
ook.erase(0,((string)"Ook!").length());
pre=current;
current="Ook!";
i++;
}
else if(ook.find("Ook.",0)==0)
{
ook.erase(0,((string)"Ook.").length());
pre=current;
current="Ook.";
i++;
}
else
{
ook.erase(0,1);
}
if(i==2)
{
i=0;
if((pre=="Ook?")&&(current=="Ook?"))
{
cout << "Ook to Brainfuck converter: Error: Ook? Ook? is not a valid command" << endl;
}
else if((pre=="Ook.")&&(current=="Ook."))
{
bf+='+';
}
else if((pre=="Ook!")&&(current=="Ook!"))
{
bf+='-';
}
else if((pre=="Ook.")&&(current=="Ook?"))
{
bf+='>';
}
else if((pre=="Ook?")&&(current=="Ook."))
{
bf+='<';
}
else if((pre=="Ook!")&&(current=="Ook?"))
{
bf+='[';
}
else if((pre=="Ook?")&&(current=="Ook!"))
{
bf+=']';
}
else if((pre=="Ook!")&&(current=="Ook."))
{
bf+='.';
}
else if((pre=="Ook.")&&(current=="Ook!"))
{
bf+=',';
}
}
}
return bf;
}

int countchar(string str,char c)
{
int i=0;
for(int i=0;i<str.length();i++)
{
if(str[i]==c)
{
i++;
}
}
return i;
}

void bf(string commands)
{
char c=0;
long long int *p=new long long int[65536];
int count=0;
int line=1, column=1;
if(countchar(commands,'[')!=countchar(commands,']'))
{
cout << "Brainfuck Interpreter: Error: Every [/] requires a matching ]/[" << endl;
return;
}
for(int i=0;i<65536;i++)
{
p[i]=0;
}
for(int i=0;i<commands.length();i++)
{
count++;
c=commands[i];
column++;
if(c=='.')
{
putch(*p);
}
else if(c==',')
{
(*p)=getch();
}
else if(c=='+')
{
(*p)++;
}
else if(c=='-')
{
(*p)--;
}
else if(c=='>')
{
p++;
}
else if(c=='<')
{
p--;
}
else if(c=='[')
{
if((*p)==0)
{
bool found=false;
int j=0;
for(int k=i+1;k<commands.length();k++)
{
if((commands[k]==']')&&(j==0))
{
i=k;
found=true;
break;
}
else if(commands[k]=='[')
{
j++;
}
else if(commands[k]==']')
{
j--;
}
}
if(!found)
{
cout << "Brainfuck Interpreter: Error: Matching ] not found. Line: " << line << " Column: " << column << " Character Number: " << i << endl;
return;
}
}
}
else if(c==']')
{
if((*p)!=0)
{
bool found=false;
int j=0;
for(int k=i-1;k>=0;k--)
{
if((commands[k]=='[')&&(j==0))
{
i=k;
found=true;
break;
}
else if(commands[k]==']')
{
j++;
}
else if(commands[k]=='[')
{
j--;
}
}
if(!found)
{
cout << "Brainfuck Interpreter: Error: Matching [ not found. Line: " << line << " Column: " << column << " Character Number: " << i << endl;
return;
}
}
}
else if(c=='n')
{
line++;
column=1;
}
}
}

int main(int argc, char *argv[])
{
bool usage=true;
for(int i=1;i<argc;i++)
{
if((string)argv[i]=="-bf")
{
bf(loadfile(argv[i+1]));
usage=false;
i++;
}
else if((string)argv[i]=="-ook")
{
bf(ooktobf(loadfile(argv[i+1])));
usage=false;
i++;
}
else if((string)argv[i]=="-ooktobf")
{
ofstream out;
out.open(argv[i+2],ios_base::out);
if(out.is_open())
{
out << ooktobf(loadfile(argv[i+1]));
}
out.close();
usage=false;
i+=2;
}
}
if(usage)
{
cout << "Usage:" << endl;
cout << argv[0] << " -ook <ookfile>" << endl;
cout << argv[0] << " -bf <bffile>" << endl;
cout << argv[0] << " -ooktobf <ookfile> <bffile/outputfile>" << endl;
}
return 0;
}


MfG Liar
Liar ist offline   Mit Zitat antworten
Antwort
   
- Anzeige -

Werbung ist gerade online    

[HaBo] » Software Home » Code Kitchen » Programmieraufgaben » Ook?Ook!
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61