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

namespace Pr3 {

	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::TextBox^ textBoxA;
	private: System::Windows::Forms::TextBox^ textBoxB;
	private: System::Windows::Forms::TextBox^ textBoxC;
	private: System::Windows::Forms::TextBox^ textBoxAns1;
	private: System::Windows::Forms::TextBox^ textBoxAns2;
	private: System::Windows::Forms::Button^ buttonCalculate;

	private: System::Windows::Forms::Button^ button2;
	private: System::Windows::Forms::Label^ ErrorLabel;
	private: System::Windows::Forms::Label^ label1;
	private: System::Windows::Forms::Label^ label2;
	private: System::Windows::Forms::Label^ label3;
	private: System::Windows::Forms::Label^ label4;
	private: System::Windows::Forms::Label^ label5;
	private: System::Windows::Forms::GroupBox^ groupBox1;
	private: System::Windows::Forms::GroupBox^ groupBox2;

	protected:



	protected:

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

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Требуемый метод для поддержки конструктора — не изменяйте 
		/// содержимое этого метода с помощью редактора кода.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBoxA = (gcnew System::Windows::Forms::TextBox());
			this->textBoxB = (gcnew System::Windows::Forms::TextBox());
			this->textBoxC = (gcnew System::Windows::Forms::TextBox());
			this->textBoxAns1 = (gcnew System::Windows::Forms::TextBox());
			this->textBoxAns2 = (gcnew System::Windows::Forms::TextBox());
			this->buttonCalculate = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->ErrorLabel = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
			this->groupBox1->SuspendLayout();
			this->groupBox2->SuspendLayout();
			this->SuspendLayout();
			// 
			// textBoxA
			// 
			this->textBoxA->Location = System::Drawing::Point(37, 26);
			this->textBoxA->MaxLength = 10;
			this->textBoxA->Name = L"textBoxA";
			this->textBoxA->ShortcutsEnabled = false;
			this->textBoxA->Size = System::Drawing::Size(100, 22);
			this->textBoxA->TabIndex = 0;
			this->textBoxA->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			this->textBoxA->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
			// 
			// textBoxB
			// 
			this->textBoxB->Location = System::Drawing::Point(37, 54);
			this->textBoxB->MaxLength = 10;
			this->textBoxB->Name = L"textBoxB";
			this->textBoxB->ShortcutsEnabled = false;
			this->textBoxB->Size = System::Drawing::Size(100, 22);
			this->textBoxB->TabIndex = 1;
			this->textBoxB->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			this->textBoxB->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
			// 
			// textBoxC
			// 
			this->textBoxC->Location = System::Drawing::Point(37, 82);
			this->textBoxC->MaxLength = 10;
			this->textBoxC->Name = L"textBoxC";
			this->textBoxC->ShortcutsEnabled = false;
			this->textBoxC->Size = System::Drawing::Size(100, 22);
			this->textBoxC->TabIndex = 2;
			this->textBoxC->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			this->textBoxC->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
			// 
			// textBoxAns1
			// 
			this->textBoxAns1->Location = System::Drawing::Point(40, 21);
			this->textBoxAns1->Name = L"textBoxAns1";
			this->textBoxAns1->ReadOnly = true;
			this->textBoxAns1->Size = System::Drawing::Size(118, 22);
			this->textBoxAns1->TabIndex = 3;
			this->textBoxAns1->TabStop = false;
			this->textBoxAns1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// textBoxAns2
			// 
			this->textBoxAns2->Location = System::Drawing::Point(200, 21);
			this->textBoxAns2->Name = L"textBoxAns2";
			this->textBoxAns2->ReadOnly = true;
			this->textBoxAns2->Size = System::Drawing::Size(118, 22);
			this->textBoxAns2->TabIndex = 4;
			this->textBoxAns2->TabStop = false;
			this->textBoxAns2->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
			// 
			// buttonCalculate
			// 
			this->buttonCalculate->Location = System::Drawing::Point(383, 112);
			this->buttonCalculate->Name = L"buttonCalculate";
			this->buttonCalculate->Size = System::Drawing::Size(158, 33);
			this->buttonCalculate->TabIndex = 6;
			this->buttonCalculate->Text = L"Решить";
			this->buttonCalculate->UseVisualStyleBackColor = true;
			this->buttonCalculate->Click += gcnew System::EventHandler(this, &MyForm::buttonCalculate_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(218, 112);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(158, 33);
			this->button2->TabIndex = 5;
			this->button2->Text = L"Сбросить";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
			// 
			// ErrorLabel
			// 
			this->ErrorLabel->AutoSize = true;
			this->ErrorLabel->Location = System::Drawing::Point(6, 55);
			this->ErrorLabel->Name = L"ErrorLabel";
			this->ErrorLabel->Size = System::Drawing::Size(130, 16);
			this->ErrorLabel->TabIndex = 7;
			this->ErrorLabel->Text = L"                                         ";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(6, 29);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(25, 16);
			this->label1->TabIndex = 8;
			this->label1->Text = L"a =";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(6, 57);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(25, 16);
			this->label2->TabIndex = 9;
			this->label2->Text = L"b =";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(6, 85);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(24, 16);
			this->label3->TabIndex = 10;
			this->label3->Text = L"c =";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(4, 24);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(30, 16);
			this->label4->TabIndex = 11;
			this->label4->Text = L"x1 =";
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(164, 24);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(30, 16);
			this->label5->TabIndex = 12;
			this->label5->Text = L"x2 =";
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->textBoxAns2);
			this->groupBox1->Controls->Add(this->label5);
			this->groupBox1->Controls->Add(this->ErrorLabel);
			this->groupBox1->Controls->Add(this->textBoxAns1);
			this->groupBox1->Controls->Add(this->label4);
			this->groupBox1->Location = System::Drawing::Point(218, 7);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(323, 99);
			this->groupBox1->TabIndex = 13;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = L"Корни уравнения";
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->textBoxB);
			this->groupBox2->Controls->Add(this->textBoxA);
			this->groupBox2->Controls->Add(this->label3);
			this->groupBox2->Controls->Add(this->textBoxC);
			this->groupBox2->Controls->Add(this->label2);
			this->groupBox2->Controls->Add(this->label1);
			this->groupBox2->Location = System::Drawing::Point(12, 7);
			this->groupBox2->Name = L"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(200, 138);
			this->groupBox2->TabIndex = 14;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = L"Коэфиценты уравнения";
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(545, 150);
			this->Controls->Add(this->groupBox2);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->buttonCalculate);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->Name = L"MyForm";
			this->Text = L"Решение квадратного уравнения";
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->groupBox2->ResumeLayout(false);
			this->groupBox2->PerformLayout();
			this->ResumeLayout(false);

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

