Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабы Волков / Отчёт квадратное уравнение.docx
Скачиваний:
1
Добавлен:
09.07.2025
Размер:
436.27 Кб
Скачать

2 Схемы алгоритмов

При нажатии на кнопку «Закрыть», выполняется функция закрытия программы (рисунок 1).

Рисунок 1 – Схема алгоритма функции обработки события нажатия кнопки «Закрыть»

При нажатии на кнопку «Сброс», выполняется функция, представленная на рисунке 2.

Рисунок 2 – Схема алгоритма функции обработки события нажатия кнопки «Сброс»

При нажатии на кнопку «Решить», выполняется алгоритм, представленный на рисунке 3.

Рисунок 3 – Схема алгоритма функции обработки события нажатия кнопки «Решить»

3 Текст программы

#pragma endregion

private: System::Void btnSolve_Click(System::Object^ sender, System::EventArgs^ e)

{

double a, b, c, x1, x2, D;

textBoxX1->Text = L"";

textBoxX2->Text = L"";

if ((textBoxA->Text != "")&&(textBoxB->Text != "")&&(textBoxC->Text != ""))

{

a = System::Convert::ToDouble(textBoxA->Text);

b = System::Convert::ToDouble(textBoxB->Text);

c = System::Convert::ToDouble(textBoxC->Text);

this->lblError->Text = L"";

if (a != 0)

{

D = b * b - 4 * a * c;

if (sqrt(D) > 0)

{

x1 = (-b + sqrt(D)) / (2 * a);

x2 = (-b - sqrt(D)) / (2 * a);

this->lblComment->Text = L"Дискриминант > 0. Уравнение\nимеет два корня.";

textBoxX1->Text = System::Convert::ToString(x1);

textBoxX2->Text = System::Convert::ToString(x2);

}

if (sqrt(D) == 0)

{

x1 = (-b / (2 * a));

x2 = (-b / (2 * a));

this->lblComment->Text = L"Дискриминант = 0. Уравнение\nимеет два одинаковых корня.";

textBoxX1->Text = System::Convert::ToString(x1);

textBoxX2->Text = System::Convert::ToString(x2);

}

if (D < 0)

{

this->lblComment->Text = L"Дискриминант < 0. Уравнение\nне имеет действительных корней.";

}

}

if ((a == 0) && (b != 0) && (c != 0)) {

x1 = (-c / b);

textBoxX1->Text = System::Convert::ToString(x1);

this->lblComment->Text = L"Линейное уравнение -\nимеет один корень";

}

if ((a == 0) && (b == 0) && (c != 0))

{

this->lblComment->Text = "0 = " + c*(-1) + ". Ложное тождество!";

}

if ((a == 0) && (b == 0) && (c == 0))

{

this->lblComment->Text = L"0 = 0. Верное тождество.";

}

}

else if (textBoxA->Text == "")

{

lblError->Text = L"";

this->lblError->Text = L"Введите коэффициент а.";

}

else if (textBoxB->Text == "")

{

lblError->Text = L"";

this->lblError->Text = L"Введите коэффициент b.";

}

else if (textBoxC->Text == "")

{

lblError->Text = L"";

this->lblError->Text = L"Введите коэффициент c.";

}

}

private: System::Void btnReset_Click(System::Object^ sender, System::EventArgs^ e)

{

this->lblComment->Text = L"";

textBoxA->Text = L"";

textBoxB->Text = L"";

textBoxC->Text = L"";

textBoxX1->Text = L"";

textBoxX2->Text = L"";

lblError->Text = L"";

}

private: System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e)

{

this->Close();

}

private: System::Void textBoxA_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)

{

//Ввод только цифр

char number = e->KeyChar;

if (!Char::IsDigit(number) && number != 8 && number != 44 && number != 45)

{e->Handled = true; }

if (number == 44) { if (textBoxA->Text->IndexOf(44) != -1) { e->Handled = true; }

else { e->Handled = false; } }

if (number == 45) { if (textBoxA->Text->IndexOf(45) != -1) { e->Handled = true; }

else { e->Handled = false; } }

}

