数组,对象,类来组织具有多项选择和对/错问题的测试问卷程序

时间:2018-08-30 08:42:56

标签: c++ arrays class object vector

我的问题与学校作业有关,我想强调的是,我不是在找人给我答案。但是,我正在寻找方向,以便可以解决此问题。

我制作了一个包含多项选择题和是非题的测试程序。我将此信息放在2个.txt文件中。我正在按班级组织代码。我需要从.txt文件中提取信息,将其输出到屏幕上,将用户输入的结果与测试答案进行比较,然后输出测试成绩。

我知道如何从.txt文件中提取内容,但这有多种选择以及提供字符串和char数据的真假问题。我考虑过创建数组来存储问题和答案。但是,我开始质疑是否有一种更有效的方法来组织这些数据。似乎我需要太多的数组,也许1或2个方法可能就足够了。我知道如何计算测验分数,不需要那部分的指导。

我正在使用Microsoft Visual Studio IDE开发此C ++代码。

我研究了数组,向量,对象,类以及许多我认为可能与此问题相关的主题。我通读了大学教科书,听了讲课,看了YouTube视频,并通过Facebook咨询了一些程序员。我已经用Google搜索了各种问题,并阅读了有关各种C ++主题和编程示例的答案。我仍然感到困惑的事实使我相信,也许我只是没有像应该那样去掌握这些概念。

具有更多专业知识的人可以向我指出正确的方向吗?

******** \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ****

这是我到目前为止创建的代码:

// Test.cpp : This file contains the 'main' function. Program execution 
begins and ends there

#include "pch.h"
#include "QuestionMC.h"
#include "QuestionTF.h"

#include "QuestionMC.cpp"
#include "QuestionTF.cpp"

#include "TestBankMC.txt"
#include "TestBankTF.txt"

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

