
Добавил:
eipimru
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лаба 2 (Сложный калькулятор) / MyForm
.h#pragma once
namespace Pr2 {
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^ textbox;
private: System::Windows::Forms::Button^ Backspace;
private: System::Windows::Forms::Button^ CE;
private: System::Windows::Forms::Button^ C;
protected:
protected:
private: System::Windows::Forms::Panel^ numberPanel;
private: System::Windows::Forms::Button^ NumberButton_7;
private: System::Windows::Forms::Button^ NegativeButton;
private: System::Windows::Forms::Button^ comma;
private: System::Windows::Forms::Button^ NumberButton_0;
private: System::Windows::Forms::Button^ NumberButton_3;
private: System::Windows::Forms::Button^ NumberButton_2;
private: System::Windows::Forms::Button^ NumberButton_1;
private: System::Windows::Forms::Button^ NumberButton_6;
private: System::Windows::Forms::Button^ NumberButton_5;
private: System::Windows::Forms::Button^ NumberButton_4;
private: System::Windows::Forms::Button^ NumberButton_9;
private: System::Windows::Forms::Button^ NumberButton_8;
private: System::Windows::Forms::Panel^ panel1;
private: System::Windows::Forms::Button^ OperationButton_divide;
private: System::Windows::Forms::Button^ OperationButton_equal;
private: System::Windows::Forms::Button^ OperationButton_reciprocal;
private: System::Windows::Forms::Button^ OperationButton_percent;
private: System::Windows::Forms::Button^ OperationButton_sqrt;
private: System::Windows::Forms::Button^ OperationButton_Add;
private: System::Windows::Forms::Button^ OperationButton_subtract;
private: System::Windows::Forms::Button^ OperationButton_multiply;
private: System::Windows::Forms::Panel^ panel2;
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->textbox = (gcnew System::Windows::Forms::TextBox());
this->Backspace = (gcnew System::Windows::Forms::Button());
this->CE = (gcnew System::Windows::Forms::Button());
this->C = (gcnew System::Windows::Forms::Button());
this->numberPanel = (gcnew System::Windows::Forms::Panel());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->OperationButton_equal = (gcnew System::Windows::Forms::Button());
this->OperationButton_reciprocal = (gcnew System::Windows::Forms::Button());
this->OperationButton_percent = (gcnew System::Windows::Forms::Button());
this->OperationButton_sqrt = (gcnew System::Windows::Forms::Button());
this->OperationButton_Add = (gcnew System::Windows::Forms::Button());
this->OperationButton_subtract = (gcnew System::Windows::Forms::Button());
this->OperationButton_multiply = (gcnew System::Windows::Forms::Button());
this->OperationButton_divide = (gcnew System::Windows::Forms::Button());
this->NegativeButton = (gcnew System::Windows::Forms::Button());
this->comma = (gcnew System::Windows::Forms::Button());
this->NumberButton_0 = (gcnew System::Windows::Forms::Button());
this->NumberButton_3 = (gcnew System::Windows::Forms::Button());
this->NumberButton_2 = (gcnew System::Windows::Forms::Button());
this->NumberButton_1 = (gcnew System::Windows::Forms::Button());
this->NumberButton_6 = (gcnew System::Windows::Forms::Button());
this->NumberButton_5 = (gcnew System::Windows::Forms::Button());
this->NumberButton_4 = (gcnew System::Windows::Forms::Button());
this->NumberButton_9 = (gcnew System::Windows::Forms::Button());
this->NumberButton_8 = (gcnew System::Windows::Forms::Button());
this->NumberButton_7 = (gcnew System::Windows::Forms::Button());
this->panel2 = (gcnew System::Windows::Forms::Panel());
this->numberPanel->SuspendLayout();
this->panel1->SuspendLayout();
this->panel2->SuspendLayout();
this->SuspendLayout();
//
// textbox
//
this->textbox->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->textbox->Location = System::Drawing::Point(3, 3);
this->textbox->MaxLength = 25;
this->textbox->Name = L"textbox";
this->textbox->ReadOnly = true;
this->textbox->Size = System::Drawing::Size(323, 30);
this->textbox->TabIndex = 0;
this->textbox->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
//
// Backspace
//
this->Backspace->Location = System::Drawing::Point(3, 39);
this->Backspace->Name = L"Backspace";
this->Backspace->Size = System::Drawing::Size(121, 34);
this->Backspace->TabIndex = 1;
this->Backspace->Text = L"Backspace";
this->Backspace->UseVisualStyleBackColor = true;
this->Backspace->Click += gcnew System::EventHandler(this, &MyForm::Backspace_Click);
//
// CE
//
this->CE->Location = System::Drawing::Point(130, 39);
this->CE->Name = L"CE";
this->CE->Size = System::Drawing::Size(116, 34);
this->CE->TabIndex = 2;
this->CE->Text = L"CE";
this->CE->UseVisualStyleBackColor = true;
this->CE->Click += gcnew System::EventHandler(this, &MyForm::CE_Click);
//
// C
//
this->C->Location = System::Drawing::Point(252, 39);
this->C->Name = L"C";
this->C->Size = System::Drawing::Size(74, 34);
this->C->TabIndex = 3;
this->C->Text = L"C";
this->C->UseVisualStyleBackColor = true;
this->C->Click += gcnew System::EventHandler(this, &MyForm::C_Click);
//
// numberPanel
//
this->numberPanel->Controls->Add(this->panel1);
this->numberPanel->Controls->Add(this->NegativeButton);
this->numberPanel->Controls->Add(this->comma);
this->numberPanel->Controls->Add(this->NumberButton_0);
this->numberPanel->Controls->Add(this->NumberButton_3);
this->numberPanel->Controls->Add(this->NumberButton_2);
this->numberPanel->Controls->Add(this->NumberButton_1);
this->numberPanel->Controls->Add(this->NumberButton_6);
this->numberPanel->Controls->Add(this->NumberButton_5);
this->numberPanel->Controls->Add(this->NumberButton_4);
this->numberPanel->Controls->Add(this->NumberButton_9);
this->numberPanel->Controls->Add(this->NumberButton_8);
this->numberPanel->Controls->Add(this->NumberButton_7);
this->numberPanel->Location = System::Drawing::Point(3, 79);
this->numberPanel->Name = L"numberPanel";
this->numberPanel->Size = System::Drawing::Size(329, 256);
this->numberPanel->TabIndex = 4;
//
// panel1
//
this->panel1->Controls->Add(this->OperationButton_equal);
this->panel1->Controls->Add(this->OperationButton_reciprocal);
this->panel1->Controls->Add(this->OperationButton_percent);
this->panel1->Controls->Add(this->OperationButton_sqrt);
this->panel1->Controls->Add(this->OperationButton_Add);
this->panel1->Controls->Add(this->OperationButton_subtract);
this->panel1->Controls->Add(this->OperationButton_multiply);
this->panel1->Controls->Add(this->OperationButton_divide);
this->panel1->Location = System::Drawing::Point(202, 0);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(127, 256);
this->panel1->TabIndex = 13;
//
// OperationButton_equal
//
this->OperationButton_equal->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_equal->Location = System::Drawing::Point(65, 189);
this->OperationButton_equal->Name = L"OperationButton_equal";
this->OperationButton_equal->Size = System::Drawing::Size(56, 56);
this->OperationButton_equal->TabIndex = 19;
this->OperationButton_equal->Text = L"=";
this->OperationButton_equal->UseVisualStyleBackColor = true;
this->OperationButton_equal->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_reciprocal
//
this->OperationButton_reciprocal->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_reciprocal->Location = System::Drawing::Point(65, 127);
this->OperationButton_reciprocal->Name = L"OperationButton_reciprocal";
this->OperationButton_reciprocal->Size = System::Drawing::Size(56, 56);
this->OperationButton_reciprocal->TabIndex = 18;
this->OperationButton_reciprocal->Text = L"1/x";
this->OperationButton_reciprocal->UseVisualStyleBackColor = true;
this->OperationButton_reciprocal->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_percent
//
this->OperationButton_percent->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_percent->Location = System::Drawing::Point(65, 65);
this->OperationButton_percent->Name = L"OperationButton_percent";
this->OperationButton_percent->Size = System::Drawing::Size(56, 56);
this->OperationButton_percent->TabIndex = 17;
this->OperationButton_percent->Text = L"%";
this->OperationButton_percent->UseVisualStyleBackColor = true;
this->OperationButton_percent->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_sqrt
//
this->OperationButton_sqrt->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_sqrt->Location = System::Drawing::Point(65, 3);
this->OperationButton_sqrt->Name = L"OperationButton_sqrt";
this->OperationButton_sqrt->Size = System::Drawing::Size(56, 56);
this->OperationButton_sqrt->TabIndex = 16;
this->OperationButton_sqrt->Text = L"sqrt";
this->OperationButton_sqrt->UseVisualStyleBackColor = true;
this->OperationButton_sqrt->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_Add
//
this->OperationButton_Add->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_Add->Location = System::Drawing::Point(3, 189);
this->OperationButton_Add->Name = L"OperationButton_Add";
this->OperationButton_Add->Size = System::Drawing::Size(56, 56);
this->OperationButton_Add->TabIndex = 15;
this->OperationButton_Add->Text = L"+";
this->OperationButton_Add->UseVisualStyleBackColor = true;
this->OperationButton_Add->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_subtract
//
this->OperationButton_subtract->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_subtract->Location = System::Drawing::Point(3, 127);
this->OperationButton_subtract->Name = L"OperationButton_subtract";
this->OperationButton_subtract->Size = System::Drawing::Size(56, 56);
this->OperationButton_subtract->TabIndex = 14;
this->OperationButton_subtract->Text = L"-";
this->OperationButton_subtract->UseVisualStyleBackColor = true;
this->OperationButton_subtract->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_multiply
//
this->OperationButton_multiply->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_multiply->Location = System::Drawing::Point(3, 65);
this->OperationButton_multiply->Name = L"OperationButton_multiply";
this->OperationButton_multiply->Size = System::Drawing::Size(56, 56);
this->OperationButton_multiply->TabIndex = 13;
this->OperationButton_multiply->Text = L"*";
this->OperationButton_multiply->UseVisualStyleBackColor = true;
this->OperationButton_multiply->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// OperationButton_divide
//
this->OperationButton_divide->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular,
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
this->OperationButton_divide->Location = System::Drawing::Point(3, 3);
this->OperationButton_divide->Name = L"OperationButton_divide";
this->OperationButton_divide->Size = System::Drawing::Size(56, 56);
this->OperationButton_divide->TabIndex = 12;
this->OperationButton_divide->Text = L"/";
this->OperationButton_divide->UseVisualStyleBackColor = true;
this->OperationButton_divide->Click += gcnew System::EventHandler(this, &MyForm::OperationButton_Click);
//
// NegativeButton
//
this->NegativeButton->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NegativeButton->Location = System::Drawing::Point(127, 189);
this->NegativeButton->Name = L"NegativeButton";
this->NegativeButton->Size = System::Drawing::Size(56, 56);
this->NegativeButton->TabIndex = 11;
this->NegativeButton->Text = L"+/-";
this->NegativeButton->UseVisualStyleBackColor = true;
this->NegativeButton->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// comma
//
this->comma->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->comma->Location = System::Drawing::Point(65, 189);
this->comma->Name = L"comma";
this->comma->Size = System::Drawing::Size(56, 56);
this->comma->TabIndex = 10;
this->comma->Text = L",";
this->comma->UseVisualStyleBackColor = true;
this->comma->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_0
//
this->NumberButton_0->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_0->Location = System::Drawing::Point(3, 189);
this->NumberButton_0->Name = L"NumberButton_0";
this->NumberButton_0->Size = System::Drawing::Size(56, 56);
this->NumberButton_0->TabIndex = 9;
this->NumberButton_0->Text = L"0";
this->NumberButton_0->UseVisualStyleBackColor = true;
this->NumberButton_0->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_3
//
this->NumberButton_3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_3->Location = System::Drawing::Point(127, 127);
this->NumberButton_3->Name = L"NumberButton_3";
this->NumberButton_3->Size = System::Drawing::Size(56, 56);
this->NumberButton_3->TabIndex = 8;
this->NumberButton_3->Text = L"3";
this->NumberButton_3->UseVisualStyleBackColor = true;
this->NumberButton_3->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_2
//
this->NumberButton_2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_2->Location = System::Drawing::Point(65, 127);
this->NumberButton_2->Name = L"NumberButton_2";
this->NumberButton_2->Size = System::Drawing::Size(56, 56);
this->NumberButton_2->TabIndex = 7;
this->NumberButton_2->Text = L"2";
this->NumberButton_2->UseVisualStyleBackColor = true;
this->NumberButton_2->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_1
//
this->NumberButton_1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_1->Location = System::Drawing::Point(3, 127);
this->NumberButton_1->Name = L"NumberButton_1";
this->NumberButton_1->Size = System::Drawing::Size(56, 56);
this->NumberButton_1->TabIndex = 6;
this->NumberButton_1->Text = L"1";
this->NumberButton_1->UseVisualStyleBackColor = true;
this->NumberButton_1->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_6
//
this->NumberButton_6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_6->Location = System::Drawing::Point(127, 65);
this->NumberButton_6->Name = L"NumberButton_6";
this->NumberButton_6->Size = System::Drawing::Size(56, 56);
this->NumberButton_6->TabIndex = 5;
this->NumberButton_6->Text = L"6";
this->NumberButton_6->UseVisualStyleBackColor = true;
this->NumberButton_6->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_5
//
this->NumberButton_5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_5->Location = System::Drawing::Point(65, 65);
this->NumberButton_5->Name = L"NumberButton_5";
this->NumberButton_5->Size = System::Drawing::Size(56, 56);
this->NumberButton_5->TabIndex = 4;
this->NumberButton_5->Text = L"5";
this->NumberButton_5->UseVisualStyleBackColor = true;
this->NumberButton_5->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_4
//
this->NumberButton_4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_4->Location = System::Drawing::Point(3, 65);
this->NumberButton_4->Name = L"NumberButton_4";
this->NumberButton_4->Size = System::Drawing::Size(56, 56);
this->NumberButton_4->TabIndex = 3;
this->NumberButton_4->Text = L"4";
this->NumberButton_4->UseVisualStyleBackColor = true;
this->NumberButton_4->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_9
//
this->NumberButton_9->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_9->Location = System::Drawing::Point(127, 3);
this->NumberButton_9->Name = L"NumberButton_9";
this->NumberButton_9->Size = System::Drawing::Size(56, 56);
this->NumberButton_9->TabIndex = 2;
this->NumberButton_9->Text = L"9";
this->NumberButton_9->UseVisualStyleBackColor = true;
this->NumberButton_9->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_8
//
this->NumberButton_8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_8->Location = System::Drawing::Point(65, 3);
this->NumberButton_8->Name = L"NumberButton_8";
this->NumberButton_8->Size = System::Drawing::Size(56, 56);
this->NumberButton_8->TabIndex = 1;
this->NumberButton_8->Text = L"8";
this->NumberButton_8->UseVisualStyleBackColor = true;
this->NumberButton_8->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// NumberButton_7
//
this->NumberButton_7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->NumberButton_7->Location = System::Drawing::Point(3, 3);
this->NumberButton_7->Name = L"NumberButton_7";
this->NumberButton_7->Size = System::Drawing::Size(56, 56);
this->NumberButton_7->TabIndex = 0;
this->NumberButton_7->Text = L"7";
this->NumberButton_7->UseVisualStyleBackColor = true;
this->NumberButton_7->Click += gcnew System::EventHandler(this, &MyForm::NumberButton_Click);
//
// panel2
//
this->panel2->Controls->Add(this->textbox);
this->panel2->Controls->Add(this->numberPanel);
this->panel2->Controls->Add(this->Backspace);
this->panel2->Controls->Add(this->C);
this->panel2->Controls->Add(this->CE);
this->panel2->Location = System::Drawing::Point(12, 12);
this->panel2->Name = L"panel2";
this->panel2->Size = System::Drawing::Size(344, 347);
this->panel2->TabIndex = 5;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(358, 358);
this->Controls->Add(this->panel2);
this->MaximizeBox = false;
this->Name = L"MyForm";
this->Text = L"Калькулятор";
this->TopMost = true;
this->numberPanel->ResumeLayout(false);
this->panel1->ResumeLayout(false);
this->panel2->ResumeLayout(false);
this->panel2->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
double previous_number = 0;
double last_input = 0;
bool has_previous_number = false;
bool answer_shown = false;
System::Windows::Forms::Button^ saved_operation = CE;
private: System::Void NumberButton_Click(System::Object^ sender, System::EventArgs^ e)
{
if (answer_shown)
{
textbox->Text = "";
answer_shown = false;
}
System::Windows::Forms::Button^ button = safe_cast <System::Windows::Forms::Button^> (sender);
if (button == NegativeButton)
{
if (textbox->Text->IndexOf("-") != -1)
textbox->Text = textbox->Text->Substring(1);
else
textbox->Text = "-" + textbox->Text;
}
else
{
if (!(textbox->Text->Length - textbox->Text->Replace(",", "")->Length > 0 && button == comma))
textbox->Text += button->Text;
// не ставить запятую, если она уже есть
}
}
private: System::Void OperationButton_Click(System::Object^ sender, System::EventArgs^ e)
{
System::Windows::Forms::Button^ button = safe_cast <System::Windows::Forms::Button^> (sender);
double current_number;
try
{
current_number = Convert::ToDouble(textbox->Text);
}
catch (...)
{
show_message("Введите число");
return;
}
// 1/x
if (button == OperationButton_reciprocal)
{
if (current_number != 0)
{
show_message(Convert::ToString(1/current_number));
answer_shown = false;
}
else
{
show_message("Введите число, отличное от 0");
}
}
// проценты
else if (button == OperationButton_percent)
{
if (has_previous_number)
show_message(Convert::ToString(previous_number * 0.01 * current_number));
else
show_message(Convert::ToString(0));
answer_shown = false;
}
// корень
else if (button == OperationButton_sqrt)
{
if (current_number >= 0)
{
show_message(Convert::ToString(Math::Sqrt(current_number)));
answer_shown = false;
}
else
{
show_message("Введите неотрицательное");
}
}
else if (has_previous_number)
{
if (button == OperationButton_equal)
// равно
{
if (answer_shown)
{
previous_number = calculate(saved_operation, previous_number, last_input);
show_message(Convert::ToString(previous_number));
}
else
{
if (saved_operation == OperationButton_divide && current_number == 0)
show_message("Введите число, отличное от 0");
else
{
previous_number = calculate(saved_operation, previous_number, current_number);
last_input = current_number;
show_message(Convert::ToString(previous_number));
}
}
}
else
// операции, требующие 2-ух чисел
{
if (answer_shown)
saved_operation = button;
else
{
if (saved_operation == OperationButton_divide && current_number == 0)
show_message("Введите число, отличное от 0");
else
{
double result_number = calculate(saved_operation, previous_number, current_number);
previous_number = calculate(saved_operation, previous_number, current_number);
show_message(Convert::ToString(previous_number));
saved_operation = button;
}
}
}
}
else
{
has_previous_number = true;
previous_number = current_number;
saved_operation = button;
answer_shown = true;
}
}
private: System::Void Backspace_Click(System::Object^ sender, System::EventArgs^ e)
{
if (textbox->Text->Length > 0)
{
char c = (Convert::ToChar(textbox->Text->Substring(0, 1)));
if ((c >= '0' && c <= '9') || c == '-')
textbox->Text = textbox->Text->Remove(textbox->Text->Length - 1);
else
textbox->Text = "";
}
}
private: System::Void CE_Click(System::Object^ sender, System::EventArgs^ e)
{
textbox->Text = "";
}
private: System::Void C_Click(System::Object^ sender, System::EventArgs^ e)
{
reset();
}
private: void show_message(System::String^ message)
{
textbox->Text = message;
answer_shown = true;
}
private: void reset()
{
textbox->Text = "";
has_previous_number = false;
answer_shown = true;
}
private: double calculate(System::Windows::Forms::Button^ operation, double a, double b)
{
if (operation == OperationButton_Add)
{
return a + b;
}
else if (operation == OperationButton_subtract)
{
return a - b;
}
else if (operation == OperationButton_multiply)
{
return a * b;
}
else if (operation == OperationButton_divide)
{
return a / b;
}
else return a;
}
};
}
Соседние файлы в папке Лаба 2 (Сложный калькулятор)