Jalolov Izzatillo
Kalkulyator dasturini yaratishda C++ builder dasturlash muhitining RAD Studio XE3 versiyadan foydalandim. Dastal yangi loyiha yaratamiz va saqlaymiz.
So’ng dastur interfeysini o’lchamlarini to’g’rilab, hususiyatlarini o`zgartirib olamiz. Keyin Border Iconsdan Maximizeni o’chirib, Border Styleni bsSingle, Positionni poScreenCenter qilamiz hamda Formani fontini o`zgartiramiz.
So`ng Panel2 ga SpeedButton tugmalarini joylashtiramiz. O’lchamlarini to’g’rilab so`ng Captionlarini yozamiz hamda Positionni tekkislaymizdan so`ng ushbu holatga keladi.
SpeedButton tugmasiga kod yozamiz yani tugma bosilganda Captioni Label1 Captioniga teng bo`lishi uchun. Sender metodi orqali kod yozilgan tugmani boshqa tugmalarga ham bog’laymiz. O’chirish uchun alohida kod yoziladi SubString funksiya orqali dastlabki hadlarni qirqib olib qolganini ochirib tashlaydi va oxirda son ochirilganda Label1 Captionini 0 ga tenglaydi. Plyus va minus “+/-“ hamda vergul “,” belgilari uchun maxUzunlik va minUzunlik ozgaruvchilarini kiritamiz. Vergul qachon qo’yilish kerak ekanligini shartlar bilan tekshirib keyin qoyamiz. Plyus va minus belgisi qoyishda esa Label1 Captioni qiymatini -1 ga kopaytirib qoysak bo’ldi.
Ushbu kod yozilgandan so`ng ushbu holatga keladi. Amallarni bajarishda qaysi amal bajarilayaotganini bilish uchun qo`shimcha amal degan o’zgaruvchi olamiz. Amalni boshida 0 ga tenglab so`ng amallar bosilgandagi qiymatlarini yozamiz. Ikkita son ustida amallar bajarilgani uchun birinchiSon va ikkinchiSon nomli o’zgaruvchilarni olamiz. Amal uchun yana bir mantiqiy bAmal degan o`zgaruvchi olamiz va uni dastlab “false” qilib olamiz amal bosilganda u “true” ga teng bo’ladi. Amallarni bajarishda har bir amal uchun alohida “case” lardan foydalanamiz. Bu “case” lar amal tanlanib barobar bosilganda ishga tushadi. Har bir amalni ozini “case” ga yozib chiqamiz.
Bo’lishda oldin shart qo`yib ikkinchiSon 0 ga teng emasligini tekshiramiz. Agar 0 ga teng bo’lsa ekranga sonni nolga bo`lib bo’lmaslik xabari chiqadi va funksiya ishdan to’htaydi. Ildiz uchun “sqrt” darajaga ko’tarish uchun esa “pow” funksiyalaridan foydalanamiz.
SpeedButton tugmalarini klaviaturaga bog’lash uchun Key funksiya yordamida bog’lab chiqiladi va klaviatura bosilganda tugmalar ishga tushishi taminlanadi.Klaviatura tugmalari o`zini qiymatlarini tenglab chiqamiz. BackSpace yani ochirish tugmasiga vkBack, Enter tugmasi uchun esa vkReturn kodlarida foydalanamiz.
Dasturning kod qismi:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int amal = 0;
double birinchiSon, ikkinchiSon, natija;
bool bAmal= false, bTeng = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton13Click(TObject *Sender)
{
int maxUzunlik = 8;
if (Label1->Caption.Pos(",") !=0) {
maxUzunlik++;
}
if (Label1->Caption.Pos("-") !=0) {
maxUzunlik++;
}
if(bAmal) {
bAmal = false;
Label1->Caption = (((TSpeedButton *)Sender)->Caption);
} else if (bTeng) {
bTeng = false;
Label1->Caption = (((TSpeedButton *)Sender)->Caption);
}
else if (Label1->Caption == "0") {
Label1->Caption = (((TSpeedButton *)Sender)->Caption);
} else if (Label1->Caption.Length() < maxUzunlik) {
Label1->Caption = Label1->Caption + (((TSpeedButton *)Sender)->Caption);
}
}
//---------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
if (bAmal) {
bAmal = false;
Label1->Caption = "0";
} else{
int minUzunlik = 1;
if (Label1->Caption.Pos("-") != 0) {
minUzunlik++;
}
if (Label1->Caption.Length() == minUzunlik) {
Label1->Caption = "0";
} else {
Label1->Caption = Label1->Caption.SubString(0, Label1->Caption.Length()-1);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
{
if (bAmal) {
bAmal = false;
Label1->Caption="0,";
return;
}
int maxUzunlik = 8;
if (Label1->Caption.Pos(",") !=0) {
maxUzunlik++;
}
if (Label1->Caption.Pos(",") == 0 && Label1->Caption.Length() < maxUzunlik) {
Label1->Caption = Label1->Caption + ",";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
double son = StrToFloat(Label1->Caption) * (-1);
Label1->Caption = FloatToStr(son);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
amal = 1;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
amal = 2;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton20Click(TObject *Sender)
{
amal = 3;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton24Click(TObject *Sender)
{
amal = 4;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
{
if (amal != 0) {
ikkinchiSon = StrToFloat(Label1->Caption);
switch(amal){
case 1:
natija =birinchiSon + ikkinchiSon ;
break;
case 2:
natija =birinchiSon - ikkinchiSon ;
break;
case 3:
natija =birinchiSon * ikkinchiSon ;
break;
case 4:
if ( ikkinchiSon == 0) {
ShowMessage("Sonni nolga bo`lib bo`lmaydi");
amal = 0;
return;
} else{
natija = birinchiSon / ikkinchiSon;
}
break;
case 5:
natija =Sqrt(birinchiSon);
break;
case 6:
natija =pow(birinchiSon , 2);
break;
default:
Label1->Caption = FloatToStr(natija);
amal=0;
}
Label1->Caption = FloatToStr(natija);
amal=0;
bTeng = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
Label1->Caption = "0";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
Label1->Caption = 0;
amal = 0;
bAmal = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton21Click(TObject *Sender)
{
Label1->Caption = FloatToStr( 1 / StrToFloat(Label1->Caption)) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
Label1->Caption = FloatToStr(StrToFloat(Label1->Caption) / 100);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton23Click(TObject *Sender)
{
amal = 5;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton22Click(TObject *Sender)
{
amal = 6;
bAmal = true;
birinchiSon = StrToFloat(Label1->Caption);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
{
if (Key >= '0' && Key <= '9' ) {
int maxUzunlik = 8;
if (Label1->Caption.Pos(",") !=0) {
maxUzunlik++;
}
if (Label1->Caption.Pos("-") !=0) {
maxUzunlik++;
}
if(bAmal) {
bAmal = false;
Label1->Caption = Key;
} else if (bTeng) {
bTeng = false;
Label1->Caption = Key;
}
else if (Label1->Caption == "0") {
Label1->Caption = Key;
} else if (Label1->Caption.Length() < maxUzunlik) {
Label1->Caption = Label1->Caption + Key;
}
} else if ( Key == '+' ) {
SpeedButton16->Click();
} else if ( Key == '-' ) {
SpeedButton8->Click();
} else if ( Key == '*' ) {
SpeedButton20->Click();
} else if ( Key == '/' ) {
SpeedButton24->Click();
} else if ( Key == vkBack ) {
SpeedButton4->Click();
} else if ( Key == vkReturn || Key == '=' ) {
SpeedButton12->Click();
}
else if ( Key == '.' || Key == ',' ){
SpeedButton11->Click();
}
}
//---------------------------------------------------------------------------
Do'stlaringiz bilan baham: |