2-dedline. Kalkulyator dasturini tayyorlash.
Men Kalkulyator dasturini tayyorlash uchun C++ Builder dasturlash
muhitining Builder 3 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,
C aption 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 :
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 kodlari:
#include
#pragma hdrstop
#include
#include "Unit1.h"
/-------------------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int amal;
float bir_son, ikki_son,natija=0;
String uzunlik;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="1";
}
else
Label1->Caption=Label1->Caption+"1";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="2";
}
else
Label1->Caption=Label1->Caption+"2";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="3";
}
else
Label1->Caption=Label1->Caption+"3";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="4";
}
else
Label1->Caption=Label1->Caption+"4";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="5";
}
else
Label1->Caption=Label1->Caption+"5";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="6";
}
else
Label1->Caption=Label1->Caption+"6";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="7";
}
else
Label1->Caption=Label1->Caption+"7";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="8";
}
else
Label1->Caption=Label1->Caption+"8";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
{
if (Label1->Caption==0)
{
Label1->Caption="9";
}
else
Label1->Caption=Label1->Caption+"9";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton18Click(TObject *Sender)
{
if (Label1->Caption !="0")
{
Label1->Caption=Label1->Caption+"0";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
amal=1;
bir_son = Label1->Caption.ToDouble();
Label1->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
amal=2;
bir_son = Label1->Caption.ToDouble();
Label1->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
{
amal=3;
bir_son = Label1->Caption.ToDouble();
Label1->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
amal=4;
bir_son = Label1->Caption.ToDouble();
Label1->Caption="0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton20Click(TObject *Sender)
{
int a;
ikki_son=Label1->Caption.ToDouble();
switch (amal)
{
case 1:natija=bir_son+ikki_son;break;
case 2:natija=bir_son-ikki_son;break;
case 3:natija=bir_son*ikki_son; break;
case 4:natija=bir_son/ikki_son;
break;
}
uzunlik=FloatToStr(natija);
a=uzunlik.Length();
if (a>9)
{
Label1->Caption=uzunlik.Delete(9,a-9);
}
else
Label1->Caption=natija;
Label1->Caption=natija;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton15Click(TObject *Sender)
{
Label1->Caption="0";
amal=1;
bir_son=0;
ikki_son=0;
/-----------------------------------------------------------------------------
Barcha ko’dlarni yozib so’ngra kankulyatordi ishga tushiramiz.
Dostları ilə paylaş: |