niedziela, 24 marca 2024

Calculator - C++ Builder


class Calculator:

File - calc_unit.h 

#ifndef calc_unitH
#define calc_unitH
//---------------------------------------------------------------------------


class Calculator
{
private:
float x1,x2,result;
public:
Calculator();
void Read(float a, float b);
float Plus();
float Minus();
float Multiplication();
float Division();
float SQRT();
float Inverse();
float EXP();
};
#endif

File - calc_unit.cpp

#pragma hdrstop

#include "calc_unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

Calculator::Calculator()
{
;
}
void Calculator::Read(float a, float b)
{
x1=a;
x2=b;
}
float Calculator::Plus()
{
result=x1+x2;
return result;
}

float Calculator::Minus()
{
result=x1-x2;
return result;
}
float Calculator::Multiplication()
{
result=x1*x2;
return result;
}
float Calculator::Division()
{
   result=x1/x2;
   return result;
}
float Calculator::SQRT()
{
result=sqrt(x1);
return result;
}
float Calculator::Inverse()
{
result=(1.00/x1);
return result;
}
float Calculator::EXP()
{
  int i,temp=int(x2);
  result=x1;
  for(i=0;i<temp-1;i++)
   result*=x1;
  return result;
}

Project C++ Builder:

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "p_Proj_Calc.h"
#include "calc_unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Calculator cl;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Plus());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
  Edit3->Text=FloatToStr(cl.Minus());

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Multiplication());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Division());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.Inverse());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.SQRT());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
 cl.Read(StrToFloat(Edit1->Text),StrToFloat(Edit2->Text));
 Edit3->Text=FloatToStr(cl.EXP());
}