Skip to content

Commit

Permalink
finished 17.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 30, 2017
1 parent cd2153e commit 6d38263
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 170 deletions.
26 changes: 25 additions & 1 deletion ch17/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,28 @@ Because `std::accumulate`'s third parameter is the initial value of the sum. It'
(a): 64 bits, lower-order 6 bits are `100000`, remaining bits are 0.
(b): 00000000000011110110100110110101
(c): depends on what user has been input.
(c): depends on what user has been input.
## Exercise 17.10
> Using the sequence 1, 2, 3, 5, 8, 13, 21, initialize a `bitset` that has a 1 bit in each position corresponding to a number in this sequence. Default initialize another `bitset` and write a small program to turn on each of the appropriate bits.
[`bitset` initialization and turn on test](ex17_10.cpp)
## 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?
[`QuizResponses` Definition](ex17_11_quiz_responses.h)
## 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 updates the quiz results accordingly.
[`QuizResponses` Definition](ex17_11_quiz_responses.h)
## Exercise 17.13
> Write an integral object that contains the correct answers for the true/false quiz. Use it to generate grades on the quiz for the data structure from the previous two exercises.
[Test `QuizResponses` class](ex17_11_quiz_responses_test.cpp)
35 changes: 0 additions & 35 deletions ch17/ex17.10/main.cpp

This file was deleted.

97 changes: 0 additions & 97 deletions ch17/ex17.11.12.13/main.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions ch17/ex17.9/main.cpp

This file was deleted.

4 changes: 3 additions & 1 deletion ch17/ex17_03_text_query.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <fstream>
#include <iostream>
#include <map>
Expand Down Expand Up @@ -34,4 +36,4 @@ class TextQuery {
};

ostream& print(ostream&, const query_result&);
}
} // namespace EX03
4 changes: 3 additions & 1 deletion ch17/ex17_04_findBook.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <numeric>
Expand Down Expand Up @@ -56,4 +58,4 @@ void reportResults(std::istream& in, std::ostream& os,
std::cout << "----" << std::endl;
}
}
}
} // namespace EX04
4 changes: 3 additions & 1 deletion ch17/ex17_05_findBook.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <numeric>
Expand Down Expand Up @@ -57,4 +59,4 @@ void reportResults(std::istream& in, std::ostream& os,
std::cout << "----" << std::endl;
}
}
}
} // namespace EX05
4 changes: 3 additions & 1 deletion ch17/ex17_06_findBook.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <algorithm>
#include <iostream>
#include <numeric>
Expand Down Expand Up @@ -67,4 +69,4 @@ void reportResults(std::istream& in, std::ostream& os,
std::cout << "----" << std::endl;
}
}
}
} // namespace EX06
18 changes: 18 additions & 0 deletions ch17/ex17_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <bitset>
#include <cassert>

int main()
{
// init from the sequence: 1, 2, 3, 5, 8, 13, 21
std::bitset<22> bitseq("1000000010000100101110");
std::cout << bitseq << std::endl;

// Default initialize, then turn on.
std::bitset<22> bit_default;
for (auto i : {1, 2, 3, 5, 8, 13, 21})
bit_default.set(i);
std::cout << bit_default << std::endl;

assert(bitseq == bit_default);
}
26 changes: 26 additions & 0 deletions ch17/ex17_11_quiz_responses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <bitset>
#include <string>

namespace EX11 {
using size = std::size_t;
template <size N> class QuizResponses {
public:
QuizResponses() = default;
QuizResponses(const std::string& s) : answers(s) {}

// added a function that takes a question number and a value to indicate
// a true/false answer and updates the quiz results accordingly
void answer(size n, bool v) { answers.set(n - 1, v); }

// generate grades on the quiz
size score(const QuizResponses& correct)
{
return (this->answers ^ correct.answers).flip().count() * 1.0 / N * 100;
}

private:
std::bitset<N> answers;
};
} // namespace EX11
15 changes: 15 additions & 0 deletions ch17/ex17_11_quiz_responses_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "ex17_11_quiz_responses.h"
#include <iostream>

int main()
{
EX11::QuizResponses<10> simple_quiz_answers("1100110101");

EX11::QuizResponses<100> complicated_quiz_answers;
complicated_quiz_answers.answer(1, true);
complicated_quiz_answers.answer(2, false);
complicated_quiz_answers.answer(3, true);

EX11::QuizResponses<10> simple_quiz_correct("1010101010");
std::cout << simple_quiz_answers.score(simple_quiz_correct) << std::endl;
}

0 comments on commit 6d38263

Please sign in to comment.