Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) OpenQuestion

 

OpenQuestion is a class for an open question.

 

 

 

 

 

openquestion.h

 

//---------------------------------------------------------------------------
/*
OpenQuestion, class for an open question
Copyright (C) 2011  Richl Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl/CppOpenQuestion
//---------------------------------------------------------------------------
#ifndef OPENQUESTION_H
#define OPENQUESTION_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include "question.h"
//---------------------------------------------------------------------------
///class for an open question
struct OpenQuestion : public Question
{
  ///Throws nothing or std::out_of_range or std::runtime_error
  OpenQuestion(const std::string& question);

  ///An open question has multiple possible answers
  OpenQuestion(
    const std::string& filename,
    const std::string& question,
    const std::vector<std::string>& answers);

  ///Create a copy of the Question, depending on the derived class its type
  Question * Clone() const;

  const std::vector<std::string>& GetAnswers() const;

  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();

  private:
  friend void boost::checked_delete<>(OpenQuestion *);

  ///The wrong answers are at indices 2 to SeperateString(input,',').size()
  static const std::vector<std::string> ExtractAnswers(
    const std::string& input);

  //Split a string
  //From http://www.richelbilderbeek.nl/CppSeperateString.htm
  static const std::vector<std::string> SeperateString(
    const std::string& input,
    const char seperator);

};
//---------------------------------------------------------------------------
#endif // OPENQUESTION_H

 

 

 

 

 

openquestion.cpp

 

//---------------------------------------------------------------------------
/*
OpenQuestion, class for an open question
Copyright (C) 2011  Richl Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl/CppOpenQuestion
//---------------------------------------------------------------------------
#include <cassert>
#include <string>
#include <fstream>
//---------------------------------------------------------------------------
#include <boost/algorithm/string/split.hpp>
//---------------------------------------------------------------------------
#include "openquestion.h"
//---------------------------------------------------------------------------
OpenQuestion::OpenQuestion(const std::string& question)
  : Question(
      SeperateString(question,',').at(0),
      SeperateString(question,',').at(1),
      ExtractAnswers(question))
{

}
//---------------------------------------------------------------------------
OpenQuestion::OpenQuestion(
  const std::string& filename,
  const std::string& question,
  const std::vector<std::string>& answers)
: Question(filename,question, answers )
{
  //assert(!filename.empty() && "Filename must not be empty");
  //assert(FileExists(filename) == true && "File must exists");
  //assert(!question.empty() && "OpenQuestion must not be empty");
  //assert(!correct_answers.empty() && "Correct answer must not be empty");
}
//---------------------------------------------------------------------------
///Create a copy of the Question, depending on the derived class its type
Question * OpenQuestion::Clone() const
{
  return new OpenQuestion(
    this->GetFilename(),
    this->GetQuestion(),
    this->GetAnswers());
}
//---------------------------------------------------------------------------
const std::vector<std::string> OpenQuestion::ExtractAnswers(const std::string& input)
{
  const std::vector<std::string> v = SeperateString(input,',');
  if (v.size() != 3)
  {
    throw std::logic_error("An open question has exactly three comma-seperated elements");
  }

  const std::vector<std::string> w = SeperateString(v[2],'/');
  if (w.size() == 0)
  {
    throw std::logic_error("An open question has at least one correct answer");
  }
  return w;
}
//---------------------------------------------------------------------------
const std::vector<std::string>& OpenQuestion::GetAnswers() const
{
  return this->Question::GetAnswers();
}
//---------------------------------------------------------------------------
const std::string OpenQuestion::GetVersion()
{
  return "1.1";
}
//---------------------------------------------------------------------------
const std::vector<std::string> OpenQuestion::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-06-27: version 1.0: initial version");
  v.push_back("2011-09-16: version 1.1: allow parsing from std::string");
  return v;
}
//---------------------------------------------------------------------------
const std::vector<std::string> OpenQuestion::SeperateString(
  const std::string& input,
  const char seperator)
{
  std::vector<std::string> v;
  boost::algorithm::split(v,input,
    std::bind2nd(std::equal_to<char>(),seperator),
    boost::algorithm::token_compress_on);
  return v;
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml