Solução2
- pragma once
char flag = 0; float operando1=0, operando2 = 0, resultado = 0;
namespace calculadora4 {
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> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // }
protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ soma; private: System::Windows::Forms::Button^ subtracao; private: System::Windows::Forms::Button^ multiplicacao; private: System::Windows::Forms::Button^ divisao; protected:
protected:
private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ op1; private: System::Windows::Forms::TextBox^ op2;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ result;
private: System::Windows::Forms::Label^ label3;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
- pragma region Windows Form Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->soma = (gcnew System::Windows::Forms::Button()); this->subtracao = (gcnew System::Windows::Forms::Button()); this->multiplicacao = (gcnew System::Windows::Forms::Button()); this->divisao = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->op1 = (gcnew System::Windows::Forms::TextBox()); this->op2 = (gcnew System::Windows::Forms::TextBox()); this->label2 = (gcnew System::Windows::Forms::Label()); this->button1 = (gcnew System::Windows::Forms::Button()); this->result = (gcnew System::Windows::Forms::TextBox()); this->label3 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // soma // this->soma->Location = System::Drawing::Point(28, 172); this->soma->Name = L"soma"; this->soma->Size = System::Drawing::Size(52, 42); this->soma->TabIndex = 10; this->soma->Text = L"+"; this->soma->UseVisualStyleBackColor = true; this->soma->Click += gcnew System::EventHandler(this, &Form1::soma_Click); // // subtracao // this->subtracao->Location = System::Drawing::Point(86, 172); this->subtracao->Name = L"subtracao"; this->subtracao->Size = System::Drawing::Size(54, 42); this->subtracao->TabIndex = 11; this->subtracao->Text = L"-"; this->subtracao->UseVisualStyleBackColor = true; this->subtracao->Click += gcnew System::EventHandler(this, &Form1::subtracao_Click); // // multiplicacao // this->multiplicacao->Location = System::Drawing::Point(146, 172); this->multiplicacao->Name = L"multiplicacao"; this->multiplicacao->Size = System::Drawing::Size(59, 42); this->multiplicacao->TabIndex = 12; this->multiplicacao->Text = L"x"; this->multiplicacao->UseVisualStyleBackColor = true; this->multiplicacao->Click += gcnew System::EventHandler(this, &Form1::multiplicacao_Click); // // divisao // this->divisao->Location = System::Drawing::Point(211, 172); this->divisao->Name = L"divisao"; this->divisao->Size = System::Drawing::Size(63, 42); this->divisao->TabIndex = 13; this->divisao->Text = L"/"; this->divisao->UseVisualStyleBackColor = true; this->divisao->Click += gcnew System::EventHandler(this, &Form1::divisao_Click); // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(36, 18); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(66, 13); this->label1->TabIndex = 15; this->label1->Text = L"Operando 1:"; // // op1 // this->op1->Location = System::Drawing::Point(108, 12); this->op1->Name = L"op1"; this->op1->Size = System::Drawing::Size(148, 20); this->op1->TabIndex = 16; // // op2 // this->op2->Location = System::Drawing::Point(108, 55); this->op2->Name = L"op2"; this->op2->Size = System::Drawing::Size(148, 20); this->op2->TabIndex = 18; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(36, 62); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(66, 13); this->label2->TabIndex = 17; this->label2->Text = L"Operando 2:"; // // button1 // this->button1->Location = System::Drawing::Point(329, 150); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(92, 64); this->button1->TabIndex = 19; this->button1->Text = L"Resolva"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // result // this->result->Location = System::Drawing::Point(108, 104); this->result->Name = L"result"; this->result->Size = System::Drawing::Size(148, 20); this->result->TabIndex = 21; // // label3 // this->label3->AutoSize = true; this->label3->Location = System::Drawing::Point(36, 111); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(58, 13); this->label3->TabIndex = 20; this->label3->Text = L"Resultado:"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(447, 275); this->Controls->Add(this->result); this->Controls->Add(this->label3); this->Controls->Add(this->button1); this->Controls->Add(this->op2); this->Controls->Add(this->label2); this->Controls->Add(this->op1); this->Controls->Add(this->label1); this->Controls->Add(this->divisao); this->Controls->Add(this->multiplicacao); this->Controls->Add(this->subtracao); this->Controls->Add(this->soma); this->Name = L"Form1"; this->Text = L"Calculadora simples de 4 operações"; this->ResumeLayout(false); this->PerformLayout();
}
- pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
operando1 = Convert::ToDouble(op1->Text); operando2 = Convert::ToDouble(op2->Text);
switch(flag) { case 1:
resultado = operando1 + operando2;
result->Text = Convert::ToString(resultado); break; case 2:
resultado = operando1 - operando2;
result->Text = Convert::ToString(resultado); break; case 3:
resultado = operando1 * operando2;
result->Text = Convert::ToString(resultado); break; case 4: if(operando2) { resultado = operando1/operando2; result->Text = Convert::ToString(resultado); }else result->Text = "Divisão por zero"; break; default: result->Text = "Erro!!"; break; }
}
private: System::Void soma_Click(System::Object^ sender, System::EventArgs^ e) {
flag = 1; //teclou soma
}
private: System::Void subtracao_Click(System::Object^ sender, System::EventArgs^ e) {
flag = 2; //teclou subtração
} private: System::Void multiplicacao_Click(System::Object^ sender, System::EventArgs^ e) {
flag = 3; //teclou multiplicação
}
private: System::Void divisao_Click(System::Object^ sender, System::EventArgs^ e) {
flag = 4; //teclou divisão
} }; }