int main() {

    string question, answer;
    string q, a;
    int v;
    int value;

    cout << "====================================================\n";
    cout << "==                                                ==\n";
    cout << "==         WELCOME TO THE FBI ENTRANCE EXAM!      ==\n";
    cout << "==                                                ==\n";
    cout << "====================================================\n";
    cout << endl;
    cout << endl;
    cout << "You will be asked a series of 11 questions consisting of both 
            True/False questions and multiple choice questions.\n";

   cout << "Please answer each question to the best of your ability\n\n\n" << 
           "Good luck!\n\n\n" << endl;


  //open file TestBankTF.txt
  ifstream file("TestBankTF.txt");

       // check for errors
        if (infile.fail()) {
            cerr << "Error Opening File" << endl;
            exit(1);
        }

        if (file.is_open())
        {
           string myTestBankTF[12];
           for (size_t i = 0; i < 12; i++){
               {
                    file >> myTestBankTF[i];
               }
        }

        // close TestBank.txt file
        infile.close();

    cout << "Please answer the following true or false questions with either 
            'true' or 'false': \n\n";
    cin >> question;

    string answersTF[3];

    for (size_t i = 0; i < 12; i++)
    {
        if (myTestBankTF[12 == question)
        {
            cout << question << "\n\n";
            cout << "Answer: " << endl;
            cin >> answersTF[3];
        }
        else
        {
            cerr << question << "Error loading in myTestBankTF[].";
        }
    } 

    //open file TestBankMC.txt
    ifstream file("TestBankMC.txt");

    //check for errors
    if (infile.fail())
        {
            cerr << "Error opening file" << endl;
            exit(1);
        }

    if (file.is_open())
        {
            string myTestBankMC[71];

            for (size_t i = 0; i < 71; i++)
            {
                string myTestBankMC[i];
            }
        }

    // close TestBankMC.txt file
    infile.close();

    cout << "Please answer the following multiple choice questions with the 
            appropriate letter answer (A, B, C, D, or E): \n\n";
    cin >> question;

    string answersMC[11];
    string TestBankMC[71];

    for (size_t i = 0; i < 71; i++)
        {
            if (myTestBankMC[71] == question)
                {
                    cout << question << "\n\n";
                    cout << "Answer: " << endl;
                    cin >> answersMC[11];
                    cin.ignore();
                }
            else
                {
                    cerr << question << "Error loading in myTestBankTF[].";
                }
        }

    return 0;

} // end of main function

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// header file is class Questions' class and function declarations


    class Question
    {
public:
    Question();

    ~Question();

    Question(string, int, string)

    const std::string& getQuestion() const;

    const std::string& getAnswer() const;

    int getValue() const;

    virtual std::string printOptions() const; 


protected:
    std::string question, answer;
    int value;
};

#endif 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// class Question's class and function definitions

Question::Question() {}

Question::~Question() {}

Question :: Question (std::string q, int v, std::string a)
     : question(std::move(q)), answer(std::move(a)), value(v) {}

Question :: const std::string& getQuestion() const
{
    return question;
}

Question :: const string& getAnswer() const
{
    return answer;
}

Question :: int getValue() const
{
    return value;
}

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// header file is class QuestionMC's class and function declarations

class QuestionMC:: public Question
{
public:
     QuestionMC();

    ~QuestionMC();

    QuestionMC(const std::string&, int, const std::string&)

    void addOption(const std::string& option);

    string printOptions() const override {};

private:
    vector options;
    static const size_t MaxOptions = 6;
};

#endif 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// subclass QuestionMC's class and member functions definitions

QuestionMC::QuestionMC(){}

QuestionMC::~QuestionMC(){}

QuestionMC(const string& q, int v, const string& a)
     : Question(q, v, a) {}

void QuestionMC::addOption(const string& option)
{
    if (options.size() < MaxOptions)
        options.push_back(option);
}

string QuestionMC::printOptions() const override
{
    ostringstream oss;
    char first = 'A';
    for (auto& option : options)
        oss << (first > 'A' ? "\n" : " ") << first++ << "." << option;
    return oss.str();
 }    

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// class and function declarations for subclass QuestionTF.h

class QuestionTF:public Question
{
   {
public:
    QuestionTF();

    ~QuestionTF();

    QuestionTF(const std::string&, int, const std::string&)

    std::string printOptions() const override {};  
};

#endif

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// Class and functions definitions for subclass QuestionTF of class Question

QuestionTF::QuestionTF(){}

QuestionTF::~QuestionTF(){}

QuestionTF::QuestionTF(const std::string& q, int v, const std::string& a)
    : Question(q, v, a) {}

QuestionTF:: std::string printOptions() const override
{
    return "True/False";
}

2 个答案:

答案 0 :(得分:0)

好的,我会尽力使这个答案含糊不清。

  1. 答案数据应在txt文件中具有格式。让我们说“ *”代表问题的开始和结束。 “ +”代表正确答案,“-”代表错误答案。

    1. 设有一个问题类和一个答案类。确保该课程中有一个答案列表

    2. 为问题创建一个类并将所有答案添加到其中(使用part1中的格式将txt输入字符串的一部分切成单独的问题和答案)

    3. 有问题列表

    4. 一个接一个地显示问题

    5. 计算结果

词后,您应该在具体而简洁的问题命名器中更多地使用堆栈溢出。这类模棱两可的问题应该用谷歌搜索。

答案 1 :(得分:0)

我建议:

  • 一个称为cQuestion的基类。这将具有适用于各种问题的代码
  • cQuestion的专业化名称为cTF。电汇问题代码
  • cQuestion的专业名称为cMC。 MC问题代码
  • 一个包含各种问题的类,称为cBank。

您的主要方法如下所示:

cBank Bank

// read questions from txt file
Bank.Read( "TestBankTF.txt");
Bank.Read( "TestBankMC.txt");

// Loop over questions
for( cQuestion * Q : Bank )
{
   // Pose question
   Q->Pose();

   // Get Answer
   Q->Answer();

   // Calculate question score
   Q->Calculate();
}

// Display results
Bank.Results():

此设计的最小实现代码为available here