첫 번째 부분
당신은 당신의 질문을 편집하고 저장 Excel을 사용하여 확실히 할 수 있습니다. 그러나 파일을 저장할 때 파일 형식에주의하십시오.
Excel 파일을 .xls
또는 .xlsx
파일 대신 .csv
파일로 저장하려고합니다. 파일 -> 다른 이름으로 파일 저장 ->으로 이동하여 유형을 .csv
으로 변경하십시오.
.csv
파일을 읽는 것이 훨씬 쉽기 때문입니다. .csv
파일은 각 셀을 쉼표 (,
)로 구분하고 각 행을 줄 바꿈 ('\n'
) 문자로 구분합니다.
그래서, 여기에 샘플 엑셀 파일입니다

가 나는 .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;
}
두 번째 부분
은 임의의 숫자를 선택하려면, 당신은이를 사용할 수 있습니다 코드 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);
(나는 another answer로부터 차용)이 하단쪽으로 바이어스를 생성하지 않는다. 그러나 새로운 엔진은 C++ 11 컴파일러에서만 사용할 수 있습니다. 그래서 그걸 명심하십시오. 이전 방법을 사용해야하는 경우 다음을 따라 편차를 수정할 수 있습니다 answer.
5 개의 다른 숫자를 선택하려면 임의의 숫자를 생성 할 때마다 배열에 저장하고이 숫자가 이미 사용되었는지 확인하십시오. 이것은 질문의 반복을 막을 수 있습니다.
쉬운처럼 데이터를 것 : 여기
나 자신이 시험 공부에 도움이 만든 quizzer입니다 질문 당 줄, 당신은 임의의 레코드를 가져올 필요가 있다면 숫자로 색인 생성을 지원하는 데이터 구조를 사용해야합니다 도움이 되었습니까 –