In this exercise I'm using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.
Chapter 26 // Exercise 6
Modify the calculator from Chapter 7 minimally to let it take input from a file and produce output to a file (or use your operating systems facilities for redirecting I/O). Then devise a reasonably comprehensive test for it.
Chapter 7 is back; it's the gift that keeps on giving.
I already kind of implemented this back in Chapter 16 when we had to give this code some graphics using FLTK in C16 Exercise 9. I basically took the calculator code; did nothing to it except "fake" a cin buffer by creating an istringstream and passing the binary data directly to the cin buffer:
string calculate(string input)
{
//put the input "into" cin
istringstream oss(input);
cin.rdbuf(oss.rdbuf());
//now do normal calculator stuff as we have "characters in the buffer"
string answer = to_string(statement());
//clean up token stream for next statement
ts.ignore();
return answer;
}
Using this code (Exercise 9) I made it so it reads from from a text file that takes input per line, then compares it to the expected output from the next line and compares the two. I decided to just read everything in from one file instead of outputting to another file.
Amusingly, I used ChatGPT-4 to generate the test list and as I ran the tests it gave an incorrect answer for sqrt(pow(5,3)). it said it was 8.66 instead of 11.18. I've decided to leave that in so you can see a failure.
No comments:
Post a Comment