In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 6 // Exercise 5
Add the article the to the "English" grammar in 6.4.1, so that it can describe sentences such as "The birds fly but the fish swim."
I'm not sure quite what he wants us to do in this one. Make a program or just change the psuedocode? Either way the word 'the' is extremely complicated and has many different uses. It's doesn't always come before or after a verb, same with a noun and it's not a conjunction (something that joins sentences together).
If he just wants us to change the psuedocode so it always comes before a noun (like he's put in his example) then it would be like this:
Sentence:
The Noun Verb
Sentence Conjuction Sentence
Conjuction:
and
or
but
Noun:
birds
fish
C++
The:
the
Verb:
rules
fly
swim
I think this would also give you "The c++ rules", which is bad grammar because c++ doesn't denote something plural or a direct object (as opposed to saying 'the rules' where rules denotes the plural of rule) But I don't think we're creating Microsoft Word just yet, so I'm going to leave this one where it is.
Chapter 6 // Exercise 6
Write a program that checks if a sentence is correct according to the "English" grammar in 6.4.1. Assume that every sentence is terminated by a full stop (.) surrounded by white space. For example, birds fly but the fish swim . is a sentence, but birds fly but the fish swim (terminating dot missing) and birds fly but the fish swim. (no space before dot) are not. For each sentence entered, the program should simply respond "OK" or "not OK". Hint: Don't bother with tokens; just read in a string using >>.
I honestly didn't know what to make of this so here is his answer:
Chapter 6 exercise 6
Reading through it, it uses bools and I just can't wrap my head around them. It's just like minecraft with the not gates, I understand it flips it but I just don't get the whole 'true' 'false' thing.
Overall these two exercises were just
to me.
EDIT 07/10/2019 - I have now completed these two exercises found on GitHub here. This took me a while as I started getting pedantic about how to parse the sentence. I eventually went with a very hacky way of doing things as I was starting to run out of time. Eventually, I'll go back and refactor the code again to make it a little more …elegant shall we say. I started doing it how he has done it without realising it, but it doesn't allow for all the sentence types I don't think. I do like the idea of parsing the sentence into a wordtype vector as you can create verb/noun vectors and add more words to them and when deciding what they are, use a for loop to loop through each vector. Then creating a function which checks sentence structure shouldn't be too much hassle. I say shouldn't...
"Overall these two exercises were just WTF to me."
ReplyDeleteLOL... same here!
Thanks so much for uploading your work here! It's invaluable for those who self-study, such as myself :)
ReplyDeleteB.B
No worries! And thank you, I'm glad you find it useful! :D
Delete