private: System::Void textBoxA_TextChanged(System::Object^ sender, System::EventArgs^ e) {

lblComment->Text = L"";

lblError->Text = L"";

textBoxX1->Text = L"";

textBoxX2->Text = L"";

//Допустима только одна запятая

if (this->textBoxA->Text->Length > 0 && this->textBoxA->Text[0] == ',') {

this->textBoxA->Text = "0" + this->textBoxA->Text;

textBoxA->SelectionStart = textBoxA->Text->Length;

}

//Минус допустим лишь в начале

if (this->textBoxA->Text->Length > 0) {

String^ s1;

s1 = textBoxA->Text->Substring(textBoxA->Text->Length - 1, 1);

if (s1 == "-") {

textBoxA->Text = "-" + textBoxA->Text->Remove(textBoxA->Text- >Length - 1);

textBoxA->SelectionStart = textBoxA->Text->Length;

}

}

}

private: System::Void textBoxB_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)

{

//Ввод только цифр

char number = e->KeyChar;

if (!Char::IsDigit(number) && number != 8 && number != 44 && number != 45) { e->Handled = true; }

if (number == 44) { if (textBoxB->Text->IndexOf(44) != -1) { e->Handled = true; }

else { e->Handled = false; } }

if (number == 45) { if (textBoxB->Text->IndexOf(45) != -1) { e->Handled = true; }

else { e->Handled = false; } }

}

private: System::Void textBoxB_TextChanged(System::Object^ sender, System::EventArgs^ e) {

lblComment->Text = L"";

lblError->Text = L"";

textBoxX1->Text = L"";

textBoxX2->Text = L"";

//Допустима только одна запятая

if (this->textBoxB->Text->Length > 0 && this->textBoxB->Text[0] == ',') {

this->textBoxB->Text = "0" + this->textBoxB->Text;

textBoxB->SelectionStart = textBoxB->Text->Length;

}

//Минус допустим лишь в начале

if (this->textBoxB->Text->Length > 0) {

String^ s1;

s1 = textBoxB->Text->Substring(textBoxB->Text->Length - 1, 1);

if (s1 == "-") {

textBoxB->Text = "-" + textBoxB->Text->Remove(textBoxB->Text- >Length - 1);

textBoxB->SelectionStart = textBoxB->Text->Length;

}

}

}

private: System::Void textBoxC_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)

{

//Ввод только цифр

char number = e->KeyChar;

if (!Char::IsDigit(number) && number != 8 && number != 44 && number != 45) { e->Handled = true; }

if (number == 44) { if (textBoxC->Text->IndexOf(44) != -1) { e->Handled = true; }

else { e->Handled = false; } }

if (number == 45) { if (textBoxC->Text->IndexOf(45) != -1) { e->Handled = true; }

else { e->Handled = false; } }

}

private: System::Void textBoxC_TextChanged(System::Object^ sender, System::EventArgs^ e) {

lblComment->Text = L"";

lblError->Text = L"";

textBoxX1->Text = L"";

textBoxX2->Text = L"";

//Допуск лишь одной запятой

if (this->textBoxC->Text->Length > 0 && this->textBoxC->Text[0] == ',') {

this->textBoxC->Text = "0" + this->textBoxC->Text;

textBoxB->SelectionStart = textBoxB->Text->Length;

}

//Минус допустим лишь в начале

if (this->textBoxC->Text->Length > 0) {

String^ s1;

s1 = textBoxC->Text->Substring(textBoxC->Text->Length - 1, 1);

if (s1 == "-") {

textBoxC->Text = "-" + textBoxC->Text->Remove(textBoxC->Text- >Length - 1);

textBoxC->SelectionStart = textBoxC->Text->Length;

}

}

}

};

}