Uploaded by Lochinbek Qushaqov

Lochinbek

advertisement
711-22-Guruh talabasi Qo’shaqov Lochinbek
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 <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <string>
//--------------------------------------------------------------------------#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int position = 1;
//1 - qo'shish, 2-ayrish, 3-ko'paytirish, 4-bo'lish
float number_one,number_two, output = 0;
String number_length;
//--------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "1";
}
else{
Label1->Caption = Label1->Caption + "1";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "2";
}
else{
Label1->Caption = Label1->Caption + "2";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button8Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "3";
}
else{
Label1->Caption = Label1->Caption + "3";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "4";
}
else{
Label1->Caption = Label1->Caption + "4";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button11Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "5";
}
else{
Label1->Caption = Label1->Caption + "5";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button12Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "6";
}
else{
Label1->Caption = Label1->Caption + "6";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button14Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "7";
}
else{
Label1->Caption = Label1->Caption + "7";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button15Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "8";
}
else{
Label1->Caption = Label1->Caption + "8";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button16Click(TObject *Sender)
{
if(Label1->Caption == "0"){
Label1->Caption = "9";
}
else{
Label1->Caption = Label1->Caption + "9";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Label1->Caption != "0"){
Label1->Caption = Label1->Caption + "0";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button5Click(TObject *Sender)
{
position = 1;
number_one = Label1->Caption.ToDouble();
Label1->Caption = "0";
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button9Click(TObject *Sender)
{
position = 2;
number_one = Label1->Caption.ToDouble();
Label1->Caption = "0";
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button13Click(TObject *Sender)
{
position = 3;
number_one = Label1->Caption.ToDouble();
Label1->Caption = "0";
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button17Click(TObject *Sender)
{
position = 4;
number_one = Label1->Caption.ToDouble();
Label1->Caption = "0";
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button4Click(TObject *Sender)
{
int a;
number_two = Label1->Caption.ToDouble();
switch(position){
case 1 : output = number_one + number_two; break;
case 2 : output = number_one - number_two; break;
case 3 : output = number_one * number_two; break;
case 4 : output = number_one / number_two; break;
}
number_length = FloatToStr(output);
a = number_length.Length();
if(a >9){
Label1->Caption = number_length.Delete(9,a-9);
}
else{
Label1->Caption = output;
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button20Click(TObject *Sender)
{
Label1->Caption = "0";
number_one = 0;
number_two = 0;
position = 1;
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button18Click(TObject *Sender)
{
int length = Label1->Caption.Length();
if(length != 1){
Label1->Caption = Label1->Caption.Delete(length,length-1);
}
else{
Label1->Caption = "0";
}
}
//--------------------------------------------------------------------------void __fastcall TForm1::Button3Click(TObject *Sender)
{
Label1->Caption = Label1->Caption + ",";
}
//---------------------------------------------------------------------------
Dastur h fayli (dizayni) kodi :
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//--------------------------------------------------------------------------#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//--------------------------------------------------------------------------class TForm1 : public TForm
{
__published:
// IDE-managed Components
TLabel *Label1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TButton *Button5;
TButton *Button6;
TButton *Button7;
TButton *Button8;
TButton *Button9;
TButton *Button10;
TButton *Button11;
TButton *Button12;
TButton *Button13;
TButton *Button14;
TButton *Button15;
TButton *Button16;
TButton *Button17;
TButton *Button18;
TButton *Button20;
void __fastcall Button6Click(TObject *Sender);
void __fastcall Button7Click(TObject *Sender);
void __fastcall Button8Click(TObject *Sender);
void __fastcall Button10Click(TObject *Sender);
void __fastcall Button11Click(TObject *Sender);
void __fastcall Button12Click(TObject *Sender);
void __fastcall Button14Click(TObject *Sender);
void __fastcall Button15Click(TObject *Sender);
void __fastcall Button16Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
void __fastcall Button9Click(TObject *Sender);
void __fastcall Button13Click(TObject *Sender);
void __fastcall Button17Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button20Click(TObject *Sender);
void __fastcall Button18Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private:
public:
// User declarations
// User declarations
__fastcall TForm1(TComponent* Owner);
};
//--------------------------------------------------------------------------extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------#endif
Download