Загрузка данных


#pragma once
using namespace System;
using namespace System::Windows::Forms;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace System::Drawing;

public ref class Question
{
public:
    String^ text;
    String^ image;
    int type;

    array<String^>^ vars;

    String^ correct;
};

public ref class Form1 : public Form
{
private:

    List<Question^>^ list;
    int index;
    int score;

    Label^ labelQuestion;
    Label^ labelNum;

    PictureBox^ pictureBox1;

    RadioButton^ radio1;
    RadioButton^ radio2;
    RadioButton^ radio3;

    TextBox^ textBoxAnswer;

    Button^ buttonNext;

public:

    Form1()
    {
        InitializeComponent();

        list = gcnew List<Question^>();
        index = 0;
        score = 0;

        LoadQuestions();

        ShowQuestion();
    }

private:

    void InitializeComponent()
    {
        this->Width = 700;
        this->Height = 500;

        labelNum = gcnew Label();
        labelNum->Left = 20;
        labelNum->Top = 10;
        labelNum->Width = 200;

        labelQuestion = gcnew Label();
        labelQuestion->Left = 20;
        labelQuestion->Top = 40;
        labelQuestion->Width = 500;

        pictureBox1 = gcnew PictureBox();
        pictureBox1->Left = 20;
        pictureBox1->Top = 80;
        pictureBox1->Width = 250;
        pictureBox1->Height = 150;
        pictureBox1->SizeMode =
            PictureBoxSizeMode::StretchImage;

        radio1 = gcnew RadioButton();
        radio1->Left = 20;
        radio1->Top = 250;

        radio2 = gcnew RadioButton();
        radio2->Left = 20;
        radio2->Top = 280;

        radio3 = gcnew RadioButton();
        radio3->Left = 20;
        radio3->Top = 310;

        textBoxAnswer = gcnew TextBox();
        textBoxAnswer->Left = 20;
        textBoxAnswer->Top = 250;
        textBoxAnswer->Width = 250;

        buttonNext = gcnew Button();
        buttonNext->Text = "OK";
        buttonNext->Left = 20;
        buttonNext->Top = 380;

        buttonNext->Click +=
            gcnew EventHandler(this,
                &Form1::buttonNext_Click);

        Controls->Add(labelNum);
        Controls->Add(labelQuestion);
        Controls->Add(pictureBox1);

        Controls->Add(radio1);
        Controls->Add(radio2);
        Controls->Add(radio3);

        Controls->Add(textBoxAnswer);

        Controls->Add(buttonNext);
    }

    void LoadQuestions()
    {
        array<String^>^ s =
            File::ReadAllLines("questions.txt");

        for (int i = 0; i < s->Length; i += 8)
        {
            Question^ q = gcnew Question();

            q->text = s[i];
            q->image = s[i + 1];
            q->type = Convert::ToInt32(s[i + 2]);

            q->vars = gcnew array<String^>(3);

            q->vars[0] = s[i + 3];
            q->vars[1] = s[i + 4];
            q->vars[2] = s[i + 5];

            q->correct = s[i + 6];

            list->Add(q);
        }
    }

    void ShowQuestion()
    {
        if (index >= list->Count)
        {
            Finish();
            return;
        }

        Question^ q = list[index];

        labelNum->Text =
            "Вопрос " +
            (index + 1) + "/" + list->Count;

        labelQuestion->Text = q->text;

        if (q->image != "-")
            pictureBox1->Image =
                Image::FromFile(q->image);
        else
            pictureBox1->Image = nullptr;

        radio1->Visible = false;
        radio2->Visible = false;
        radio3->Visible = false;

        textBoxAnswer->Visible = false;

        if (q->type == 1)
        {
            radio1->Visible = true;
            radio2->Visible = true;
            radio3->Visible = true;

            radio1->Text = q->vars[0];
            radio2->Text = q->vars[1];
            radio3->Text = q->vars[2];
        }
        else
        {
            textBoxAnswer->Visible = true;
            textBoxAnswer->Text = "";
        }
    }

    void buttonNext_Click(
        Object^ sender,
        EventArgs^ e)
    {
        Question^ q = list[index];

        bool ok = false;

        if (q->type == 1)
        {
            int ans = 0;

            if (radio1->Checked) ans = 1;
            if (radio2->Checked) ans = 2;
            if (radio3->Checked) ans = 3;

            if (ans ==
                Convert::ToInt32(q->correct))
                ok = true;
        }

        if (q->type == 2)
        {
            if (textBoxAnswer->Text ==
                q->correct)
                ok = true;
        }

        if (q->type == 3)
        {
            String^ a =
                textBoxAnswer->Text->ToUpper();

            array<String^>^ k =
                q->correct->Split(';');

            bool all = true;

            for each(String ^ x in k)
            {
                if (!a->Contains(x))
                    all = false;
            }

            ok = all;
        }

        if (ok) score++;

        index++;

        ShowQuestion();
    }

    void Finish()
    {
        int mark = 2;

        double p =
            (double)score / list->Count;

        if (p >= 0.9) mark = 5;
        else if (p >= 0.7) mark = 4;
        else if (p >= 0.5) mark = 3;

        String^ fio =
            Microsoft::VisualBasic::Interaction
            ::InputBox(
                "Введите ФИО");

        StreamWriter^ sw =
            gcnew StreamWriter(
                "Журнал.txt",
                true);

        sw->WriteLine(
            fio + " Оценка: " + mark);

        sw->Close();

        MessageBox::Show(
            "Верно: " + score +
            "\nОценка: " + mark);

        Close();
    }
};