从表到C ++测验应用程序的数据

时间:2016-12-31 01:54:15

标签: c++ excel random import-from-excel

我写了一个代码,显示一个问题和4个答案,要求选择正确的代码,并检查选项。 现在我要构建一个包含6列的表: question / a / b / c / d / correct_answer ,以及大约数百行我的问题。现在我希望我的程序从表中随机选择5个的问题并将其显示给用户。

第一个问题是,我可以在Excel中使用它而不是在Visual Studio中使用它吗?如果没有,我应该使用什么软件尽可能简单地做这样的表,以及如何将它实现到我的Visual Studio项目中?如果是,如何将Excel表实现到我的Visual Studio项目?

第二个问题是,我应该写什么最简单的代码来从我的表中随机选择100个问题中的5个?

问题的代码是:

int main()
{

    string ans; //Users input
    string cans; //Correct answer, comes from table
    string quest; //Question, comes from table
    string a; // Answers, come from table
    string b;
    string c;
    string d;
    int points; //Amount of points

    points = 0;

    cout << "\n" << quest << "\n" << a << "\n" << b << "\n" << c << "\n" << d << "\n";
    cout << "Your answer is: ";
    cin >> ans;
    if (ans == cans)
    {
        points = points + 1;
        cout << "Yes, it is correct\n";
    }
    else
    {
        cout << "No, correct answer is " << cans << "\n";
    }


    return 0;
}

2 个答案:

答案 0 :(得分:0)

第一部分

您绝对可以使用Excel编辑问题并保存。但是当你保存它时,要注意文件格式。

您希望将Excel文件另存为.csv文件,而不是.xls.xlsx文件。只需转到文件 - &gt;将文件另存为 - &gt;并将类型更改为.csv

这是因为阅读.csv文件要容易得多。 .csv个文件的每个单元格用逗号(,)分隔,每行用换行符('\n')分隔。

所以,这是一个示例Excel文件:

Sample Data in Excel format

将其保存为.csv文件并使用文本编辑器(Atom,此处)打开后,它看起来像这样:

Sample Data in .csv format

之后,您只需要编写一些代码来读取文件。

这是我使用的代码(我添加了过多的注释,以使代码对初学者更明确):

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;
const int MAX_QUESTIONS = 3;
const int COLUMNS = 6; //Question, Options A, B, C, D, Correct Answer

int main() {

    ifstream fp;
    fp.open("test.csv");

    //If file could not be opened
    if (fp.fail()) {
        std::cout << "failed to open file" << std::endl;
        return 1;
    }

    //Create a 2D vector of strings to store the values in the file
    vector< vector<string> > table;

    string line;

    //Loop through the entire file (fp)
    //Store all values found until I hit a newline character ('\n') in the string line
    //This loop automatically exits when the end-of-file is encountered
    while (getline(fp, line, '\n')) {

        //Create an temporary vector of strings to simulate a row in the excel file
        vector<string> row;

        //Now I am passing in this string into a string stream to further parse it
        stringstream ss;
        ss << line;

        string part;

        //Similar to the outer loop, I am storing each value until I hit a comma into the string part
        while (getline(ss, part, ',')) {

            //Add this to the row
            row.push_back(part);
        }

        //Add this row to the table
        table.push_back(row);
    }

    //Print out the entire table to make sure your values are right
    for (int i = 0; i <= MAX_QUESTIONS; ++i) {
        for (int j = 0; j < COLUMNS; ++j) {
            cout << table[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

第二部分

要选择随机数,您可以使用此代码(我从another answer借用了它)

#include <random>

std::random_device rd;     // only used once to initialise (seed) engine
std::mt19937 rng(rd());    // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(min,max); // guaranteed unbiased

auto random_integer = uni(rng);

old method不同,这并不会向低端产生偏见。但是,新引擎仅在C ++ 11编译器中可用。所以记住这一点。如果您需要使用旧方法,可以按照answer

更正偏差

要选择5个不同的数字,每次生成一个随机数时,将其存储在一个数组中,并检查该数字是否已被使用。这可以防止重复问题。

答案 1 :(得分:0)

要回答问题1:在Excel中,单击文件&gt;另存为。选择UTF-16 Unicode文本(.txt),命名文件,并将其保存在程序可以访问的位置。在C ++程序中,将fstream库用于文本文件:

#include <fstream>

将文本文件放在同一目录中,使用

ifstream fin;
fin.open("FILENAME.txt");
.
.
.
fin >> variable;
.
.
.
fin.close();

回答问题2:有一个rand()函数返回一个介于0和RAND_MAX之间的整数。然后,您可以使用模数(%)来获得所需范围内的随机数。

#include <cstdlib> // for srand() and rand()
#include <ctime> // for time()
.
.
.
srand(time(NULL)); // seed the RNG on seconds
.
.
.
var = rand() % ONE_MORE_THAN_MAX;

这是我为帮助自己学习而做的一件事:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;

const char QUESTIONS_FILE_NAME[] = "questions.dat";
const char QUESTION_ANSWER_SEPARATOR = ',';
const char QUESTION_SEPARATOR = '\n';

const short MAX_QUESTION_LENGTH = 300;
const short MAX_ANSWER_LENGTH = 300;
const short MAX_NUM_QUESTIONS = 300;


int main()
{
  char questions[MAX_NUM_QUESTIONS][MAX_QUESTION_LENGTH];
  char answers[MAX_NUM_QUESTIONS][MAX_ANSWER_LENGTH];
  short counter = 0;

  short question_choice;

  char user_answer[MAX_ANSWER_LENGTH];

  ifstream fin;
  fin.open( QUESTIONS_FILE_NAME );


  srand(time(NULL));


  while(fin.getline( questions[counter], MAX_QUESTION_LENGTH-1, 
    QUESTION_ANSWER_SEPARATOR ))
  {
    fin.getline( answers[counter], MAX_ANSWER_LENGTH-1,
      QUESTION_SEPARATOR );
    counter++;
  }

  fin.close();

  cout << endl << "Press CTRL+C to quit at any time" << endl << endl;

  while ( counter > 0 )
  {
    question_choice = rand() % counter;

    cout << endl << questions[question_choice] << " ";
    cin >> user_answer;

    if ( strcmp( user_answer, answers[question_choice] ) )
    {
      cout << endl << "Incorrect" << endl 
        << "It was " << answers[question_choice] << endl;
      strcpy( questions[counter], questions[question_choice] );
      strcpy( answers[counter], answers[question_choice] );
      counter++;
    }
    else
    {
      cout << endl << "Correct" << endl;
      counter--;
      strcpy( questions[question_choice], questions[counter] );
      strcpy( answers[question_choice], answers[counter] );
    }  
  }


  cout << endl << "You Win!!!!" << endl;

  return 0;
}

questions.dat会有这样的数据

In what year was the Pendleton Civil Service Reform Act enacted?
a.1873
b.1883
c.1893
d.1903,b
In what year did Hurricane Katrina occur?
a.2001
b.2003
c.2005
d.2007,c