1-dedline. Kalkulyator dasturini tayyorlash

1-dedline. Kalkulyator dasturini tayyorlash.
Men Kalkulyator dasturini tayyorlash uchun C++ Builder dasturlash muhitining Rad Studio XE3 versiyasidan foydalandim. C++ Builderda yangi loyiha yaratamiz:
Dastlab Kalkulyator dasturini interfeysini yaratib olamiz. Dastlab formaga dasturimiz ko’rinishi chiroyliroq va va yaratish davomida qulay bo’lishi uchun 2 ta Panel joylaymiz birinchisini ekran sifatida ishlatamiz ikkinchisini esa tugmalar uchun ishlatamiz. Birinchi Panelga 3ta Label komponentini joylamymiz. Birinchi Label komponentasini joylashtib kalkulyator ekrani sifatida foydalanish uchun uning Align xuxusiyatini alTopga, Alignment xususiyatini taRightJustifyga, Caption xuxusiyatini “0” ga o’zgartirib, so’ng xotira uchun yana bitta Label joylaymiz uni ham xususiyatlarini 1-Label kabi qilamiz faqat font dan rangini ozgartirib qo’yamiz , endi esa so’ngi Labelni joylaymiz bu esa M ya’ni xotir aishlaganini bizga ko’rsatib turish uchun hizmat qiladi unu chap tomonga joylaymiz va ko’rinishni yuidagi xolatga keltiramiz:
Bu holatga kelgandan so’ng asosiy labeldan boshqa labellarni Captionini o’chirib tashlaymi, chunki kodlar yozish orqali kerakli paytda nom qo’yib olamiz.
So’ng 2-Panelga barcha kerakli tugmalarni joylaymiz, tugmalar sifatida SpeedButton dan foydalanamiz chunki uning imkoniyatlari ancha keng,song dasturimiz quyidagi holatga keladi:
Ana endi barcha tugmalarimiz kod yozishga tayyor bo’lib turibdi. Endi tugmalarga kerakli kodlarni yozib chiqamiz, albatta vazifasiga qarab.
Avval raqamlarni ekranga chiqarish uchun kodlar yozib chiqamiz, so’ng Sender funksiyasi orqali boshqa raqamlarga ham ulab qo’yamiz, undan so’ng ammalarning tugmalariga kodlar yozamiz. Keyin esa tenglik tugmasini kodimni yozamiz,song boshqa tugmalarga navbatdan kodlar yozamiz.
Dastur kodi:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
#include "math.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool bAmal=False,M=False;
String sAmal;
float birinchiSon,ikkinchiSon, Natija,xotiraM;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton21Click(TObject *Sender)
{
if(bAmal)
{
(!M)?birinchiSon=StrToFloat(Label2->Caption) : M=False;
Label2->Caption="0";
bAmal=False;
}
if(Label2->Caption.Length()>12)
Label2->Font->Size=20;
else Label2->Font->Size=24;
if(Label2->Caption=="0")
Label2->Caption=((TSpeedButton*)Sender)->Caption;
else if(Label2->Caption.Length()<=16)
Label2->Caption=Label2->Caption+((TSpeedButton*)Sender)->Caption;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton28Click(TObject *Sender)
{
if(Label2->Caption.Length()>12)
Label2->Font->Size=20;
else Label2->Font->Size=24;
bAmal=True;
Label1->Caption=Label2->Caption+((TSpeedButton*)Sender)->Caption;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
float a;
if(Label2->Caption!="0")
{
a=StrToFloat(Label2->Caption)*(-1);
Label2->Caption=FloatToStr(a);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton26Click(TObject *Sender)
{
if(Label2->Caption!="0")
Label2->Caption=Label2->Caption+"0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
{
Label1->Caption="";
Label2->Caption="0";
if(Label2->Caption.Length()>12)
Label2->Font->Size=20;
else Label2->Font->Size=24;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
if(Label2->Caption!="0")
Label2->Caption=Label2->Caption.SubString(0, Label2->Caption.Length()-1);
if(Label2->Caption=="")
Label2->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton29Click(TObject *Sender)
{
int iAmal;
sAmal=Label1->Caption.SubString(Label1->Caption.Length(), 1);
ikkinchiSon=StrToFloat(Label2->Caption);
if(sAmal=="+")
iAmal=1;
else if(sAmal=="-")
iAmal=2;
else if(sAmal=="*")
iAmal=3;
else if(sAmal=="/")
iAmal=4;
switch (iAmal) {
case 1: Natija=birinchiSon+ikkinchiSon;
Label2->Caption=Natija; break;
case 2: Natija=birinchiSon-ikkinchiSon;
Label2->Caption=Natija; break;
case 3: Natija=birinchiSon*ikkinchiSon;
Label2->Caption=Natija; break;
case 4: Natija=birinchiSon/ikkinchiSon;
Label2->Caption=Natija; break;
default:
;
}
if(Label2->Caption.Length()>=15)
Label2->Caption=Label2->Caption.SubString(0, Label2->Caption.Pos(",")+8);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton30Click(TObject *Sender)
{
if(Label2->Caption.Pos(",")==0)
Label2->Caption=Label2->Caption+",";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton19Click(TObject *Sender)
{
float a;
a=StrToFloat(Label2->Caption);
a=1/a;
Label1->Caption="reciproc("+Label2->Caption+")";
Label2->Caption=FloatToStr(a);
if(Label2->Caption.Length()>=15)
Label2->Caption=Label2->Caption.SubString(0, Label2->Caption.Pos(",")+6);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton14Click(TObject *Sender)
{
float b;
ikkinchiSon=StrToFloat(Label2->Caption);
sAmal=Label1->Caption.SubString(Label1->Caption.Length(), 1);
if(sAmal=="*")
{
b=(birinchiSon*ikkinchiSon)/100;
Label2->Caption=FloatToStr(b);
}
if(Label2->Caption.Length()>=15)
Label2->Caption=Label2->Caption.SubString(0, Label2->Caption.Pos(",")+6);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
Label1->Caption="sqrt("+Label2->Caption+")";
float ildiz;
ildiz=sqrt(StrToFloat(Label2->Caption));
Label2->Caption=FloatToStr(ildiz);
if(Label2->Caption.Length()>=15)
Label2->Caption=Label2->Caption.SubString(0, Label2->Caption.Pos(",")+6);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
Label2->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
Label2->Caption=FloatToStr(xotiraM);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
if(Label2->Caption!="0")
{
xotiraM=StrToFloat(Label2->Caption);
Label3->Caption="M";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
xotiraM=0;
Label3->Caption="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
M=True;
Label1->Caption=FloatToStr(xotiraM)+"+";
birinchiSon=xotiraM;
bAmal=True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
M=True;
Label1->Caption=FloatToStr(xotiraM)+"-";
birinchiSon=xotiraM;
bAmal=True;
}
//---------------------------------------------------------------------------
Barcha kodlar tugagach dasturmizni ishga tushirib quyidagi natijani olamiz:
|