Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- #include<cctype>
- using namespace std;
- class Question{
- protected:
- char id[5];
- char * text;
- int points;
- const void copy_obj(const Question ©){
- strcpy(this->id, copy.id);
- this->text = new char[strlen(copy.text)+1];
- strcpy(this->text, copy.text);
- this->points = copy.points;
- }
- public:
- Question(){
- strcpy(this->id, "00000");
- this->text = new char[0];
- this->points = 0;
- }
- Question(const char * id, const char * text, const int points){
- strcpy(this->id, id);
- this->text = new char[strlen(text)+1];
- strcpy(this->text, text);
- this->points = points;
- }
- Question(const Question ©){
- this->copy_obj(copy);
- }
- Question &operator=(const Question ©){
- if(this != ©){
- delete [] this->text;
- this->copy_obj(copy);
- }
- return *this;
- }
- ~Question(){
- delete [] this->text;
- }
- virtual double checkAnswer(char * answer) = 0;
- bool operator>(Question &orig){
- if(this->points > orig.points)
- return true;
- else
- return false;
- }
- const int getPoeni(){
- return this->points;
- }
- friend ostream &operator << (ostream &os, Question &orig){
- os << "Question " << orig.id << ": " << orig.text << "\n";
- TrueFalseQuestion * tf = dynamic_cast<TrueFalseQuestion *>(&orig);
- os << "Correct answer: "
- if(tf){
- if(tf.answer == true)
- os << "true\n";
- else
- os << "false\n";
- }else
- os << orig.answer << "\n";
- }
- };
- class MultichoiceQuestion : public Question{
- private:
- char answer;
- public:
- MultichoiceQuestion (const char * id, const char * text, const int points, const char answer) : Question(id, text, points){
- this->answer = answer;
- }
- double checkAnswer(char * answer){
- if(this->answer == answer[0])
- return this->points;
- else
- return this->points - (this->points * 25 / 100);
- }
- };
- class TrueFalseQuestion : public Question{
- private:
- bool answer;
- public:
- TrueFalseQuestion(const char * id, const char * text, const int points, const bool answer): Question(id, text, points){
- this->answer = answer;
- }
- double checkAnswer(char * answer){
- if(answer[0] == 't'){
- if(this->answer == true)
- return this->points;
- else return 0;
- }
- }
- };
- class ShortTextAnswerQuestion : public Question{
- private:
- char answer[50];
- bool caseSens;
- public:
- ShortTextAnswerQuestion(const char * id, const char * text, const int points, const char * answer, const bool caseSens) : Question(id, text, points){
- strcpy(this->answer, answer);
- this->caseSens = caseSens;
- }
- double checkAnswer(char * answer){
- if(this->caseSens){
- if(!strcmp(this->answer, answer))
- return this->points;
- else
- return 0;
- } else {
- for (int i=0; i<strlen(answer); i++)
- this->answer[i] == tolower(this->answer[i]);
- if(!strcmp(this->answer, answer))
- return this->points;
- else
- return 0;
- }
- }
- };
- double guessAllMultichoiceQuestions (Question ** questions, int n, char * choice){
- double sum=0.0;
- for(int i=0; i<n; i++){
- MultichoiceQuestion * m = dynamic_cast<MultichoiceQuestion *>(questions[i]);
- if(m)
- sum += questions[i]->checkAnswer(choice);
- }
- return sum;
- }
- Question * questionWithMaxPoints (Question ** questions, int n) {
- int max = questions[0]->getPoeni();
- int index = 0;
- for(int i=0; i<n; i++){
- if(questions[i]->getPoeni() > max){
- max = questions[i]->getPoeni();
- index = i;
- }
- }
- return questions[index];
- }
- int main () {
- int testCase;
- cin>>testCase;
- char text [200];
- char ID [5];
- char correctAnswer [50];
- int points;
- int caseSensitive;
- char correctLetter;
- int trueOrFalse;
- if (testCase==1) {
- cout<<"Testing MultichoiceQuestion class, operator << and checkAnswer"<<endl;
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctLetter;
- MultichoiceQuestion mq (ID, text, points, correctLetter);
- cout<<mq<<endl;
- for (char ans = 'a'; ans <= 'd'; ans++) {
- char answer [2];
- answer[0] = ans;
- cout<<mq.checkAnswer(answer)<<endl;
- }
- }
- else if (testCase == 2) {
- cout<<"Testing TrueFalseQuestion class, operator << and checkAnswer"<<endl;
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>trueOrFalse;
- TrueFalseQuestion tfq (ID, text, points, (trueOrFalse==1));
- cout<<tfq<<endl;
- char trueAnswer [2]; trueAnswer[0]='t';
- char falseAnswer [2]; falseAnswer[0]='f';
- cout<<tfq.checkAnswer(trueAnswer)<<endl;
- cout<<tfq.checkAnswer(falseAnswer)<<endl;
- } else if (testCase == 3) {
- cout<<"Testing ShortTextAnswerQuestion class (case sensitive), operator << and checkAnswer"<<endl;
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctAnswer;
- ShortTextAnswerQuestion staq (ID, text, points, correctAnswer, true);
- cout<<staq<<endl;
- cout<<staq.checkAnswer(correctAnswer)<<endl;
- for (int i=0;i<strlen(correctAnswer);i++) {
- if (isupper(correctAnswer[i]))
- correctAnswer[i]=tolower(correctAnswer[i]);
- else
- correctAnswer[i]=toupper(correctAnswer[i]);
- }
- //cout<<correctAnswer<<endl;
- cout<<staq.checkAnswer(correctAnswer)<<endl;
- } else if (testCase == 4) {
- cout<<"Testing ShortTextAnswerQuestion class (case insensitive, operator << and checkAnswer"<<endl;
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctAnswer;
- ShortTextAnswerQuestion staq (ID, text, points, correctAnswer, false);
- cout<<staq<<endl;
- cout<<staq.checkAnswer(correctAnswer)<<endl;
- for (int i=0;i<strlen(correctAnswer);i++) {
- if (isupper(correctAnswer[i]))
- correctAnswer[i]=tolower(correctAnswer[i]);
- else
- correctAnswer[i]=toupper(correctAnswer[i]);
- }
- //cout<<correctAnswer<<endl;
- cout<<staq.checkAnswer(correctAnswer)<<endl;
- } else if (testCase == 5) {
- cout<<"Test questAllMultichoiceQuestions"<<endl;
- int n;
- cin>>n;
- Question ** questions = new Question * [n];
- for (int j=0;j<n;j++) {
- int type;
- cin>>type; //1=MCQ, 2=TFQ, 3=STAQ
- if (type==1) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctLetter;
- questions[j] = new MultichoiceQuestion (ID, text, points, correctLetter);
- }
- else if (type==2) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>trueOrFalse;
- questions[j] = new TrueFalseQuestion (ID, text, points, (trueOrFalse==1));
- } else if (type==3) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctAnswer;
- questions[j] = new ShortTextAnswerQuestion (ID, text, points, correctAnswer, true);
- }
- }
- cout<<"a: "<<guessAllMultichoiceQuestions(questions, n, 'a')<<endl;
- cout<<"b: "<<guessAllMultichoiceQuestions(questions, n, 'b')<<endl;
- cout<<"c: "<<guessAllMultichoiceQuestions(questions, n, 'c')<<endl;
- cout<<"d: "<<guessAllMultichoiceQuestions(questions, n, 'd')<<endl;
- } else if (testCase == 6) {
- cout<<"Test questionWithMaxPoints"<<endl;
- int n;
- cin>>n;
- Question ** questions = new Question * [n];
- for (int j=0;j<n;j++) {
- int type;
- cin>>type; //1=MCQ, 2=TFQ, 3=STAQ
- if (type==1) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctLetter;
- questions[j] = new MultichoiceQuestion (ID, text, points, correctLetter);
- }
- else if (type==2) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>trueOrFalse;
- questions[j] = new TrueFalseQuestion (ID, text, points, (trueOrFalse==1));
- } else if (type==3) {
- cin.get();
- cin.getline(ID,5);
- cin.getline(text,200);
- cin>>points;
- cin>>correctAnswer;
- questions[j] = new ShortTextAnswerQuestion (ID, text, points, correctAnswer, true);
- }
- }
- cout<<(*(questionWithMaxPoints(questions,n)));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement