gpt4 book ai didi

c++ - 数组、对象、类来组织具有多项选择和真/假问题的测试问卷程序

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:04 27 4
gpt4 key购买 nike

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

我制作了一个由多项选择题和判断题组成的测试程序。我将此信息放在 2 个 .txt 文件中。我正在按类(class)组织我的代码。我需要从 .txt 文件中提取信息,将其输出到屏幕,将用户输入的结果与测试答案进行比较,并输出测试分数。

我知道如何从 .t​​xt 文件中提取数据,但这有多项选择题和给出字符串和字符数据的判断题。我考虑过创建数组来存储问题和答案;然而,我开始质疑是否有更有效的方式来组织这些数据。似乎我需要太多数组,也许 1 或 2 个方法就足够了。我知道如何计算考试分数,不需要这部分的指导。

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

我研究了数组、 vector 、对象、类以及我认为可能与此问题相关的更多主题。我通读了我的大学教科书,听了讲座,看了 YouTube 视频,并通过 Facebook 咨询了一些程序员。我用谷歌搜索了各种 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";
}

最佳答案

我提议:

  • 一个名为 cQuestion 的基类。这将具有适用于各种问题的代码
  • cQuestion 的一个特例,称为 cTF。 T/F问题代码
  • 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 .

关于c++ - 数组、对象、类来组织具有多项选择和真/假问题的测试问卷程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52092678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com