Тема: Допоможіть з типом даних (С++ Windows Forms)
Я написав програму, яка обчислює дані за Теоремою Піфагора. Мені потрібно щоб у пермінну a i b можна було б вводити не тільки числа, а й символьні значення. Наприклад, присвоїти перемінній a значення sqrt(25) чи pow(5,2). Але тип даних double цього не підтримує. Допоможіть трохи змінити код. Ось вихідний код.
#include <cmath>
#pragma once
namespace ТеоремаПіфагора {
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>
/// Сводка для Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RadioButton^ radioButton1;
protected:
private: System::Windows::Forms::RadioButton^ radioButton2;
private: System::Windows::Forms::GroupBox^ groupBox1;
private: System::Windows::Forms::GroupBox^ groupBox2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label1;
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label3 = (gcnew System::Windows::Forms::Label());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->SuspendLayout();
//
// radioButton1
//
this->radioButton1->AutoSize = true;
this->radioButton1->Location = System::Drawing::Point(6, 19);
this->radioButton1->Name = L"radioButton1";
this->radioButton1->Size = System::Drawing::Size(258, 17);
this->radioButton1->TabIndex = 0;
this->radioButton1->Text = L"Відомі два катети. Потрібно знайти гіпотенузу";
this->radioButton1->UseVisualStyleBackColor = true;
this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton1_CheckedChanged);
//
// radioButton2
//
this->radioButton2->AutoSize = true;
this->radioButton2->Location = System::Drawing::Point(6, 42);
this->radioButton2->Name = L"radioButton2";
this->radioButton2->Size = System::Drawing::Size(350, 17);
this->radioButton2->TabIndex = 1;
this->radioButton2->Text = L"Відомі один катет і гіпотенуза. Потрібно знайти невідомий катет";
this->radioButton2->UseVisualStyleBackColor = true;
this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &Form1::radioButton2_CheckedChanged);
//
// groupBox1
//
this->groupBox1->Controls->Add(this->radioButton1);
this->groupBox1->Controls->Add(this->radioButton2);
this->groupBox1->Location = System::Drawing::Point(12, 12);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(365, 69);
this->groupBox1->TabIndex = 2;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Оберіть потрібний випадок";
//
// groupBox2
//
this->groupBox2->Controls->Add(this->button1);
this->groupBox2->Controls->Add(this->label3);
this->groupBox2->Controls->Add(this->textBox2);
this->groupBox2->Controls->Add(this->label2);
this->groupBox2->Controls->Add(this->textBox1);
this->groupBox2->Controls->Add(this->label1);
this->groupBox2->Location = System::Drawing::Point(12, 98);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size = System::Drawing::Size(365, 127);
this->groupBox2->TabIndex = 3;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"Розв\'язання";
//
// button1
//
this->button1->Location = System::Drawing::Point(268, 25);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(91, 43);
this->button1->TabIndex = 5;
this->button1->Text = L"Обчислити";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->label3->Location = System::Drawing::Point(6, 74);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(35, 13);
this->label3->TabIndex = 4;
this->label3->Text = L"label3";
this->label3->Visible = false;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(129, 48);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(133, 20);
this->textBox2->TabIndex = 3;
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(6, 51);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(113, 13);
this->label2->TabIndex = 2;
this->label2->Text = L"Введіть другий катет";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(129, 25);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(133, 20);
this->textBox1->TabIndex = 1;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(6, 28);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(117, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Введіть перший катет";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
this->ClientSize = System::Drawing::Size(392, 237);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->MaximizeBox = false;
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Теорема Піфагора";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
double a = Convert::ToDouble(textBox1->Text);
double b = Convert::ToDouble(textBox2->Text);
double c;
if (radioButton1 -> Checked == true) {
c = sqrt( pow(a,2) + pow(b,2));
label3->Text = String::Format("Гіпотенуза дорівнює " + c);
label3->Visible = true;
}
if (radioButton2->Checked == true) {
c = sqrt( pow(b,2) - pow(a,2));
label3->Text = String::Format("Невідомий катет дорівнює " + c);
label3->Visible = true;
}
}
private: System::Void radioButton1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "Введіть перший катет";
label2->Text = "Введіть другий катет";
label3->Visible = false;
}
private: System::Void radioButton2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "Введіть відомий катет";
label2->Text = "Введіть гіпотенузу";
textBox1->Text = "";
textBox2->Text = "";
label3->Visible = false;
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
radioButton1->Checked = true;
}
};
}
Ось скріни програми: