1.9-rasm. Oddiy kalkulyator
float accum; //akkumulyator (amallar bajarilish
// natijasi)
int amal; /* amallar:
1 - '+'
2 - '-'
0 - 'bajarish' ('=' tugmasi bosilish)*/
int f; /*f==0 - yangi sonning birinchi raqamini kutish,
masalan, indikatorda natijadan amal
bajarilgandan keyin.
f==1 - qolgan raqamlarni kutish*/
//forma konstruktori
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner) {
f =0; // birinchi raqamni kutish
amal = 0; // keyingi bajarish amali
StaticText1->Caption = 0;
}
// "0" tugmasi
void __fastcall TForm1::Btn0Click(TObject *Sender) {
if (f!=0)
StaticText1->Caption = StaticText1->Caption + "0";
}
// "1" tugmasi
void __fastcall TForm1::Btn1Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "1";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "1";
}
// "2" tugmasi
void __fastcall TForm1::Btn2Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "2";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "2";
}
// "3" tugmasi
void __fastcall TForm1::Btn3Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "3";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "3";
}
// "4" tugmasi
void __fastcall TForm1::Btn4Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "4";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "4";
}
// "5" tugmasi
void __fastcall TForm1::Btn5Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "5";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "5";
}
// "6" tugmasi
void __fastcall TForm1::Btn6Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "6";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "6";
}
// "7" tugmasi
void __fastcall TForm1::Btn7Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "7";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "7";
}
// "8" tugmasi
void __fastcall TForm1::Btn8Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "8";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "8";
}
// "9" tugmasi
void __fastcall TForm1::Btn9Click(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "9";
f = 1;
}
else
StaticText1->Caption = StaticText1->Caption + "9";
}
// "," tugmasi (o'nlik nuqta)
void __fastcall TForm1::BtnvClick(TObject *Sender) {
if (f==0) {
StaticText1->Caption = "0,";
f = 1;
}
else
if (StaticText1->Caption.Pos(",") == 0)
StaticText1->Caption = StaticText1->Caption+",";
}
// "C" tugmasi (o'chirish)
void __fastcall TForm1::BtnCClick(TObject *Sender) {
StaticText1->Caption = "0";
accum = 0;
amal = 0;
f = 0; // birinchi raqamni kutish
}
// "=" tugmasi
void __fastcall TForm1::BtnEClick(TObject *Sender) {
if (f!=0) {
DoAmal(); // amalni bajarish
f = 0; // yangi sonni birinchi raqamini kutish
}
amal = 0;
}
// "+" tugmasi
void __fastcall TForm1::BtnPClick(TObject *Sender) {
/* oldingi amalni bajarish kerak, natija indikatorga chiqariladi, joriy amalni saqlash va yangi sonni kiritish tartibini o'rnatish kerak*/
if (f!=0) {
// indikatorda son bor
DoAmal(); // oldingi amalni bajarish
f = 0; // yansi sonni birinchi raqamni kutish
}
amal =1;
}
// "-" tugmasi
void __fastcall TForm1::BtnMClick(TObject *Sender) {
if (f!=0) {
// indikatorda son bor
DoAmal(); // oldingi amalni bajarish
f = 0; // yansi sonni birinchi raqamni kutish
}
amal = 2; // joriy amalni saqlash
}
// amalni bajarish
void __fastcall TForm1::DoAmal(void) {
/* accum oldingi amal natijasidan tashkil topgan.
Endi amal kodidagi amalni bajarish kerak.
Operand indikatorda joylashgan*/
float amal2 = StrToFloat(StaticText1->Caption);
switch (amal){
case 0 : accum = amal2; break ;
case 1 : accum += amal2; break;
case 2 : accum -= amal2; break;
}
StaticText1->Caption = FloatToStrF(accum,
ffGeneral,6,3);
}
Kalkulyator-2
Kalkulyator-2 dasturi, uning forma va oynasi 1.10-rasmda keltirilgan, dastur bajarilishi vaqtida yoki, ko’pincha aytilganidek, “kodda” komponentalarni yaratish namoyish etilgan. Kalkulyator tugmalari – bu SpeedButton komponentalarni massivga birlashtirishdir. Tugmalarni yaratish va sozlash formaning konstruktorida bajariladi, u yana Click xodisasini qayta ishlash protsedurasini aniqlaydi (beradi). Shunga e’tibor beringki, SpeedButton komponentalar massivlar tavsifi va Click xodisani qayta ishlash funksiyasi forma turi tavsiflangan, sarlavha faylida joylashgan.
1.10-rasm. Kalkulyator-2 dasturining formasi va oymasi
// forma tavsifi - h-fayil bo'lagi
// fayilni bog’lash qismiga qo’lda yoziladi
#include
class TForm1 : public TForm
{
__published: // IDE-managed Components
TStaticText *StaticText1;
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
// ** bu tavsiflar bu yerga qo'lda yoziladi **
TSpeedButton *btn[16]; // tugmalar
// tugmadagi Click xodisasini qayta ishlash funksiyasi
Do'stlaringiz bilan baham: |