
Добавил:
eipimru
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лаба 5 (Экспонента) / MyForm
.h#pragma once
namespace Pr5 {
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::GroupBox^ groupBox1;
protected:
private: System::Windows::Forms::GroupBox^ groupBox2;
private: System::Windows::Forms::RadioButton^ radioButton6;
private: System::Windows::Forms::RadioButton^ radioButton5;
private: System::Windows::Forms::RadioButton^ radioButton4;
private: System::Windows::Forms::RadioButton^ radioButton3;
private: System::Windows::Forms::RadioButton^ radioButton2;
private: System::Windows::Forms::RadioButton^ radioButton1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button1;
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::Label^ label6;
private: System::Windows::Forms::Label^ label_x;
private: System::Windows::Forms::Label^ label_eps;
private: System::Windows::Forms::Label^ label_exp;
private: System::Windows::Forms::Label^ label_n;
private: System::Windows::Forms::Label^ label_exp_sum;
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
this->radioButton6 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton5 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
this->button1 = (gcnew System::Windows::Forms::Button());
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->label6 = (gcnew System::Windows::Forms::Label());
this->label_x = (gcnew System::Windows::Forms::Label());
this->label_eps = (gcnew System::Windows::Forms::Label());
this->label_exp = (gcnew System::Windows::Forms::Label());
this->label_n = (gcnew System::Windows::Forms::Label());
this->label_exp_sum = (gcnew System::Windows::Forms::Label());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->SuspendLayout();
//
// groupBox1
//
this->groupBox1->Controls->Add(this->textBox1);
this->groupBox1->Location = System::Drawing::Point(12, 32);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(321, 57);
this->groupBox1->TabIndex = 0;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Ввод Х (-13<= x<=20)";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(6, 21);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(309, 22);
this->textBox1->TabIndex = 0;
this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
//
// groupBox2
//
this->groupBox2->Controls->Add(this->radioButton6);
this->groupBox2->Controls->Add(this->radioButton5);
this->groupBox2->Controls->Add(this->radioButton4);
this->groupBox2->Controls->Add(this->radioButton3);
this->groupBox2->Controls->Add(this->radioButton2);
this->groupBox2->Controls->Add(this->radioButton1);
this->groupBox2->Location = System::Drawing::Point(12, 95);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size = System::Drawing::Size(321, 80);
this->groupBox2->TabIndex = 1;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"Выбор точности";
//
// radioButton6
//
this->radioButton6->AutoSize = true;
this->radioButton6->Location = System::Drawing::Point(171, 47);
this->radioButton6->Name = L"radioButton6";
this->radioButton6->Size = System::Drawing::Size(80, 20);
this->radioButton6->TabIndex = 5;
this->radioButton6->Text = L"0,000001";
this->radioButton6->UseVisualStyleBackColor = true;
this->radioButton6->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton5
//
this->radioButton5->AutoSize = true;
this->radioButton5->Location = System::Drawing::Point(171, 21);
this->radioButton5->Name = L"radioButton5";
this->radioButton5->Size = System::Drawing::Size(73, 20);
this->radioButton5->TabIndex = 4;
this->radioButton5->Text = L"0,00001";
this->radioButton5->UseVisualStyleBackColor = true;
this->radioButton5->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// radioButton4
//
this->radioButton4->AutoSize = true;
this->radioButton4->Location = System::Drawing::Point(80, 47);
this->radioButton4->Name = L"radioButton4";
this->radioButton4->Size = System::Drawing::Size(66, 20);
this->radioButton4->TabIndex = 3;
this->radioButton4->Text = L"0,0001";
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(80, 21);
this->radioButton3->Name = L"radioButton3";
this->radioButton3->Size = System::Drawing::Size(59, 20);
this->radioButton3->TabIndex = 2;
this->radioButton3->Text = L"0,001";
this->radioButton3->UseVisualStyleBackColor = true;
this->radioButton3->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(52, 20);
this->radioButton2->TabIndex = 1;
this->radioButton2->Text = L"0,01";
this->radioButton2->UseVisualStyleBackColor = true;
this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// 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(45, 20);
this->radioButton1->TabIndex = 0;
this->radioButton1->TabStop = true;
this->radioButton1->Text = L"0,1";
this->radioButton1->UseVisualStyleBackColor = true;
this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton_CheckedChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 181);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(321, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"Рассчёт exp(x)";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->label1->Location = System::Drawing::Point(67, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(221, 20);
this->label1->TabIndex = 3;
this->label1->Text = L"exp(x) = 1 + x/1 + x^2/2! + ...";
this->label1->TextAlign = System::Drawing::ContentAlignment::TopCenter;
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(9, 207);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(144, 16);
this->label2->TabIndex = 4;
this->label2->Text = L"Исходное значение=";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(9, 232);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(76, 16);
this->label3->TabIndex = 5;
this->label3->Text = L"Точность=";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(9, 257);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(169, 16);
this->label4->TabIndex = 6;
this->label4->Text = L"Точное значение exp(x)=";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(9, 282);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(132, 16);
this->label5->TabIndex = 7;
this->label5->Text = L"Число слагаемых = ";
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(9, 307);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(130, 16);
this->label6->TabIndex = 8;
this->label6->Text = L"Сумма ряда exp(x)=";
//
// label_x
//
this->label_x->AutoSize = true;
this->label_x->Location = System::Drawing::Point(204, 207);
this->label_x->Name = L"label_x";
this->label_x->Size = System::Drawing::Size(82, 16);
this->label_x->TabIndex = 9;
this->label_x->Text = L" ";
//
// label_eps
//
this->label_eps->AutoSize = true;
this->label_eps->Location = System::Drawing::Point(204, 232);
this->label_eps->Name = L"label_eps";
this->label_eps->Size = System::Drawing::Size(82, 16);
this->label_eps->TabIndex = 10;
this->label_eps->Text = L" ";
//
// label_exp
//
this->label_exp->AutoSize = true;
this->label_exp->Location = System::Drawing::Point(204, 257);
this->label_exp->Name = L"label_exp";
this->label_exp->Size = System::Drawing::Size(82, 16);
this->label_exp->TabIndex = 11;
this->label_exp->Text = L" ";
//
// label_n
//
this->label_n->AutoSize = true;
this->label_n->Location = System::Drawing::Point(204, 282);
this->label_n->Name = L"label_n";
this->label_n->Size = System::Drawing::Size(82, 16);
this->label_n->TabIndex = 12;
this->label_n->Text = L" ";
//
// label_exp_sum
//
this->label_exp_sum->AutoSize = true;
this->label_exp_sum->Location = System::Drawing::Point(204, 307);
this->label_exp_sum->Name = L"label_exp_sum";
this->label_exp_sum->Size = System::Drawing::Size(82, 16);
this->label_exp_sum->TabIndex = 13;
this->label_exp_sum->Text = L" ";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(355, 331);
this->Controls->Add(this->label_exp_sum);
this->Controls->Add(this->label_n);
this->Controls->Add(this->label_exp);
this->Controls->Add(this->label_eps);
this->Controls->Add(this->label_x);
this->Controls->Add(this->label6);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
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);
this->PerformLayout();
}
#pragma endregion
double eps = 0.1;
private: System::Void reset()
{
label_x->Text = "";
label_n->Text = "";
label_eps->Text = "";
label_exp_sum->Text = "";
label_exp->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: double getNumber(System::Windows::Forms::TextBox^ textbox) // возвращает число из текстбокса, измен¤ет текстбокс, если в нЄм перед точкой нет чисел
{
textbox->Text = textbox->Text->Replace(".", ",");
if (textbox->Text == "" || textbox->Text == "," || textbox->Text == "-" || textbox->Text == "-,")
{
//textbox->Text = "0";
textbox->Text = "";
return 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)
{
System::Windows::Forms::RadioButton^ radiobutton = safe_cast <System::Windows::Forms::RadioButton^> (sender);
if (radiobutton->Checked)
{
eps = Convert::ToDouble(radiobutton->Text);
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
double x = getNumber(textBox1);
if (-13 > x || x > 20)
{
MessageBox::Show(this, "Число не входит в указанный диапозон (-13<=x<=20)", "Экспонента", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
if (textBox1->Text == "")
{
MessageBox::Show(this, "Введите число", "Экспонента", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
return;
}
while (true)
{
double exp_ans = 0;
double term = 1;
double n = 0;
while (Math::Abs(term) >= eps)
{
exp_ans += term;
term = term * x / ++n;
}
exp_ans += term;
n++;
if (exp_ans < eps && !radioButton6->Checked)
change_eps(eps * 0.1);
else
{
label_x->Text = x.ToString();
label_n->Text = n.ToString();
label_eps->Text = eps.ToString("0.######");
System::String^ accuracy = "f" + (label_eps->Text->Length - 1).ToString();
label_exp_sum->Text = exp_ans.ToString(accuracy);
label_exp->Text = Math::Exp(x).ToString(accuracy);
return;
}
}
}
private: System::Void change_eps(double accuracy)
{
int c = accuracy.ToString("0.######")->Length - 2;
switch (c)
{
case 1:
radioButton1->Checked = true;
break;
case 2:
radioButton2->Checked = true;
break;
case 3:
radioButton3->Checked = true;
break;
case 4:
radioButton4->Checked = true;
break;
case 5:
radioButton5->Checked = true;
break;
default:
radioButton6->Checked = true;
break;
}
}
};
}
Соседние файлы в папке Лаба 5 (Экспонента)