Skip to content

Commit

Permalink
Update ex17_11_12_13.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy authored and pezy committed May 27, 2015
1 parent 633242e commit baff244
Showing 1 changed file with 57 additions and 45 deletions.
102 changes: 57 additions & 45 deletions ch17/ex17.11.12.13/main.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -32,54 +29,69 @@
#include <utility>
#include <string>
#include <iostream>
//class type: quiz (for Data Stuture)

//class Quiz
template<std::size_t N>
class Quiz{
class Quiz
{
public:
//constructor
Quiz()=default;
Quiz(std::string& s):bitquiz(s){}
//generate the grade for this quiz
template<std::size_t M>
friend std::size_t grade(Quiz<M>&,Quiz<M>&);
//output the bitset
template<std::size_t M>
friend std::ostream& operator<<(std::ostream&,Quiz<M>&);
//constructors
Quiz() = default;
Quiz(std::string& s) :bitquiz(s){}

//generate grade
template<std::size_t M>
friend std::size_t grade(Quiz<M> const&, Quiz<M> const&);

//update bitset
void update(std::pair<std::size_t,bool>);
//print
template<std::size_t M>
friend std::ostream& operator<<(std::ostream&, Quiz<M> const&);

//update bitset
void update(std::pair<std::size_t, bool>);
private:
//the bitset
std::bitset<N> bitquiz;
std::bitset<N> bitquiz;
};
#endif

template<std::size_t N>
void Quiz<N>::update(std::pair<std::size_t,bool> pair){
bitquiz.set(pair.first,pair.second);//update the value
void Quiz<N>::update(std::pair<std::size_t, bool> pair)
{
bitquiz.set(pair.first, pair.second);
}

template<std::size_t M>
std::ostream& operator<<(std::ostream& os,Quiz<M>& quiz){
os<<quiz.bitquiz;
return os;
std::ostream& operator<<(std::ostream& os, Quiz<M> const& quiz)
{
os << quiz.bitquiz;
return os;
}

template<std::size_t M>
std::size_t grade(Quiz<M>& corAns,Quiz<M>& stuAns){
auto result = stuAns.bitquiz ^ corAns.bitquiz;
std::size_t grade(Quiz<M> const& corAns, Quiz<M> 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<<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);
return 0;


int main()
{
//Ex17_11
std::string s = "1010101";
Quiz<10> 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;
}

0 comments on commit baff244

Please sign in to comment.