Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
03.09.2024
Размер:
15.77 Кб
Скачать
#pragma once

namespace pr4 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Сводка для MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: добавьте код конструктора
			//
		}

	protected:
		/// <summary>
		/// Освободить все используемые ресурсы.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::RadioButton^ radioButton1;
	private: System::Windows::Forms::RadioButton^ radioButton2;

	private: System::Windows::Forms::Button^ button1;
	private: System::Windows::Forms::TextBox^ textBoxValue;


	private: System::Windows::Forms::TextBox^ textBoxCurrency;
	private: System::Windows::Forms::TextBox^ textBoxUnconvertedValue;


	private: System::Windows::Forms::Label^ label1;
	private: System::Windows::Forms::Label^ labelCurrency;

	private: System::Windows::Forms::RadioButton^ radioButton4;
	private: System::Windows::Forms::RadioButton^ radioButton3;
	private: System::Windows::Forms::Label^ labelFromCurrency;
	private: System::Windows::Forms::TextBox^ textBoxConvertedValue;


	private: System::Windows::Forms::Label^ labelToCurrency;
	private: System::Windows::Forms::GroupBox^ groupBox1;
	private: System::Windows::Forms::GroupBox^ groupBox2;
	private: System::Windows::Forms::GroupBox^ groupBox3;



	protected:

	private:
		/// <summary>
		/// Обязательная переменная конструктора.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Требуемый метод для поддержки конструктора — не изменяйте 
		/// содержимое этого метода с помощью редактора кода.
		/// </summary>
		void InitializeComponent(void)
		{
			this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->textBoxValue = (gcnew System::Windows::Forms::TextBox());
			this->textBoxCurrency = (gcnew System::Windows::Forms::TextBox());
			this->textBoxUnconvertedValue = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->labelCurrency = (gcnew System::Windows::Forms::Label());
			this->labelFromCurrency = (gcnew System::Windows::Forms::Label());
			this->textBoxConvertedValue = (gcnew System::Windows::Forms::TextBox());
			this->labelToCurrency = (gcnew System::Windows::Forms::Label());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->groupBox3->SuspendLayout();
			this->SuspendLayout();
			// 
			// radioButton1
			// 
			this->radioButton1->AutoSize = true;
			this->radioButton1->Checked = true;
			this->radioButton1->Location = System::Drawing::Point(6, 21);
			this->radioButton1->Name = L"radioButton1";
			this->radioButton1->Size = System::Drawing::Size(141, 20);
			this->radioButton1->TabIndex = 0;
			this->radioButton1->TabStop = true;
			this->radioButton1->Text = L"Доллары в Рубли";
			this->radioButton1->UseVisualStyleBackColor = true;
			this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
			// 
			// radioButton2
			// 
			this->radioButton2->AutoSize = true;
			this->radioButton2->Location = System::Drawing::Point(6, 47);
			this->radioButton2->Name = L"radioButton2";
			this->radioButton2->Size = System::Drawing::Size(144, 20);
			this->radioButton2->TabIndex = 1;
			this->radioButton2->Text = L" Рубли в Доллары";
			this->radioButton2->UseVisualStyleBackColor = true;
			this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
			// 
			// radioButton4
			// 
			this->radioButton4->AutoSize = true;
			this->radioButton4->Location = System::Drawing::Point(6, 99);
			this->radioButton4->Name = L"radioButton4";
			this->radioButton4->Size = System::Drawing::Size(118, 20);
			this->radioButton4->TabIndex = 3;
			this->radioButton4->Text = L"Рубли в Евро";
			this->radioButton4->UseVisualStyleBackColor = true;
			this->radioButton4->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
			// 
			// radioButton3
			// 
			this->radioButton3->AutoSize = true;
			this->radioButton3->Location = System::Drawing::Point(6, 73);
			this->radioButton3->Name = L"radioButton3";
			this->radioButton3->Size = System::Drawing::Size(118, 20);
			this->radioButton3->TabIndex = 2;
			this->radioButton3->Text = L"Евро в Рубли";
			this->radioButton3->UseVisualStyleBackColor = true;
			this->radioButton3->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(309, 151);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(227, 29);
			this->button1->TabIndex = 3;
			this->button1->Text = L"Конвертировать";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
			// 
			// textBoxValue
			// 
			this->textBoxValue->Location = System::Drawing::Point(170, 27);
			this->textBoxValue->MaxLength = 15;
			this->textBoxValue->Name = L"textBoxValue";
			this->textBoxValue->Size = System::Drawing::Size(114, 22);
			this->textBoxValue->TabIndex = 5;
			this->textBoxValue->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			this->textBoxValue->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
			// 
			// textBoxCurrency
			// 
			this->textBoxCurrency->Location = System::Drawing::Point(170, 55);
			this->textBoxCurrency->MaxLength = 15;
			this->textBoxCurrency->Name = L"textBoxCurrency";
			this->textBoxCurrency->Size = System::Drawing::Size(114, 22);
			this->textBoxCurrency->TabIndex = 6;
			this->textBoxCurrency->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			this->textBoxCurrency->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
			this->textBoxCurrency->Leave += gcnew System::EventHandler(this, &MyForm::textBoxCurrency_Leave);
			// 
			// textBoxUnconvertedValue
			// 
			this->textBoxUnconvertedValue->Location = System::Drawing::Point(6, 21);
			this->textBoxUnconvertedValue->Name = L"textBoxUnconvertedValue";
			this->textBoxUnconvertedValue->ReadOnly = true;
			this->textBoxUnconvertedValue->Size = System::Drawing::Size(82, 22);
			this->textBoxUnconvertedValue->TabIndex = 7;
			this->textBoxUnconvertedValue->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(-1, 30);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(165, 16);
			this->label1->TabIndex = 8;
			this->label1->Text = L"Сумма для конвертации";
			// 
			// labelCurrency
			// 
			this->labelCurrency->AutoSize = true;
			this->labelCurrency->Location = System::Drawing::Point(-1, 58);
			this->labelCurrency->Name = L"labelCurrency";
			this->labelCurrency->Size = System::Drawing::Size(136, 16);
			this->labelCurrency->TabIndex = 9;
			this->labelCurrency->Text = L"Курс обмена (руб/$)";
			// 
			// labelFromCurrency
			// 
			this->labelFromCurrency->AutoSize = true;
			this->labelFromCurrency->Location = System::Drawing::Point(94, 24);
			this->labelFromCurrency->Name = L"labelFromCurrency";
			this->labelFromCurrency->Size = System::Drawing::Size(33, 16);
			this->labelFromCurrency->TabIndex = 10;
			this->labelFromCurrency->Text = L"$    =";
			// 
			// textBoxConvertedValue
			// 
			this->textBoxConvertedValue->Location = System::Drawing::Point(144, 21);
			this->textBoxConvertedValue->Name = L"textBoxConvertedValue";
			this->textBoxConvertedValue->ReadOnly = true;
			this->textBoxConvertedValue->Size = System::Drawing::Size(92, 22);
			this->textBoxConvertedValue->TabIndex = 11;
			this->textBoxConvertedValue->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// labelToCurrency
			// 
			this->labelToCurrency->AutoSize = true;
			this->labelToCurrency->Location = System::Drawing::Point(242, 24);
			this->labelToCurrency->Name = L"labelToCurrency";
			this->labelToCurrency->Size = System::Drawing::Size(31, 16);
			this->labelToCurrency->TabIndex = 12;
			this->labelToCurrency->Text = L"руб";
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->radioButton1);
			this->groupBox1->Controls->Add(this->radioButton3);
			this->groupBox1->Controls->Add(this->radioButton2);
			this->groupBox1->Controls->Add(this->radioButton4);
			this->groupBox1->Location = System::Drawing::Point(309, 8);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(227, 137);
			this->groupBox1->TabIndex = 13;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Направление Конвертации";
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->textBoxUnconvertedValue);
			this->groupBox2->Controls->Add(this->labelFromCurrency);
			this->groupBox2->Controls->Add(this->labelToCurrency);
			this->groupBox2->Controls->Add(this->textBoxConvertedValue);
			this->groupBox2->Location = System::Drawing::Point(14, 107);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(289, 73);
			this->groupBox2->TabIndex = 14;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"результат конвертации";
			// 
			// groupBox3
			// 
			this->groupBox3->Controls->Add(this->textBoxCurrency);
			this->groupBox3->Controls->Add(this->textBoxValue);
			this->groupBox3->Controls->Add(this->label1);
			this->groupBox3->Controls->Add(this->labelCurrency);
			this->groupBox3->Location = System::Drawing::Point(12, 8);
			this->groupBox3->Name = L"groupBox3";
			this->groupBox3->Size = System::Drawing::Size(291, 81);
			this->groupBox3->TabIndex = 15;
			this->groupBox3->TabStop = false;
			this->groupBox3->Text = L"Исходные данные";
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(539, 190);
			this->Controls->Add(this->groupBox3);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->button1);
			this->Cursor = System::Windows::Forms::Cursors::IBeam;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"MyForm";
			this->Text = L"Конвертер валют";
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			this->groupBox3->ResumeLayout(false);
			this->groupBox3->PerformLayout();
			this->ResumeLayout(false);
			  
		}
