Chapter II defensive programming game of guessing words
/*** book: "thinkinginc++" * function: Realize a guessing game * time: October 8, 2014 21:54:44* Author: cutter_point*/#include <cstdlib> #include < Iostream> #include <string>using namespace Std;int main () {cout<< "Figure it out for yourself, between 1 and 100, let me guess your mind" < <endl << "I'm going to keep guessing, know what I guess, but you always tell me if it's high or low" <<endl; int low=1, high=100; BOOL Guessed=false; while (!guessed)//As long as not guessed, then guessed is false, then continue to execute {//If the user plays the yin if (Low > High) {cout& lt;< "Fuck you! Can not play a good game, not pure in this number "<<endl; return exit_failure; } int guess= (Low+high)/2; Get the median value cout<< "I guess this number is:" <<guess<< ". "; cout<< "Tell me if it's higher (high) or low, or am I guessing?" (Equal) "<<endl; string response; cin>>response; Switch (ToUpper (response[0)) {case ' H ': high=guess-1; Break Case ' L ': low=guess+1; Break Case ' E ': guessed=true; Break default:cout<< "Pro, you entered the wrong data! "<<endl; Continue }} cout<< "YES I got it!!!" <<endl; return exit_success; Returns a macro inside a cstdlib that indicates that the execution of the program failed}
Automatic Date1.h
/*** book: "thinkinginc++" * Function: Construction of Time class * Time: October 8, 2014 21:55:09* Author: cutter_point*/#ifndef date1_h_included#define Date1_h_ Included#include <string>class date{public:struct Duration//A handle structure representing the time {int years; int months; int days; constructor Duration (int y, int m, int D): Years (y), months (m), days (d) {}}*dt; Date (); Date (int year, Int. month, int day) {Dt=new Duration (year, month, day);} Date (const std::string&); int GetYear () const {return dt->years;} int GetMonth () const {return dt->months;} int GetDay () const {return dt->days;} std::string toString () const; friend bool operator< (const date&, const date&); friend bool Operator> (const date&, const date&); friend bool operator<= (const date&, const date&); friend bool operator>= (const date&, const date&); friend bool operator== (const date&, const date&); friend bool Operator!= (const DATE&, const date&); Friend Duration Duration (const date&, const date&);}; #endif//date1_h_included
Test procedure 1SimpleDateTest.cpp
/*** book: "thinkinginc++" * Function: Test time class * Time: October 8, 2014 21:55:33* Author: cutter_point*/#include <iostream> #include " Date1.h "using namespace Std;int npass=0, nfail=0;void test (bool t) {if (t) npass++; else nfail++;} The function used to test int main () { Date mybday (1951, 1); Test (mybday.getyear () = = 1951); Test (mybday.getmonth () = = ten); Test (mybday.getday () = = 1); cout<< "Passed:" <<nPass<< ", Failed:" <<nFail<<endl; return 0;}
"Thinkinginc++" 70, defensive programming