Home > DeveloperSection > Blogs > Calculator in C++

Calculator in C


.NET Visual C++ 
Ratings:
0 Comment(s)
 956  View(s)
Rate this:

Calculator in C++

In this blog, I’m create a Calculator using Visual studio c++

Description

This is simple calculator that contains four basic mathimatical functions for common calculation.

C++ code for the design

Calculator.h

#pragma once

#include <string.h>

#include <exception>

namespace CalculatorSam {

 

            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 Calculator

            /// </summary>

            public ref class Calculator : public System::Windows::Forms::Form

            {public:

                        private: System::String^ value;

            private: System::Windows::Forms::Button^  btnClear;

            private: System::String^ OperatorSelected;

            public:

                        Calculator(void)

                        {

                                    InitializeComponent();

                                    //

                                    //TODO: Add the constructor code here

                                    //

                        }

 

            protected:

                        /// <summary>

                        /// Clean up any resources being used.

                        /// </summary>

                        ~Calculator()

                        {

                                    if (components)

                                    {

                                                delete components;

                                    }

                        }

            private: System::Windows::Forms::TextBox^  txtResult;

            protected:

            private: System::Windows::Forms::Button^  btn1;

            private: System::Windows::Forms::Button^  btn2;

            private: System::Windows::Forms::Button^  btn3;

            private: System::Windows::Forms::Button^  btn4;

            private: System::Windows::Forms::Button^  btn5;

            private: System::Windows::Forms::Button^  btn6;

            private: System::Windows::Forms::Button^  btn7;

            private: System::Windows::Forms::Button^  btn8;

            private: System::Windows::Forms::Button^  btn9;

            private: System::Windows::Forms::Button^  btn0;

            private: System::Windows::Forms::Button^  btnEqual;

            private: System::Windows::Forms::Button^  btnDecimal;

            private: System::Windows::Forms::Button^  btnAdd;

            private: System::Windows::Forms::Button^  btnSub;

            private: System::Windows::Forms::Button^  btnMul;

            private: System::Windows::Forms::Button^  btnDiv;

 

            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->txtResult = (gcnew System::Windows::Forms::TextBox());

                                    this->btn1 = (gcnew System::Windows::Forms::Button());

                                    this->btn2 = (gcnew System::Windows::Forms::Button());

                                    this->btn3 = (gcnew System::Windows::Forms::Button());

                                    this->btn4 = (gcnew System::Windows::Forms::Button());

                                    this->btn5 = (gcnew System::Windows::Forms::Button());

                                    this->btn6 = (gcnew System::Windows::Forms::Button());

                                    this->btn7 = (gcnew System::Windows::Forms::Button());

                                    this->btn8 = (gcnew System::Windows::Forms::Button());

                                    this->btn9 = (gcnew System::Windows::Forms::Button());

                                    this->btn0 = (gcnew System::Windows::Forms::Button());

                                    this->btnEqual = (gcnew System::Windows::Forms::Button());

                                    this->btnDecimal = (gcnew System::Windows::Forms::Button());

                                    this->btnAdd = (gcnew System::Windows::Forms::Button());

                                    this->btnSub = (gcnew System::Windows::Forms::Button());

                                    this->btnMul = (gcnew System::Windows::Forms::Button());

                                    this->btnDiv = (gcnew System::Windows::Forms::Button());

                                    this->btnClear = (gcnew System::Windows::Forms::Button());

                                    this->SuspendLayout();

                                    //

                                    // txtResult

                                    // this is textbox to show input numbers

                                    this->txtResult->Location = System::Drawing::Point(12, 24);

                                    this->txtResult->Name = L"txtResult";

                                    this->txtResult->Size = System::Drawing::Size(231, 20);

                                    this->txtResult->TabIndex = 0;

                                    //

                                    // btn1

                                    // this is button  for 1

                                    this->btn1->Location = System::Drawing::Point(13, 71);

                                    this->btn1->Name = L"btn1";

                                    this->btn1->Size = System::Drawing::Size(45, 38);

                                    this->btn1->TabIndex = 1;

                                    this->btn1->Text = L"1";

                                    this->btn1->UseVisualStyleBackColor = true;

                                    this->btn1->Click += gcnew System::EventHandler(this, &Calculator::button_Click);

                                    //

                                    // btn2

                                    // this is button  for 2

                                    this->btn2->Location = System::Drawing::Point(74, 71);

                                    this->btn2->Name = L"btn2";

                                    this->btn2->Size = System::Drawing::Size(45, 38);

                                    this->btn2->TabIndex = 2;

                                    this->btn2->Text = L"2";

                                    this->btn2->UseVisualStyleBackColor = true;

                                    this->btn2->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn3

                                    // this is button  for 3

                                    this->btn3->Location = System::Drawing::Point(135, 71);

                                    this->btn3->Name = L"btn3";

                                    this->btn3->Size = System::Drawing::Size(45, 38);

                                    this->btn3->TabIndex = 3;

                                    this->btn3->Text = L"3";

                                    this->btn3->UseVisualStyleBackColor = true;

                                    this->btn3->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn4

                                    // this is button  for 4

                                    this->btn4->Location = System::Drawing::Point(196, 71);

                                    this->btn4->Name = L"btn4";

                                    this->btn4->Size = System::Drawing::Size(45, 38);

                                    this->btn4->TabIndex = 4;

                                    this->btn4->Text = L"4";

                                    this->btn4->UseVisualStyleBackColor = true;

                                    this->btn4->Click += gcnew System::EventHandler(this, &Calculator:: button _Click);

                                    //

                                    // btn5

                                    // this is button  for 5

                                    this->btn5->Location = System::Drawing::Point(13, 123);

                                    this->btn5->Name = L"btn5";

                                    this->btn5->Size = System::Drawing::Size(45, 38);

                                    this->btn5->TabIndex = 5;

                                    this->btn5->Text = L"5";

                                    this->btn5->UseVisualStyleBackColor = true;

                                    this->btn5->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn6

                                    //this is button  for 6

                                    this->btn6->Location = System::Drawing::Point(74, 123);

                                    this->btn6->Name = L"btn6";

                                    this->btn6->Size = System::Drawing::Size(45, 38);

                                    this->btn6->TabIndex = 6;

                                    this->btn6->Text = L"6";

                                    this->btn6->UseVisualStyleBackColor = true;

                                    this->btn6->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn7

                                    // this is button  for 7

                                    this->btn7->Location = System::Drawing::Point(135, 123);

                                    this->btn7->Name = L"btn7";

                                    this->btn7->Size = System::Drawing::Size(45, 38);

                                    this->btn7->TabIndex = 7;

                                    this->btn7->Text = L"7";

                                    this->btn7->UseVisualStyleBackColor = true;

                                    this->btn7->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn8

                                    // this is button  for 8

                                    this->btn8->Location = System::Drawing::Point(196, 123);

                                    this->btn8->Name = L"btn8";

                                    this->btn8->Size = System::Drawing::Size(45, 38);

                                    this->btn8->TabIndex = 8;

                                    this->btn8->Text = L"8";

                                    this->btn8->UseVisualStyleBackColor = true;

                                    this->btn8->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn9

                                    // this is button  for 9

                                    this->btn9->Location = System::Drawing::Point(13, 175);

                                    this->btn9->Name = L"btn9";

                                    this->btn9->Size = System::Drawing::Size(45, 38);

                                    this->btn9->TabIndex = 9;

                                    this->btn9->Text = L"9";

                                    this->btn9->UseVisualStyleBackColor = true;

                                    this->btn9->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btn0

                                    // this is button  for 0

                                    this->btn0->Location = System::Drawing::Point(74, 175);

                                    this->btn0->Name = L"btn0";

                                    this->btn0->Size = System::Drawing::Size(45, 38);

                                    this->btn0->TabIndex = 10;

                                    this->btn0->Text = L"0";

                                    this->btn0->UseVisualStyleBackColor = true;

                                    this->btn0->Click += gcnew System::EventHandler(this, &Calculator:: button_Click);

                                    //

                                    // btnEqual

                                    // this is button  for =

                                    this->btnEqual->Location = System::Drawing::Point(196, 175);

                                    this->btnEqual->Name = L"btnEqual";

                                    this->btnEqual->Size = System::Drawing::Size(45, 38);

                                    this->btnEqual->TabIndex = 11;

                                    this->btnEqual->Text = L"=";

                                    this->btnEqual->UseVisualStyleBackColor = true;

                                    this->btnEqual->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    //

                                    // btnDecimal

                                    // this is button  for .

                                    this->btnDecimal->Location = System::Drawing::Point(135, 175);

                                    this->btnDecimal->Name = L"btnDecimal";

                                    this->btnDecimal->Size = System::Drawing::Size(45, 38);

                                    this->btnDecimal->TabIndex = 12;

                                    this->btnDecimal->Text = L".";

                                    this->btnDecimal->UseVisualStyleBackColor = true;

                                    this->btnDecimal->Click += gcnew System::EventHandler(this, &Calculator::btnDecimal_Click);

                                    //

                                    // btnAdd

                                    // this is button  for +

                                    this->btnAdd->Location = System::Drawing::Point(13, 227);

                                    this->btnAdd->Name = L"btnAdd";

                                    this->btnAdd->Size = System::Drawing::Size(45, 38);

                                    this->btnAdd->TabIndex = 13;

                                    this->btnAdd->Text = L"+";

                                    this->btnAdd->UseVisualStyleBackColor = true;

                                    this->btnAdd->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    //

                                    // btnSub

                                    // this is button  for -

                                    this->btnSub->Location = System::Drawing::Point(74, 227);

                                    this->btnSub->Name = L"btnSub";

                                    this->btnSub->Size = System::Drawing::Size(45, 38);

                                    this->btnSub->TabIndex = 14;

                                    this->btnSub->Text = L"-";

                                    this->btnSub->UseVisualStyleBackColor = true;

                                    this->btnSub->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    //

                                    // btnMul

                                    // this is button  for *

                                    this->btnMul->Location = System::Drawing::Point(135, 227);

                                    this->btnMul->Name = L"btnMul";

                                    this->btnMul->Size = System::Drawing::Size(45, 38);

                                    this->btnMul->TabIndex = 15;

                                    this->btnMul->Text = L"*";

                                    this->btnMul->UseVisualStyleBackColor = true;

                                    this->btnMul->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    //

                                    // btnDiv

                                    // this is button  for /

                                    this->btnDiv->Location = System::Drawing::Point(196, 227);

                                    this->btnDiv->Name = L"btnDiv";

                                    this->btnDiv->Size = System::Drawing::Size(45, 38);

                                    this->btnDiv->TabIndex = 16;

                                    this->btnDiv->Text = L"/";

                                    this->btnDiv->UseVisualStyleBackColor = true;

                                    this->btnDiv->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    //

                                    // btnClear

                                    // this is button  for clear

                                    this->btnClear->Location = System::Drawing::Point(12, 279);

                                    this->btnClear->Name = L"btnClear";

                                    this->btnClear->Size = System::Drawing::Size(45, 38);

                                    this->btnClear->TabIndex = 17;

                                    this->btnClear->Text = L"Clear";

                                    this->btnClear->UseVisualStyleBackColor = true;

                                    this->btnClear->Click += gcnew System::EventHandler(this, &Calculator::btnClear_Click);

                                    //

                                    // Calculator

                                    //

                                    this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

                                    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

                                    this->ClientSize = System::Drawing::Size(258, 322);

                                    this->Controls->Add(this->btnClear);

                                    this->Controls->Add(this->btnDiv);

                                    this->Controls->Add(this->btnMul);

                                    this->Controls->Add(this->btnSub);

                                    this->Controls->Add(this->btnAdd);

                                    this->Controls->Add(this->btnDecimal);

                                    this->Controls->Add(this->btnEqual);

                                    this->Controls->Add(this->btn0);

                                    this->Controls->Add(this->btn9);

                                    this->Controls->Add(this->btn8);

                                    this->Controls->Add(this->btn7);

                                    this->Controls->Add(this->btn6);

                                    this->Controls->Add(this->btn5);

                                    this->Controls->Add(this->btn4);

                                    this->Controls->Add(this->btn3);

                                    this->Controls->Add(this->btn2);

                                    this->Controls->Add(this->btn1);

                                    this->Controls->Add(this->txtResult);

                                    this->Name = L"Calculator";

                                    this->Text = L"Calculator";

                                    this->Click += gcnew System::EventHandler(this, &Calculator::btnOperation);

                                    this->ResumeLayout(false);

                                    this->PerformLayout();

 

                        }

#pragma endregion

