Sugestão de Solução Calculadora Científica Visual C++
Calculadora.h:
#pragma once
class Calculadora
{ private:
double op1, op2, result;
public:
Calculadora(void);
double Soma(double, double);
double Subtracao(double, double);
double Multiplicacao(double, double);
double Divisao(double, double);
double EqSegundoGrau(double);
double Exponencial(double);
double Seno(double);
} ;
Calculadora.cpp:
#include "StdAfx.h"
#include "Calculadora.h"
#include "math.h"
#define PI 3.1416
Calculadora::Calculadora(void)
{
}
double Calculadora::Soma(double x, double y)
{ op1 = x;
op2 = y;
result = op1 + op2;
return result;
}
double Calculadora::Subtracao(double x, double y)
{op1 = x;
op2 = y;
result = op1 - op2;
return result;
}
double Calculadora::Multiplicacao(double x, double y)
{ op1 = x;
op2 = y;
result = op1 * op2;
return result;
}
double Calculadora::Divisao(double x, double y)
{op1 = x;
op2 = y;
result = op1 / op2;
return result;
}
double Calculadora::EqSegundoGrau(double x)
{ op1 = x;
result = op1 * op1;
return result;
}
double Calculadora::Exponencial(double x)
{ op1 = x;
result = pow(2.71828,op1);
return result;
}
double Calculadora::Seno(double x)
{ op1 = x;
result = sin(op1*PI/180);
return result;
}
Form1.h:
#pragma once
#include "Calculadora.h"
int opcao;
Calculadora Calc;
namespace CalculadoraGraf {
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();
Display->Focus();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ somar;
private: System::Windows::Forms::Button^ subtrair;
private: System::Windows::Forms::Button^ multiplicar;
private: System::Windows::Forms::Button^ dividir;
protected:
protected:
protected:
protected:
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button8;
private: System::Windows::Forms::TextBox^ Display;
private: System::Windows::Forms::Button^ Igual;
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->somar = (gcnew System::Windows::Forms::Button());
this->subtrair = (gcnew System::Windows::Forms::Button());
this->multiplicar = (gcnew System::Windows::Forms::Button());
this->dividir = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button8 = (gcnew System::Windows::Forms::Button());
this->Display = (gcnew System::Windows::Forms::TextBox());
this->Igual = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// somar
//
this->somar->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->somar->Location = System::Drawing::Point(37, 82);
this->somar->Name = L"somar";
this->somar->Size = System::Drawing::Size(59, 57);
this->somar->TabIndex = 0;
this->somar->Text = L"+";
this->somar->UseVisualStyleBackColor = true;
this->somar->Click += gcnew System::EventHandler(this, &Form1::somar_Click);
//
// subtrair
//
this->subtrair->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->subtrair->Location = System::Drawing::Point(102, 82);
this->subtrair->Name = L"subtrair";
this->subtrair->Size = System::Drawing::Size(59, 57);
this->subtrair->TabIndex = 1;
this->subtrair->Text = L"-";
this->subtrair->UseVisualStyleBackColor = true;
this->subtrair->Click += gcnew System::EventHandler(this, &Form1::subtrair_Click);
//
// multiplicar
//
this->multiplicar->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->multiplicar->Location = System::Drawing::Point(167, 82);
this->multiplicar->Name = L"multiplicar";
this->multiplicar->Size = System::Drawing::Size(59, 57);
this->multiplicar->TabIndex = 2;
this->multiplicar->Text = L"x";
this->multiplicar->UseVisualStyleBackColor = true;
this->multiplicar->Click += gcnew System::EventHandler(this, &Form1::multiplicar_Click);
// // dividir // this->dividir->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->dividir->Location = System::Drawing::Point(232, 82); this->dividir->Name = L"dividir"; this->dividir->Size = System::Drawing::Size(59, 57); this->dividir->TabIndex = 3; this->dividir->Text = L"/"; this->dividir->UseVisualStyleBackColor = true; this->dividir->Click += gcnew System::EventHandler(this, &Form1::dividir_Click); // // button5 // this->button5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->button5->Location = System::Drawing::Point(37, 157); this->button5->Name = L"button5"; this->button5->Size = System::Drawing::Size(59, 57); this->button5->TabIndex = 4; this->button5->Text = L"x^2"; this->button5->UseVisualStyleBackColor = true; this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click); // // button6 // this->button6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->button6->Location = System::Drawing::Point(105, 157); this->button6->Name = L"button6"; this->button6->Size = System::Drawing::Size(59, 57); this->button6->TabIndex = 5; this->button6->Text = L"exp(x)"; this->button6->UseVisualStyleBackColor = true; this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click); // // button8 // this->button8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->button8->Location = System::Drawing::Point(170, 158); this->button8->Name = L"button8"; this->button8->Size = System::Drawing::Size(59, 57); this->button8->TabIndex = 7; this->button8->Text = L"sen(x)"; this->button8->UseVisualStyleBackColor = true; this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click); // // Display // this->Display->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->Display->Location = System::Drawing::Point(37, 26); this->Display->Name = L"Display"; this->Display->Size = System::Drawing::Size(254, 38); this->Display->TabIndex = 8; this->Display->Text = L"0.0"; this->Display->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // Igual // this->Igual->BackColor = System::Drawing::SystemColors::ActiveCaption; this->Igual->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->Igual->Location = System::Drawing::Point(235, 157); this->Igual->Name = L"Igual"; this->Igual->Size = System::Drawing::Size(56, 57); this->Igual->TabIndex = 0; this->Igual->Text = L"="; this->Igual->UseVisualStyleBackColor = false; this->Igual->Click += gcnew System::EventHandler(this, &Form1::button9_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(307, 245); this->Controls->Add(this->Igual); this->Controls->Add(this->Display); this->Controls->Add(this->button8); this->Controls->Add(this->button6); this->Controls->Add(this->button5); this->Controls->Add(this->dividir); this->Controls->Add(this->multiplicar); this->Controls->Add(this->subtrair); this->Controls->Add(this->somar); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout();
}
- pragma endregion
double x, y, r;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void somar_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 1; x = System::Convert::ToDouble(Display->Text); Display->Clear(); Display->Focus();
}
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
switch(opcao)
{
case 1:
y = System::Convert::ToDouble(Display->Text);
r=Calc.Soma(x, y); break;
case 2:
y = System::Convert::ToDouble(Display->Text);
r=Calc.Subtracao(x,y);
break;
case 3:
y = System::Convert::ToDouble(Display->Text);
r=Calc.Multiplicacao(x,y); break;
case 4:
y = System::Convert::ToDouble(Display->Text);
r=Calc.Divisao(x,y); break;
}//switch
Display->Text = System::Convert::ToString(r); } private: System::Void subtrair_Click(System::Object^ sender, System::EventArgs^ e) { opcao = 2; x = System::Convert::ToDouble(Display->Text); Display->Clear(); Display->Focus();
}
private: System::Void multiplicar_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 3;
x = System::Convert::ToDouble(Display->Text); Display->Clear(); Display->Focus();
}
private: System::Void dividir_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 4;
x = System::Convert::ToDouble(Display->Text); Display->Clear(); Display->Focus();
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 5;
x = System::Convert::ToDouble(Display->Text); r=Calc.EqSegundoGrau(x);
Display->Text = System::Convert::ToString(r);
Display->Focus();
}
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 6;
x = System::Convert::ToDouble(Display->Text); r=Calc.Exponencial(x); Display->Text = System::Convert::ToString(r); Display->Focus();
}
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e) {
opcao = 7;
x = System::Convert::ToDouble(Display->Text); r=Calc.Seno(x); Display->Text = System::Convert::ToString(r); Display->Focus();
}
}; }