private: System::Void textBox_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
	reset();
	e->Handled = true;
	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)))	// если в строке уже нет одной зап§той или точки
		e->Handled = false;
	else if (e->KeyChar == '-')
	{
		int selectionStart = textbox->SelectionStart;
		if (textbox->Text->IndexOf("-") != -1)
		{
			textbox->Text = textbox->Text->Replace("-", "");
			textbox->SelectionStart = selectionStart - 1;
			if (textbox->SelectionStart < 0)
				textbox->SelectionStart = 0;
		}
		else
		{
			textbox->Text = "-" + textbox->Text;
			textbox->SelectionStart = selectionStart + 1;
		}
	}
};
private: System::Void buttonCalculate_Click(System::Object^ sender, System::EventArgs^ e)
{
	reset();
	double a = getNumber(textBoxA);
	double b = getNumber(textBoxB);
	double c = getNumber(textBoxC);
	if (a == 0 && b == 0)
	{
		if (c==0)
			ErrorLabel->Text = "0 = 0 - верное равенство.";
		else
			ErrorLabel->Text = c.ToString() + " = 0 - неверное равенство.";
	}
	else if (a == 0)
	{
		textBoxAns1->Text = (-c / b).ToString();
		ErrorLabel->Text = "Линейное уравнение – имеет один корень";
	}
	else if (b == 0)
	{
		if (-c * a < 0)
		{
			ErrorLabel->Text = "Неполное квадратное уравнение.\nДействительных корней нет.";
		}
		else
		{
			double x1 = Math::Sqrt(-c / a);
			textBoxAns1->Text = (-x1).ToString();
			textBoxAns2->Text = x1.ToString();
			if (x1 == 0)
				ErrorLabel->Text = "Неполное квадратное уравнение.\nУравнение имеет 2 одинаковых корня.";
			else
				ErrorLabel->Text = "Неполное квадратное уравнение.\nУравнение имеет два корня.";
		}
	}
	else
	{
		double d = b * b - 4 * a * c;
		if (d == 0)
		{
			textBoxAns1->Text = ((-b - Math::Sqrt(d)) / (2 * a)).ToString();
			textBoxAns2->Text = textBoxAns1->Text;
			ErrorLabel->Text = "Дискриминант = 0.\nУравнение имеет два одинаковых корня.";
		}
		else if (d>0)
		{
			textBoxAns1->Text = ((-b - Math::Sqrt(d)) / (2 * a)).ToString();
			textBoxAns2->Text = ((-b + Math::Sqrt(d)) / (2 * a)).ToString();
			ErrorLabel->Text = "Дискриминант > 0.\nУравнение имеет два корня.";
		}
		else
			ErrorLabel->Text = "Дискриминант < 0.\nУравнение не имеет действительных корней.";
	}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
	reset();
	textBoxA->Text = "";
	textBoxB->Text = "";
	textBoxC->Text = "";
}
};
}
Соседние файлы в папке Лаба 3 (Уравнения)