                        private: bool IsNum;

//calling display function and passing the text of button clicked as string

            private: System::Void button_Click (System::Object^  sender, System::EventArgs^  e) {

                                                  if(OperatorSelected!=String::Empty && !IsNum)

                                                               txtResult->Text="";

                                                   Button^ button = (Button^)sender ;

                                                   txtResult->Text+=button->Text;

                                                   IsNum=true;

                                     }

//checking whether ‘.’ Is present in text box or not if present then no

             //action will be taken as there can be only one decimal point in a number

            //and if not then decimal will be added.

private: System::Void btnDecimal_Click(System::Object^  sender, System::EventArgs^  e) {

                                      System::String^ textBoxValue = txtResult->Text;

                                                 if(textBoxValue->IndexOf(".")<0)

                                                 {

                                                             if(textBoxValue->Length<=0)

                                                                         txtResult->Text+="0.";

                                                             else

                                                                         txtResult->Text+=".";

                                                 }

                         }

//this function perform all mathematical operation

private: System::Void btnOperation(System::Object^  sender, System::EventArgs^  e) {

                                      Button^ button = (Button^)sender ;

                                                try

                                                 {

                                                             if(!String::IsNullOrEmpty(OperatorSelected) && !String::IsNullOrEmpty(value))

                                                 {

//this condition perform addition operation

                                                             if(OperatorSelected=="+")

                                                             {

                                                                         txtResult->Text=Convert::ToString(Convert::ToDouble(value) + Convert::ToDouble(txtResult->Text));

 

                                                             }

//this condition perform  subtraction operation

 

                                                             else if(OperatorSelected=="-")

                                                             {

                                                                         txtResult->Text=Convert::ToString(Convert::ToDouble(value) - Convert::ToDouble(txtResult->Text));

                                                             }

//this condition perform division operation

                                                             else if(OperatorSelected=="/")

                                                             {

                                                                         txtResult->Text=Convert::ToString(Convert::ToDouble(value) / Convert::ToDouble(txtResult->Text));

                                                             }

//this condition perform division operation

 

                                                             else if(OperatorSelected=="*")

                                                             {

                                                                         txtResult->Text=Convert::ToString(Convert::ToDouble(value) * Convert::ToDouble(txtResult->Text));

                                                             }

                                                             else if(button->Text=="=")

                                                             {txtResult->Text=value;

                                                             }

                                                             value=txtResult->Text;

                                                    }

                                                    value=txtResult->Text;

                                                            OperatorSelected=button->Text;

                                                   IsNum=false;

                                                 }

                                                 catch(std::exception & ex)

                 {

                                                             txtResult->Text="";

                                                  }

                       

                         }

private: System::Void btnClear_Click(System::Object^  sender, System::EventArgs^  e) {

                                      txtResult->Text="";

                                                 value=String::Empty;

                         }

};

}

 

Calculator.cpp

#include "Calculator.h"

using namespace CalculatorSam;

//Entry Point for Window form

void WinMain()

{

            Application::EnableVisualStyles();

            Application::SetCompatibleTextRenderingDefault(false);

            CalculatorSam::Calculator Form;

            Application::Run(%Form);

            Application::Exit();

            Environment::Exit(0);

}


Don't want to miss updates? Please click the below button!

Follow MindStick