2016-12-31 3 views
0

という質問と4つの回答を示すコードを書いて、正しいものを選択して選択を確認します。 今私は6つの列:質問/ a/b/c/d/correct_answerを含むテーブルを作成し、私の質問で約百行を作成します。今私のプログラムはにランダムに5をテーブルから選んで、それをユーザーに見せたいと思います。テーブルからC++クイズアプリケーションへのデータ

最初の質問はです。Excelでテーブルを作成してVisual Studioよりも使用できますか?そうでない場合は、できるだけ簡単にこのようなテーブルを作成するためにどのソフトウェアを使用する必要がありますか、Visual Studioプロジェクトに実装する方法は?はいの場合、Visual StudioプロジェクトにExcelテーブルを実装する方法は?

2番目の質問は、私のテーブルから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; 
} 
+0

最も簡単なのようなデータを持っているでしょう。ここ

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

は、私は自分が勉強を支援するために作られたquizzerですあなたはランダムなレコードを取得する必要がある場合は、番号でインデックスをサポートするデータ構造を使用する必要があります、助けて嬉しいです –

答えて

0

前編

あなたは間違いなくあなたの質問を編集して保存するために、Excelを使用することができます。しかし、それを保存するときは、ファイル形式に注意してください。

Excelファイルを.xlsまたは.xlsxファイルの代わりに.csvファイルとして保存するとします。 [ファイル] - [名前を付けてファイルを保存]に移動し、タイプを.csvに変更してください。

これは、.csvファイルを読む方がずっと簡単だからです。 .csvファイルには、各セルがカンマ(,)で区切られ、各行が改行('\n')文字で区切られています。

だから、ここのサンプルExcelファイルです:

Sample Data in Excel format

私は.csvファイルとして保存して(ここではアトム、)テキストエディタを使用して、それを開いた後、それは次のようになります。

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からそれを借り)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つの異なる番号を選択するには、乱数を生成するたびに配列に格納し、この番号がすでに使用されているかどうかを確認します。これは質問の繰り返しを防ぐことができます。

+0

お返事ありがとうございます。私はあなたの手順に従って、私のプログラムは私のテーブルをとてもうまく印刷します。 – Mac

+0

しかし、冒頭で批判されていた私の弦に、どのように各行の部分(質問、回答a、回答bなど)を付けるのですか? – Mac

+0

さて、私は解決策を見つけたと思うが、比較のためにあなたの投稿を投稿することができる。 – Mac

0

質問1に答えるには:Excelで、[ファイル]> [名前を付けて保存]をクリックします。 UTF-16 Unicode Text(.txt)を選択し、ファイルに名前を付け、プログラムがアクセスできる場所に保存します。 、同じディレクトリにあるテキストファイルで

#include <fstream> 

質問2に答えるために

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

を使用します:あなたのC++プログラムでは、テキストファイルのfstreamのライブラリを使うのrand()関数が存在することをゼロとRAND_MAXの間の整数を返します。次に、モジュラス(%)を使用して、希望する範囲の乱数を得ることができます。 、1

#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 
+0

rand()関数は下端に向かってバイアスを作成します。そして、これは(1,100)のような小さな範囲でも非常に目に見えます。 C++ 11コンパイラを使わない場合を除き、[ここ](http://stackoverflow.com/a/19728404/5055644)の方法を使用することができます。 なぜ、 'char'sの代わりに' string'sを使用してみませんか?あなたは文字列の長さについて心配する必要はありません(または私はそれを前提に間違っていますか?) – Apara

関連する問題