#pragma endregion
double dol_to_rub = 0;
double rub_to_dol = 0;
double eur_to_rub = 0;
double rub_to_eur = 0;
int currency_in_use = 1;

private: double getNumber(System::Windows::Forms::TextBox^ textbox)	// возвращает число из текстбокса, измен¤ет текстбокс, если в нЄм перед точкой нет чисел
{
	textbox->Text = textbox->Text->Replace(".", ",");
	if (textbox->Text == "")
		textbox->Text = "0";
	if (textbox->Text->IndexOf(",") == 0)
		textbox->Text = textbox->Text->Replace(",", "0,");
	return Convert::ToDouble(textbox->Text);
}

private: System::Void radioButton_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{
	reset_answer();
	double exchange_rate = 0;

	System::Windows::Forms::RadioButton^ radiobutton = safe_cast <System::Windows::Forms::RadioButton^> (sender);
	if (radiobutton->Checked)
	{
		if (radiobutton == radioButton1)
		{
			currency_in_use = 1;
			labelFromCurrency->Text = "$    =";
			labelToCurrency->Text = "руб";
			labelCurrency->Text = "Курс обмена (руб/$)";
			exchange_rate = dol_to_rub;
		}
		else if (radiobutton == radioButton2)
		{
			currency_in_use = 2;
			labelFromCurrency->Text = "руб =";
			labelToCurrency->Text = "$";
			labelCurrency->Text = "Курс обмена (руб/$)";
			exchange_rate = rub_to_dol;
		}
		else if (radiobutton == radioButton3)
		{
			currency_in_use = 3;
			labelFromCurrency->Text = "€    =";
			labelToCurrency->Text = "руб";
			labelCurrency->Text = "Курс обмена (руб/€)";
			exchange_rate = eur_to_rub;
		}
		else if (radiobutton == radioButton4)
		{
			currency_in_use = 4;
			labelFromCurrency->Text = "руб =";
			labelToCurrency->Text = "€";
			labelCurrency->Text = "Курс обмена (руб/€)";
			exchange_rate = rub_to_eur;
		}

		if (exchange_rate <= 0)
			textBoxCurrency->Text = "";
		else
			textBoxCurrency->Text = Convert::ToString(exchange_rate);
	}
}
private: System::Void textBox_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
	reset_answer();
	System::Windows::Forms::TextBox^ textbox = safe_cast <System::Windows::Forms::TextBox^> (sender);
	if ((e->KeyChar >= '0') && (e->KeyChar <= '9')								// ввод цифр
		|| e->KeyChar == 127													// delete
		|| e->KeyChar == 8														// backspace	
		|| ((e->KeyChar == '.' || e->KeyChar == ',') &&										// ввод . или ,
			(textbox->Text->IndexOf(L",") == -1 && textbox->Text->IndexOf(L".") == -1)))	// если в строке уже нет одной зап¤той или точки
		return;
	e->Handled = true;	// иначе не вводить
}

