Advertisement
metalni

zadaca 2

Aug 3rd, 2020
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cctype>
  4.  
  5. using namespace std;
  6.  
  7. class Question{
  8.     protected:
  9.         char id[5];
  10.         char * text;
  11.         int points;
  12.         const void copy_obj(const Question &copy){
  13.             strcpy(this->id, copy.id);
  14.             this->text = new char[strlen(copy.text)+1];
  15.             strcpy(this->text, copy.text);
  16.             this->points = copy.points;
  17.         }
  18.     public:
  19.         Question(){
  20.             strcpy(this->id, "00000");
  21.             this->text = new char[0];
  22.             this->points = 0;
  23.         }
  24.         Question(const char * id, const char * text, const int points){
  25.             strcpy(this->id, id);
  26.             this->text = new char[strlen(text)+1];
  27.             strcpy(this->text, text);
  28.             this->points = points;
  29.         }
  30.         Question(const Question &copy){
  31.             this->copy_obj(copy);
  32.         }
  33.         Question &operator=(const Question &copy){
  34.             if(this != &copy){
  35.                 delete [] this->text;
  36.                 this->copy_obj(copy);
  37.             }
  38.             return *this;
  39.         }
  40.         ~Question(){
  41.             delete [] this->text;
  42.         }
  43.         virtual double checkAnswer(char * answer) = 0;
  44.         bool operator>(Question &orig){
  45.             if(this->points > orig.points)
  46.                 return true;
  47.             else
  48.                 return false;
  49.         }
  50.         const int getPoeni(){
  51.             return this->points;
  52.         }
  53.         friend ostream &operator << (ostream &os, Question &orig){
  54.             os << "Question " << orig.id << ": " << orig.text << "\n";
  55.             TrueFalseQuestion * tf = dynamic_cast<TrueFalseQuestion *>(&orig);
  56.  
  57.             os << "Correct answer: "
  58.             if(tf){
  59.                 if(tf.answer == true)
  60.                     os << "true\n";
  61.                 else
  62.                     os << "false\n";
  63.             }else
  64.              os << orig.answer << "\n";
  65.  
  66.         }
  67. };
  68.  
  69. class MultichoiceQuestion  : public Question{
  70.     private:
  71.         char answer;
  72.     public:
  73.         MultichoiceQuestion (const char * id, const char * text, const int points, const char answer) : Question(id, text, points){
  74.             this->answer = answer;
  75.         }
  76.         double checkAnswer(char * answer){
  77.             if(this->answer == answer[0])
  78.                 return this->points;
  79.             else
  80.                 return this->points - (this->points * 25 / 100);
  81.         }
  82. };
  83.  
  84. class TrueFalseQuestion : public Question{
  85.     private:
  86.         bool answer;
  87.     public:
  88.         TrueFalseQuestion(const char * id, const char * text, const int points, const bool answer): Question(id, text, points){
  89.             this->answer = answer;
  90.         }
  91.         double checkAnswer(char * answer){
  92.             if(answer[0] == 't'){
  93.                 if(this->answer == true)
  94.                     return this->points;
  95.                 else return 0;
  96.             }
  97.         }
  98. };
  99.  
  100. class ShortTextAnswerQuestion : public Question{
  101.     private:
  102.         char answer[50];
  103.         bool caseSens;
  104.     public:
  105.         ShortTextAnswerQuestion(const char * id, const char * text, const int points, const char * answer, const bool caseSens) : Question(id, text, points){
  106.             strcpy(this->answer, answer);
  107.             this->caseSens = caseSens;
  108.         }
  109.         double checkAnswer(char * answer){
  110.             if(this->caseSens){
  111.                 if(!strcmp(this->answer, answer))
  112.                     return this->points;
  113.                 else
  114.                     return 0;
  115.             } else {
  116.                 for (int i=0; i<strlen(answer); i++)
  117.                     this->answer[i] == tolower(this->answer[i]);
  118.                 if(!strcmp(this->answer, answer))
  119.                     return this->points;
  120.                 else
  121.                     return 0;
  122.             }
  123.         }
  124. };
  125.  
  126. double guessAllMultichoiceQuestions (Question ** questions, int n, char * choice){
  127.     double sum=0.0;
  128.     for(int i=0; i<n; i++){
  129.         MultichoiceQuestion * m = dynamic_cast<MultichoiceQuestion *>(questions[i]);
  130.         if(m)
  131.             sum += questions[i]->checkAnswer(choice);
  132.     }
  133.     return sum;
  134. }
  135.  
  136. Question * questionWithMaxPoints (Question ** questions, int n) {
  137.     int max = questions[0]->getPoeni();
  138.     int index = 0;
  139.     for(int i=0; i<n; i++){
  140.         if(questions[i]->getPoeni() > max){
  141.             max = questions[i]->getPoeni();
  142.             index = i;
  143.         }
  144.     }
  145.     return questions[index];
  146. }
  147.  
  148. int main () {
  149.     int testCase;
  150.     cin>>testCase;
  151.  
  152.     char text [200];
  153.     char ID [5];
  154.     char correctAnswer [50];
  155.     int points;
  156.     int caseSensitive;
  157.     char correctLetter;
  158.     int trueOrFalse;
  159.  
  160.     if (testCase==1) {
  161.         cout<<"Testing MultichoiceQuestion class, operator << and checkAnswer"<<endl;
  162.         cin.get();
  163.         cin.getline(ID,5);
  164.         cin.getline(text,200);
  165.         cin>>points;
  166.         cin>>correctLetter;
  167.         MultichoiceQuestion mq (ID, text, points, correctLetter);
  168.         cout<<mq<<endl;
  169.         for (char ans = 'a'; ans <= 'd'; ans++) {
  170.             char answer [2];
  171.             answer[0] = ans;
  172.             cout<<mq.checkAnswer(answer)<<endl;
  173.         }
  174.     }
  175.     else if (testCase == 2) {
  176.         cout<<"Testing TrueFalseQuestion class, operator << and checkAnswer"<<endl;
  177.         cin.get();
  178.         cin.getline(ID,5);
  179.         cin.getline(text,200);
  180.         cin>>points;
  181.         cin>>trueOrFalse;
  182.         TrueFalseQuestion tfq (ID, text, points, (trueOrFalse==1));
  183.         cout<<tfq<<endl;
  184.         char trueAnswer [2]; trueAnswer[0]='t';
  185.         char falseAnswer [2]; falseAnswer[0]='f';
  186.         cout<<tfq.checkAnswer(trueAnswer)<<endl;
  187.         cout<<tfq.checkAnswer(falseAnswer)<<endl;
  188.     } else if (testCase == 3) {
  189.         cout<<"Testing ShortTextAnswerQuestion class (case sensitive), operator << and checkAnswer"<<endl;
  190.         cin.get();
  191.         cin.getline(ID,5);
  192.         cin.getline(text,200);
  193.         cin>>points;
  194.         cin>>correctAnswer;
  195.         ShortTextAnswerQuestion staq (ID, text, points, correctAnswer, true);
  196.         cout<<staq<<endl;
  197.         cout<<staq.checkAnswer(correctAnswer)<<endl;
  198.         for (int i=0;i<strlen(correctAnswer);i++) {
  199.             if (isupper(correctAnswer[i]))
  200.                 correctAnswer[i]=tolower(correctAnswer[i]);
  201.             else
  202.                 correctAnswer[i]=toupper(correctAnswer[i]);
  203.         }
  204.         //cout<<correctAnswer<<endl;
  205.         cout<<staq.checkAnswer(correctAnswer)<<endl;
  206.     } else if (testCase == 4) {
  207.         cout<<"Testing ShortTextAnswerQuestion class (case insensitive, operator << and checkAnswer"<<endl;
  208.         cin.get();
  209.         cin.getline(ID,5);
  210.         cin.getline(text,200);
  211.         cin>>points;
  212.         cin>>correctAnswer;
  213.         ShortTextAnswerQuestion staq (ID, text, points, correctAnswer, false);
  214.         cout<<staq<<endl;
  215.         cout<<staq.checkAnswer(correctAnswer)<<endl;
  216.         for (int i=0;i<strlen(correctAnswer);i++) {
  217.             if (isupper(correctAnswer[i]))
  218.                 correctAnswer[i]=tolower(correctAnswer[i]);
  219.             else
  220.                 correctAnswer[i]=toupper(correctAnswer[i]);
  221.         }
  222.         //cout<<correctAnswer<<endl;
  223.         cout<<staq.checkAnswer(correctAnswer)<<endl;
  224.     } else if (testCase == 5) {
  225.         cout<<"Test questAllMultichoiceQuestions"<<endl;
  226.         int n;
  227.         cin>>n;
  228.         Question ** questions = new Question * [n];
  229.         for (int j=0;j<n;j++) {
  230.             int type;
  231.             cin>>type; //1=MCQ, 2=TFQ, 3=STAQ
  232.             if (type==1) {
  233.                 cin.get();
  234.                 cin.getline(ID,5);
  235.                 cin.getline(text,200);
  236.                 cin>>points;
  237.                 cin>>correctLetter;
  238.                 questions[j] = new MultichoiceQuestion (ID, text, points, correctLetter);
  239.             }
  240.             else if (type==2) {
  241.                 cin.get();
  242.                 cin.getline(ID,5);
  243.                 cin.getline(text,200);
  244.                 cin>>points;
  245.                 cin>>trueOrFalse;
  246.                 questions[j] = new TrueFalseQuestion (ID, text, points, (trueOrFalse==1));
  247.             } else if (type==3) {
  248.                 cin.get();
  249.                 cin.getline(ID,5);
  250.                 cin.getline(text,200);
  251.                 cin>>points;
  252.                 cin>>correctAnswer;
  253.                 questions[j] = new ShortTextAnswerQuestion (ID, text, points, correctAnswer, true);
  254.             }
  255.  
  256.         }
  257.  
  258.         cout<<"a: "<<guessAllMultichoiceQuestions(questions, n, 'a')<<endl;
  259.         cout<<"b: "<<guessAllMultichoiceQuestions(questions, n, 'b')<<endl;
  260.         cout<<"c: "<<guessAllMultichoiceQuestions(questions, n, 'c')<<endl;
  261.         cout<<"d: "<<guessAllMultichoiceQuestions(questions, n, 'd')<<endl;
  262.     } else if (testCase == 6) {
  263.         cout<<"Test questionWithMaxPoints"<<endl;
  264.         int n;
  265.         cin>>n;
  266.         Question ** questions = new Question * [n];
  267.         for (int j=0;j<n;j++) {
  268.             int type;
  269.             cin>>type; //1=MCQ, 2=TFQ, 3=STAQ
  270.             if (type==1) {
  271.                 cin.get();
  272.                 cin.getline(ID,5);
  273.                 cin.getline(text,200);
  274.                 cin>>points;
  275.                 cin>>correctLetter;
  276.                 questions[j] = new MultichoiceQuestion (ID, text, points, correctLetter);
  277.             }
  278.             else if (type==2) {
  279.                 cin.get();
  280.                 cin.getline(ID,5);
  281.                 cin.getline(text,200);
  282.                 cin>>points;
  283.                 cin>>trueOrFalse;
  284.                 questions[j] = new TrueFalseQuestion (ID, text, points, (trueOrFalse==1));
  285.             } else if (type==3) {
  286.                 cin.get();
  287.                 cin.getline(ID,5);
  288.                 cin.getline(text,200);
  289.                 cin>>points;
  290.                 cin>>correctAnswer;
  291.                 questions[j] = new ShortTextAnswerQuestion (ID, text, points, correctAnswer, true);
  292.             }
  293.  
  294.         }
  295.  
  296.         cout<<(*(questionWithMaxPoints(questions,n)));
  297.     }
  298.  
  299.     return 0;
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement