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


#pragma once

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::IO;

namespace Examinator
{
	public ref class Form1 :
		public System::Windows::Forms::Form
	{
	public:

		Form1(void)
		{
			InitializeComponent();

			index = 0;
			score = 0;

			LoadQuestions();
			ShowQuestion();
		}

	protected:

		~Form1()
		{
			if (components)
				delete components;
		}

	private:

		array<String^>^ lines;

		int index;
		int score;

		Label^ label1;

		PictureBox^ pictureBox1;

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

		TextBox^ textBox1;

		Button^ button1;

		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

		void InitializeComponent(void)
		{
			this->label1 =
				gcnew Label();

			this->pictureBox1 =
				gcnew PictureBox();

			this->radio1 =
				gcnew RadioButton();

			this->radio2 =
				gcnew RadioButton();

			this->radio3 =
				gcnew RadioButton();

			this->textBox1 =
				gcnew TextBox();

			this->button1 =
				gcnew Button();

			this->SuspendLayout();

			label1->Location =
				Point(20,20);

			label1->Size =
				Drawing::Size(500,30);

			pictureBox1->Location =
				Point(20,60);

			pictureBox1->Size =
				Drawing::Size(250,150);

			pictureBox1->SizeMode =
				PictureBoxSizeMode::StretchImage;

			radio1->Location =
				Point(20,230);

			radio2->Location =
				Point(20,260);

			radio3->Location =
				Point(20,290);

			textBox1->Location =
				Point(20,230);

			textBox1->Size =
				Drawing::Size(250,20);

			button1->Text =
				L"OK";

			button1->Location =
				Point(20,350);

			button1->Click +=
				gcnew EventHandler(
				this,
				&Form1::button1_Click);

			this->Controls->Add(label1);
			this->Controls->Add(pictureBox1);

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

			this->Controls->Add(textBox1);

			this->Controls->Add(button1);

			this->Text =
				L"Экзаменатор";

			this->ClientSize =
				Drawing::Size(600,420);

			this->ResumeLayout(false);
		}

#pragma endregion

	private:

		void LoadQuestions()
		{
			lines =
				File::ReadAllLines(
				"questions.txt");
		}

		void ShowQuestion()
		{
			if (index >= lines->Length)
			{
				Finish();
				return;
			}

			label1->Text =
				lines[index];

			String^ img =
				lines[index+1];

			if (img != "-")
			{
				pictureBox1->Image =
					Image::FromFile(img);
			}
			else
			{
				pictureBox1->Image =
					nullptr;
			}

			int type =
				Convert::ToInt32(
				lines[index+2]);

			radio1->Visible =
				false;

			radio2->Visible =
				false;

			radio3->Visible =
				false;

			textBox1->Visible =
				false;

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

				radio2->Visible =
					true;

				radio3->Visible =
					true;

				radio1->Text =
					lines[index+3];

				radio2->Text =
					lines[index+4];

				radio3->Text =
					lines[index+5];
			}
			else
			{
				textBox1->Visible =
					true;

				textBox1->Text =
					"";
			}
		}

		void button1_Click(
			Object^ sender,
			EventArgs^ e)
		{
			bool ok =
				false;

			int type =
				Convert::ToInt32(
				lines[index+2]);

			String^ correct =
				lines[index+6];

			if (type == 1)
			{
				int ans = 0;

				if (radio1->Checked)
					ans = 1;

				if (radio2->Checked)
					ans = 2;

				if (radio3->Checked)
					ans = 3;

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

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

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

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

				if (a->Contains(k[0]) &&
					a->Contains(k[1]) &&
					a->Contains(k[2]))
				{
					ok = true;
				}
			}

			if (ok)
				score++;

			index += 8;

			ShowQuestion();
		}

		void Finish()
		{
			textBox1->Visible =
				true;

			String^ fio =
				textBox1->Text;

			if (fio == "")
				fio = "Без имени";

			int mark = 2;

			if (score >= 3)
				mark = 5;

			else if (score == 2)
				mark = 4;

			else if (score == 1)
				mark = 3;

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

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

			sw->Close();

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

			Close();
		}
	};
}