http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
Chapter 7 // Exercise 7
7. Change the q and h commands to be quit and help respectively.
I'm not quite sure what he means by this one. Does he want us to change what to check for i.e have the user type 'quit' and 'help' to quit and display help? Drill 10 told us to change the quit keyword to exit and define a string for quit. I've been using 'quit' to exit the program since just because I prefer it. If so, that simply involves defining another const string called helpKey and assigning "help". Then in ts.get() checking to see if help was typed in and returning Token(help) if so.
Chapter 7 // Exercise 8
Calculation:
Statement
Print
Help
Quit
Calculation Statement
Statement:
Declaration
Expression
Declaration:
"let" Name "=" Expression
"let "const" Name "=" Expression
Expression:
Term
Expression + Term
Expression - Term
Term:
Primary
Term * Primary
Term / Primary
Term % Primary
Primary:
Number
"(" Expression ")"
"-" Primary
"+" Primary
Expression "=" Name
"sqrt(" Expression ")"
"pow(" Expression "," Expression ")"
Number:
Floating-point literal
Chapter 7 // Exercise 9
Over the course of the exercises I've already implemented three extra things. The first was to allow users to re-assign a keyword if they wanted to so long as they hadn't set it to const. The second was to create a reusable function that could check for a given character. And in the third I changed the declare() function to be a part of the Symbols class.
Chapter 7 // Exercise 10
Chapter 7 // Exercise 8
8. The grammar in section 7.6.4 is incomplete; it does not define sequences of statements, such as 4+4; 5-6; and it does not incorporate the grammar changes outlined in section 7.8. Fix the grammar.
I really don't like these exercises. This 'grammar' stuff confuses the hell out of me. But I'll give it a go:
Calculation:
Statement
Help
Quit
Calculation Statement
Statement:
Declaration
Expression
Declaration:
"let" Name "=" Expression
"let "const" Name "=" Expression
Expression:
Term
Expression + Term
Expression - Term
Term:
Primary
Term * Primary
Term / Primary
Term % Primary
Primary:
Number
"(" Expression ")"
"-" Primary
"+" Primary
Expression "=" Name
"sqrt(" Expression ")"
"pow(" Expression "," Expression ")"
Number:
Floating-point literal
Chapter 7 // Exercise 9
9. Suggest three improvements (not mentioned in this chapter) to the calculator. Implement one of them.
Over the course of the exercises I've already implemented three extra things. The first was to allow users to re-assign a keyword if they wanted to so long as they hadn't set it to const. The second was to create a reusable function that could check for a given character. And in the third I changed the declare() function to be a part of the Symbols class.
Chapter 7 // Exercise 10
10. Modify the calculator to operate on ints (only): give errors for overflow and underflow. Hint: use narrow_cast (section 7.5).
I had a look around the internet and the general consensus is that you cannot specifically check for overflow or underflow, then again I often misinterpret what is being asked for. I did however find a useful answer here that explained how narrow_cast is defined by Bjarne himself in his more advanced book. It would appear that narrow_cast gives an error itself when data is lost. Knowing this, I then changed all the double values to ints, making sure that where appropriate they were always assigned using narrow_cast to give the error warning. That said, Visual Studio will tell you where you need to use it as I found a couple of times as it won't compile when there is a possible loss of data.
Chapter 7 // Exercise 11
Chapter 7 // Exercise 11
11. Revisit two programs you wrote for the exercises in Chapter 4 or 5. Clean up up that code according to the rules outlined in this chapter. See if you find any bugs in the process.
No comments:
Post a Comment