private: System::Void textBoxCurrency_Leave(System::Object^ sender, System::EventArgs^ e)
{
	double exchange_rate = getNumber(textBoxCurrency);
	switch (currency_in_use)
	{
	case 1:
		dol_to_rub = exchange_rate;
		break;
	case 2:
		rub_to_dol = exchange_rate;
		break;
	case 3:
		eur_to_rub = exchange_rate;
		break;
	case 4:
		rub_to_eur = exchange_rate;
		break;
	}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
	double value = double(int(getNumber(textBoxValue)*100))/100;
	textBoxValue->Text = value.ToString("f2");
	double exchange_rate = getNumber(textBoxCurrency);
	if (exchange_rate == 0)
	{
		MessageBox::Show(this, "Введите курс, отличный от 0", "Неправльный курс", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
		return;
	}
	if (currency_in_use == 1 || currency_in_use == 3)
		exchange_rate = 1 / exchange_rate;
	double converted_value = (double(int(value * 100 / exchange_rate)) / 100);
	if (converted_value < 0.01)
	{
		MessageBox::Show(this, "Сумма для конвертации слишком мала", "Невозможный обмен", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
		return;
	}
	textBoxConvertedValue->Text = converted_value.ToString("f2");
	textBoxUnconvertedValue->Text = textBoxValue->Text;
}

private: System::Void reset_answer()
{
	textBoxConvertedValue->Text = "";
	textBoxUnconvertedValue->Text = "";
}
};
}
Соседние файлы в папке Лаба 4 (Конвертер)