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.
Ana endi barcha tugmalarimiz kod yozishga tayyor bo’lib turibdi. Endi tugmalarga kerakli kodlarni yozib chiqamiz, albatta vazifasiga qarab.
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int iAmal=0;
float birinchiSon, ikkinchiSon, natija;
bool bAmal=False;-------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
if(bAmal)
LABEL->Caption="0";
if(LABEL->Caption=="0"){
LABEL->Caption=((TSpeedButton * )Sender)->Caption;
Label1->Caption=Label1->Caption+((TSpeedButton * )Sender)->Caption;}
else if (LABEL->Caption.Pos("-")!=0 && LABEL->Caption.Pos(".")!=0 && LABEL->Caption.Length() < 10 ) {
LABEL->Caption=LABEL->Caption+((TSpeedButton * )Sender)->Caption;
Label1->Caption=Label1->Caption+((TSpeedButton * )Sender)->Caption;}
else if (LABEL->Caption.Pos("-")==0 && LABEL->Caption.Pos(".")!=0 && LABEL->Caption.Length() <9) {
LABEL->Caption=LABEL->Caption+((TSpeedButton * )Sender)->Caption;
Label1->Caption=Label1->Caption+((TSpeedButton * )Sender)->Caption;}
else if (LABEL->Caption.Pos("-")!=0 && LABEL->Caption.Pos(".")== 0 && LABEL->Caption.Length() <9) {
LABEL->Caption=LABEL->Caption+((TSpeedButton * )Sender)->Caption;
Label1->Caption=Label1->Caption+((TSpeedButton * )Sender)->Caption;}
else if(LABEL->Caption.Length()< 8){
LABEL->Caption=LABEL->Caption+((TSpeedButton * )Sender)->Caption;
Label1->Caption=Label1->Caption+((TSpeedButton * )Sender)->Caption;}
bAmal=False;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
if (LABEL->Caption !="0"){
double a=StrToFloat(LABEL->Caption) ;
a=a*(-1);
LABEL->Caption= FloatToStr(a) ;
Label1->Caption= "-("+Label1->Caption+")" ;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
if (LABEL->Caption != "0" && LABEL->Caption.Pos("-")!=0 && LABEL->Caption.Length() <9 ) {
LABEL->Caption=LABEL->Caption +"0"; }
else if (LABEL->Caption != "0" && LABEL->Caption.Length() <8 ) {
LABEL->Caption=LABEL->Caption +"0"; }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton27Click(TObject *Sender)
{
LABEL->Caption="0";
Label1->Caption="";
bAmal=False;
iAmal=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
if (LABEL->Caption.Pos(".") == 0) {
LABEL->Caption=LABEL->Caption+ ".";
Label1->Caption=Label1->Caption+ ".";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton28Click(TObject *Sender)
{
if (LABEL->Caption != "0" ) {
if (LABEL->Caption.Length()==1) {
LABEL->Caption="0";
Label1->Caption="";}
else {LABEL->Caption=LABEL->Caption.SubString(0, LABEL->Caption.Length()-1);
Label1->Caption=Label1->Caption.SubString(0, Label1->Caption.Length()-1); }}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton21Click(TObject *Sender)
{
if (LABEL->Caption!="0") {
double a;
a=StrToFloat(LABEL->Caption);
a=1/a;
LABEL->Caption= FloatToStr(a); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton22Click(TObject *Sender)
{
if (LABEL->Caption!="0") {
double a;
a=StrToFloat(LABEL->Caption);
a=a*a;
LABEL->Caption= FloatToStr(a); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton25Click(TObject *Sender)
{
if (LABEL->Caption!=0) {
if (LABEL->Caption!="0") {
double a;
a=StrToFloat(LABEL->Caption);
a=a/100;
LABEL->Caption= FloatToStr(a); }
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
{
bAmal=True;
String s;
s=(((TSpeedButton*)Sender)->Caption);
if(s=="+")
iAmal=1;
if(s=="-")
iAmal=2;
if(s=="*")
iAmal=3;
if(s=="/")
iAmal=4;
Label1->Caption=Label1->Caption+s;
birinchiSon=StrToFloat(LABEL->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
ikkinchiSon=StrToFloat(LABEL->Caption);
switch (iAmal) {
case 1: natija=birinchiSon+ikkinchiSon;
LABEL->Caption=FloatToStr(natija); break;
case 2: natija=birinchiSon-ikkinchiSon;
LABEL->Caption=FloatToStr(natija); break;
case 3: natija=birinchiSon*ikkinchiSon;
LABEL->Caption=FloatToStr(natija); break;
case 4: natija=birinchiSon/ikkinchiSon;
LABEL->Caption=FloatToStr(natija); break;
default:
;
}
if(LABEL->Caption.Length()>=15)
LABEL->Caption=LABEL->Caption.SubString(0, LABEL->Caption.Pos(",")+8);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton23Click(TObject *Sender)
{
if (LABEL->Caption!="0") {
double a;
a=StrToFloat(LABEL->Caption);
a= sqrt(a);
LABEL->Caption= FloatToStr(a); }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton26Click(TObject *Sender)
{
LABEL->Caption="0";
bAmal=False;
iAmal=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
SpeedButton3->Caption=Now();
SpeedButton3->Caption=SpeedButton3->Caption.SubString(SpeedButton3->Caption.Length()-10, SpeedButton3->Caption.Length());
}
//---------------------------------------------------------------------------
8>9>
Do'stlaringiz bilan baham: