Notes
Notes - notes.io |
using namespace std;
bool expect(char c, string &input, size_t pos);
void match(char c, string &input, size_t &pos);
void parse_gesamtausdruck (string &input, size_t &pos);
void parse_ausdruck(string &input, size_t &pos);
void parse_term(string &input, size_t &pos);
void parse_operand(string &input, size_t &pos);
void parse_number(std::string &input, std::size_t &pos);
//fUNCTIONDECLARATION
//expect ist einfach eine "ob char entspricht der regel funktion".
bool expect(char c, string &input, size_t pos)
{
cout << "Teste auf das Zeichen " << c << endl;
if ( pos >= input.length() )
{
cout << "Aber schon am Ende der "
<< "Eingabe-Zeichenkette angelangt.n";
return false;
}
if ( input.at(pos) == c )
{
cout << "Zeichen " << c << " gefunden.n";
return true;
}
else {
cout << "Test auf " << c << " nicht erfolgreich. "
<< "Stattdessen " << input.at(pos)
<< " gesehen." << endl;
return false;
}
}
// match ist einfach eine pos++ funktion.
// wenn wir fertig mit checken mit expect sind, kommen wir ins match und pos++
void match(char c, string &input, size_t &pos)
{
cout << "Betrete match() fuer das Zeichen " << c << endl;
if ( pos >= input.length() )
{
cout << "Input-Zeichenkette zu kurz. "
<< "Erwarte noch das Zeichen " << c << endl;
cout << "Verlasse match(): " << endl;
return; }
if ( input.at(pos) != c )
{
cout << "Fehler: an Position "
<< pos << " erwarte "
<< c << " und sehe " << input.at(pos) << endl;
cout << "Verlasse match(): " << endl;
return; }
pos++;
cout << "Zeichen " << c << " konsumiert.n"
<< "Verlasse match() fuer das Zeichen "
<< c << endl;
}
void parse_gesamtausdruck (string &input, size_t &pos)
{
cout << "Betrete gesamtausdruck()" << endl;
parse_ausdruck(input, pos);
// nach alle rekursiver operationen sollen wir prüfen ob am ende ein '.' gibt.
if(expect('.', input, pos)){
match('.', input, pos);
return;
}
}
void parse_ausdruck(string &input, size_t &pos)
{
cout << "Betrete parse_ausdruck()" << endl;
parse_term(input, pos);
while ( expect('U', input, pos) || expect('O', input, pos) )
{
if ( expect('U', input, pos) )
{
cout << "Betrete parse_ausdruck(): 'U' Fall" << endl;
match('U', input, pos);
parse_term(input, pos);
// WIR HABEN SO EINE REIHE GESAMT->AUSDRUCK->TERM->OPERAND->NUMBER
cout << "Verlasse parse_ausdruck(): 'U' Fall" << endl;
}
else if ( expect('O', input, pos) )
{
cout << "Betrete parse_ausdruck(): 'O' Fall" << endl;
match('O', input, pos);
parse_term(input, pos);
cout << "Verlasse parse_ausdruck(): 'O' Fall" << endl;
}
}
cout << "Verlasse parse_ausdruck()" << endl;
}
void parse_term(string &input, size_t &pos)
{
cout << "Betrete parse_term()" << endl;
parse_operand(input, pos);
while ( expect('<', input, pos) || expect('>', input, pos) )
{
if ( expect('<', input, pos) )
{
cout << "Betrete parse_term(): '<' Fall" << endl;
match('<', input, pos);
parse_operand(input, pos);
cout << "Verlasse parse_term(): '<' Fall" << endl;
}
else if ( expect('>', input, pos) )
{
cout << "Betrete parse_term(): '>' Fall" << endl;
match('>', input, pos);
parse_operand(input, pos);
cout << "Verlasse parse_term(): '>' Fall" << endl;
}
}
cout << "Verlasse parse_term()" << endl;
}
void parse_operand(string &input, size_t &pos)
{
cout << "Betrete parse_operand()" << endl;
parse_number(input, pos);
while ( expect('(', input, pos) || expect(')', input, pos) )
{
if ( expect('(', input, pos) )
{
cout << "Betrete parse_term(): '(' Fall" << endl;
match('(', input, pos);
parse_number(input, pos);
cout << "Verlasse parse_term(): '(' Fall" << endl;
}
else if ( expect(')', input, pos) )
{
cout << "Betrete parse_operand(): ')' Fall" << endl;
match(')', input, pos);
parse_number(input, pos);
cout << "Verlasse parse_operand(): ')' Fall" << endl;
}
}
cout << "Verlasse parse_term()" << endl;
}
void parse_number(std::string &input, std::size_t &pos)
{
std::cout << "Betrete parse_number()" << std::endl;
for(int i = 0; i < 10; i++){
if(expect(i + '0', input, pos))
{
match(i + '0', input, pos);
break;
}
}
}
// wenn wir ein integer als char haben und char haben wollen i + '0'
int main() {
size_t pos = 0;
string input = "";
cout << "Bitte geben Sie die Zeichenkette ein: ";
getline(cin, input);
parse_gesamtausdruck(input, pos);
if ( pos != input.length() )
cout << "Error! Noch Input-Zeichen uebrig.n";
system("PAUSE");
return 0; }
// linie 40 warum checken wir nochmal in match ob char entspricht haben wir das schon nicht in expect gemacht?
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team