diff --git a/ch17/ex17.11.12.13/main.cpp b/ch17/ex17.11.12.13/main.cpp index 4255a60c..4e09cfad 100644 --- a/ch17/ex17.11.12.13/main.cpp +++ b/ch17/ex17.11.12.13/main.cpp @@ -1,19 +1,16 @@ /*************************************************************************** - * @file main.cpp - * @author gau fung - * @date 25 May 2015 - * @remark This code is for the exercises from C++ Primer 5th Edition - * @note - ***************************************************************************/ +* @file main.cpp +* @author gau fung +* @date 25 May 2015 +* @remark This code is for the exercises from C++ Primer 5th Edition +* @note +***************************************************************************/ //! //! Exercise 17.11: //! Define a data structure that contains an integral object to track responses //! to a true/false quiz containing 10 questions. What changes, if any, would //! you need to make in your data structure if the quiz had 100 questions? //! -// std::bitset<10> reponses; -// std::bitset<100> reponses2; -//! //! Exercise 17.12: //! Using the data structure from the previous question, write a function that //! takes a question number and a value to indicate a true/false answer and @@ -32,54 +29,69 @@ #include #include #include -//class type: quiz (for Data Stuture) + +//class Quiz template -class Quiz{ +class Quiz +{ public: - //constructor - Quiz()=default; - Quiz(std::string& s):bitquiz(s){} - //generate the grade for this quiz - template - friend std::size_t grade(Quiz&,Quiz&); - //output the bitset - template - friend std::ostream& operator<<(std::ostream&,Quiz&); + //constructors + Quiz() = default; + Quiz(std::string& s) :bitquiz(s){} + + //generate grade + template + friend std::size_t grade(Quiz const&, Quiz const&); - //update bitset - void update(std::pair); + //print + template + friend std::ostream& operator<<(std::ostream&, Quiz const&); + + //update bitset + void update(std::pair); private: - //the bitset - std::bitset bitquiz; + std::bitset bitquiz; }; #endif + template -void Quiz::update(std::pair pair){ - bitquiz.set(pair.first,pair.second);//update the value +void Quiz::update(std::pair pair) +{ + bitquiz.set(pair.first, pair.second); } + template -std::ostream& operator<<(std::ostream& os,Quiz& quiz){ - os< const& quiz) +{ + os << quiz.bitquiz; + return os; } + template -std::size_t grade(Quiz& corAns,Quiz& stuAns){ - auto result = stuAns.bitquiz ^ corAns.bitquiz; +std::size_t grade(Quiz const& corAns, Quiz const& stuAns) +{ + auto result = stuAns.bitquiz ^ corAns.bitquiz; result.flip(); return result.count(); } -int main(){ - //Ex17_11 - std::string s="1010101"; - Quiz<10> quiz(s); - std::cout< ans(answer),stu_ans(stu_answer); - std::cout< quiz(s); + std::cout << quiz << std::endl; + + //EX17_12 + quiz.update(std::make_pair(1, true)); + std::cout << quiz << std::endl; + + //Ex17_13 + std::string answer = "10011"; + std::string stu_answer = "11001"; + Quiz<5> ans(answer), stu_ans(stu_answer); + std::cout << grade(ans, stu_ans) << std::endl; + + return 0; }