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


#pragma once

namespace MEdit {

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

	public ref class Form1 : public Form
	{
	public:
		Form1()
		{
			InitializeComponent();
		}

	private:

		TextBox^ a1; // числитель 1
		TextBox^ b1; // знаменатель 1
		TextBox^ a2; // числитель 2
		TextBox^ b2; // знаменатель 2

		TextBox^ resA; // результат числитель
		TextBox^ resB; // результат знаменатель

	void InitializeComponent()
	{
		this->Text = "Калькулятор дробей";
		this->Width = 400;
		this->Height = 250;

		// первая дробь
		a1 = gcnew TextBox();
		a1->Left = 30; a1->Top = 30; a1->Width = 50;

		b1 = gcnew TextBox();
		b1->Left = 30; b1->Top = 70; b1->Width = 50;

		Label^ line1 = gcnew Label();
		line1->Text = "—";
		line1->Left = 40; line1->Top = 50;

		// вторая дробь
		a2 = gcnew TextBox();
		a2->Left = 200; a2->Top = 30; a2->Width = 50;

		b2 = gcnew TextBox();
		b2->Left = 200; b2->Top = 70; b2->Width = 50;

		Label^ line2 = gcnew Label();
		line2->Text = "—";
		line2->Left = 210; line2->Top = 50;

		// результат
		resA = gcnew TextBox();
		resA->Left = 300; resA->Top = 30; resA->Width = 50;
		resA->ReadOnly = true;

		resB = gcnew TextBox();
		resB->Left = 300; resB->Top = 70; resB->Width = 50;
		resB->ReadOnly = true;

		Label^ line3 = gcnew Label();
		line3->Text = "—";
		line3->Left = 310; line3->Top = 50;

		// кнопки
		Button^ add = gcnew Button();
		add->Text = "+";
		add->Left = 120; add->Top = 20;
		add->Click += gcnew EventHandler(this, &Form1::Add);

		Button^ sub = gcnew Button();
		sub->Text = "-";
		sub->Left = 120; sub->Top = 50;
		sub->Click += gcnew EventHandler(this, &Form1::Sub);

		Button^ mul = gcnew Button();
		mul->Text = "*";
		mul->Left = 120; mul->Top = 80;
		mul->Click += gcnew EventHandler(this, &Form1::Mul);

		Button^ div = gcnew Button();
		div->Text = "/";
		div->Left = 120; div->Top = 110;
		div->Click += gcnew EventHandler(this, &Form1::Div);

		// добавляем
		this->Controls->Add(a1);
		this->Controls->Add(b1);
		this->Controls->Add(a2);
		this->Controls->Add(b2);
		this->Controls->Add(resA);
		this->Controls->Add(resB);
		this->Controls->Add(line1);
		this->Controls->Add(line2);
		this->Controls->Add(line3);
		this->Controls->Add(add);
		this->Controls->Add(sub);
		this->Controls->Add(mul);
		this->Controls->Add(div);
	}

	// НОД (Евклид)
	int GCD(int a, int b)
	{
		while (b != 0)
		{
			int t = b;
			b = a % b;
			a = t;
		}
		return Math::Abs(a);
	}

	void Simplify(int% a, int% b)
	{
		int g = GCD(a, b);
		a /= g;
		b /= g;
	}

	void ShowResult(int a, int b)
	{
		Simplify(a, b);
		resA->Text = a.ToString();
		resB->Text = b.ToString();
	}

	void Add(Object^ sender, EventArgs^ e)
	{
		int x1 = Convert::ToInt32(a1->Text);
		int y1 = Convert::ToInt32(b1->Text);
		int x2 = Convert::ToInt32(a2->Text);
		int y2 = Convert::ToInt32(b2->Text);

		int a = x1 * y2 + x2 * y1;
		int b = y1 * y2;

		ShowResult(a, b);
	}

	void Sub(Object^ sender, EventArgs^ e)
	{
		int x1 = Convert::ToInt32(a1->Text);
		int y1 = Convert::ToInt32(b1->Text);
		int x2 = Convert::ToInt32(a2->Text);
		int y2 = Convert::ToInt32(b2->Text);

		int a = x1 * y2 - x2 * y1;
		int b = y1 * y2;

		ShowResult(a, b);
	}

	void Mul(Object^ sender, EventArgs^ e)
	{
		int x1 = Convert::ToInt32(a1->Text);
		int y1 = Convert::ToInt32(b1->Text);
		int x2 = Convert::ToInt32(a2->Text);
		int y2 = Convert::ToInt32(b2->Text);

		ShowResult(x1 * x2, y1 * y2);
	}

	void Div(Object^ sender, EventArgs^ e)
	{
		int x1 = Convert::ToInt32(a1->Text);
		int y1 = Convert::ToInt32(b1->Text);
		int x2 = Convert::ToInt32(a2->Text);
		int y2 = Convert::ToInt32(b2->Text);

		ShowResult(x1 * y2, y1 * x2);
	}
	};
}