前編
あなたは間違いなくあなたの質問を編集して保存するために、Excelを使用することができます。しかし、それを保存するときは、ファイル形式に注意してください。
Excelファイルを.xls
または.xlsx
ファイルの代わりに.csv
ファイルとして保存するとします。 [ファイル] - [名前を付けてファイルを保存]に移動し、タイプを.csv
に変更してください。
これは、.csv
ファイルを読む方がずっと簡単だからです。 .csv
ファイルには、各セルがカンマ(,
)で区切られ、各行が改行('\n'
)文字で区切られています。
だから、ここのサンプルExcelファイルです:
私は.csv
ファイルとして保存して(ここではアトム、)テキストエディタを使用して、それを開いた後、それは次のようになります。
その後、ファイルを読むためにコードを書くだけで済みます。
これは私が(私は初心者のためのコードがより明確にするために、過剰なコメントを追加しました)に使用されるコードです:
#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からそれを借り)old method異なり
#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);
、これは下端に向かってバイアスを作成できません。ただし、新しいエンジンはC++ 11コンパイラでのみ使用できます。だから心に留めておいてください。古い方法を使用する必要がある場合は、answerに従ってバイアスを補正することができます。
5つの異なる番号を選択するには、乱数を生成するたびに配列に格納し、この番号がすでに使用されているかどうかを確認します。これは質問の繰り返しを防ぐことができます。
最も簡単なのようなデータを持っているでしょう。ここ
は、私は自分が勉強を支援するために作られたquizzerですあなたはランダムなレコードを取得する必要がある場合は、番号でインデックスをサポートするデータ構造を使用する必要があります、助けて嬉しいです –