Dasturlash asoslari


O‘zgaruvchilarning tavsifiga misollar



Yüklə 2,09 Mb.
səhifə92/111
tarix07.01.2024
ölçüsü2,09 Mb.
#203273
1   ...   88   89   90   91   92   93   94   95   ...   111
Dasturlash asoslari

10.9. O‘zgaruvchilarning tavsifiga misollar

3-misol


IntX butun sonining tavsifi. Uning qiymati Text2 matn maydoniga, natija esa Text2 matn maydoniga kiritiladi. Dasturda quyida tavsif qilingan, qatorni songa va sonni qatorga almashtiradigan funksiya foydalaniladi.
Ikkita belgidan (Label1, Label2), ikkita matn maydonidan (Text1, Text2) va ikkita tugmadan (Command1, Command2) tashkil topgan forma loyihasini yaratamiz.



10.3-rasm



Ob’yekt

Xossa

O‘rnatilgan qiymatlari

Label1

Caption
Font
BackColor

-32768 dan +32767 gacha bo‘lgan intervaldagi butun sonni kiriting

Label2

Caption
Font
BackColor

Natija

Command1

Caption

Chiqish

Command2

Caption

Natijani ko‘rsatish

Text1
Text2

Text

Text xossasi maydonini tozalash

Command2 (Natijani ko‘rsatish) tugmasi bosilgandagi protseduraning kodini yozamiz

10.4-rasm
Loyihani ishga tushiramiz



10.5-rasm

Shunga e’tibor qilish kerakki, ya’ni agar kiritilgan son qiymatlar diapazonidan tashqarida bo‘lsa, Overflow to‘lib ketish xatosi yuz beradi.





10.6-rasm

10.7-rasm

4-misol


Har xil turdagi o’zgaruvchilar va ularning har xil diapazondagi qiymatlarini ishlatish uchun yangi loyiha yaratamiz.





10.8-rasm
Dastur kodi
Option Explicit
Private Sub Command1_Click()
End
End Sub
Private Sub Command2_Click()
Dim intX1 As Integer, lngX2 As Long
Dim sngX3 As Single, dblX4 As Double
Dim binX5 As Boolean, strX6 As String
intX1 = Val(Text1.Text)
lngX2 = Val(Text2.Text)
sngX3 = Val(Text3.Text)
dblX4 = Val(Text4.Text)
binX5 = Text5.Text
strX6 = Text6.Text
Text7.Text = Str(intX1)
Text8.Text = Str(lngX2)
Text9.Text = Str(sngX3)
Text10.Text = Str(dblX4)
Text11.Text = binX5
Text12.Text = strX6
End Sub
Private Sub Form_Load()
End Sub

Dastur kodida quyiq-qiya shaklda operatorlar ajratilib yozilgan.





Ob’yekt

Xossa

O‘rnatilgan qiymatlar

Label1

Caption
Font
BackColor

Integer turidagi butun son

Label2

Caption
Font
BackColor

Long turidagi uzun butun son

Label3

Caption
Font
BackColor

Single turidagi siljuvchi nuqtali son

Label4

Caption
Font
BackColor

Double turidagi siljuvchi nuqtali son

Label5

Caption
Font
BackColor

Boolean turidagi mantiqiy qiymat

Label6

Caption
Font
BackColor

String turidagi qator

Label7

Caption
Font
BackColor

Kiritish maydoni

Label8

Caption
Font
BackColor

Natijalar maydoni

Command1

Caption

Tugatish

Command2

Caption

Natihani ko‘rsatish

Text1
Text2
Text3
Text4
Text5
Text6
Text7
Text8
Text9
Text10
Text11
Text12

Text

Text xossasi maydoni tozalash




10.9-rasm



10.10-rasm


10.10. Asosiy matematik operatorlar


Matematik operatorlar sonlar ustida hisoblashlarni bajarishga imkon beradi.





Operatorlar

Bajariladigan amallar

+

Qo‘shish

-

Ayirish

*

Ko‘paytirish

/

Bo‘lish

\

Butun sonli bo‘lish

mod

Qoldiq

/\

Darajaga oshirish



Ustuvorliklar (Prioritetlar)
Matematik operatorlar ifodalarni kiritish uchun tavsiya qilingan. Ifoda bitta operatorga bog‘lik bo‘lgan o‘zgaruvchilardan, qiymatlardan, funksiyadan tashkil topishi mumkin. Agar ifodada qavslar yo‘q bo‘lsa, operatorlar quyidagi tartib bo‘yicha bajariladi:



  1. Darajaga oshirish.

  2. Ko‘paytirish va bo‘lish.

  3. Butun sonli bo‘lish.

  4. Bo‘linmadan olingan qoldiq.

  5. Qo‘shish va ayirish.

Ifodada hisoblash tartibini o‘zgartirish uchun oddiy qavslar ishlatiladi. Masalan, (d-c*(a-b))/(a+b) formulasida avval a-b amali so‘ng natijani c ga ko‘paytmasi, olingan ko‘paytma d dan ayirmasi, undan keyin esa a+b amali va nihoyat birinchi olingan natija ikkinchisiga bo‘linadi. Ifodadagi qavslar soni chegaralanmagan, lekin shuni esta tutish kerak, ya’ni qavslar – juftli belgilar. Har bir ochilgan qavs uchun o‘zining yopilgan qavsi ham bo‘lishi shart.


Amallarning bajarilishi natijasining turi operandlarning turlariga bog‘lik bo‘ladi. Agar ikkala operand ham butun bo‘lsa, qo‘shish, ayirish, ko‘paytirish va butun sonli darajaga oshirish amallari natijasi butun bo‘ladi. Agar hech bo‘masa bitta (yoki ikkita) operand haqiqiy bo‘lsa, natija haqiqiy bo‘ladi.

5-misol: Asosiy matematik operatorlar ishlatilgan loyiha


Quyidagi turdagi formani yaratingiz:



10.11-rasm



Ob’yekt

Xossa

O‘rnatilgan qiymatlari

Form1

Caption

Matematik amallarni bajarish

Label1

Caption
Font
BackColor

Butun a ni kiriting



Label2

Caption
Font
BackColor

Butun b ni kiriting



Label3

Caption
Font
BackColor

Haqiqiy x ni kiriting





Label4

Caption
Font
BackColor

Haqiqiy y ni kiriting



Label5

Caption
Font
BackColor

BUTUN



Label6

Caption
Font
BackColor

HAQIQIY

Label7

Caption
Font
BackColor

YIG‘INDI

Label8

Caption
Font
BackColor

AYIRMA

Label9

Caption
Font
BackColor

KO‘PAYTMA

Label10

Caption
Font
BackColor

BO‘LINMA

Label11

Caption
Font
BackColor

BUTUN SONLI BO‘LINMA



Label12

Caption
Font
BackColor

QOLDIQ



Label13

Caption
Font
BackColor

SONNING KUBI

Label14

Caption
Font
BackColor

KUBIK ILDIZ

Command1

Caption

Amallar natijasi



Command2

Caption

Yakunlash



Text1, Text2,
Text3, Text4,
Text5, Text6,
Text7, Text8,
Text9, Text10,
Text11, Text12,
Text13, Text14,
Text15, Text16,
Text17, Text18,
Text19, Text20

Text

Text xossasi maydonini tozalash





Dastur kodi

Option Explicit


Private Sub Command1_Click()
Dim inta As Integer, intb As Integer
Dim dblx As Double, dbly As Double
Dim intsum As Integer, intsubstr As Integer
Dim intmult As Integer, intdiv As Integer
Dim intmod As Integer, intsqr As Integer
Dim dblsum As Double, dblsubstr As Double
Dim dblmult As Double, dbldiv As Double
Dim dblsqr As Double, dblsqrt As Double
Dim dbldiv1 As Double, dblsqrt1 As Double
inta = Val(Text1.Text)
intb = Val(Text2.Text)
dblx = Val(Text3.Text)
dbly = Val(Text4.Text)
intsum = inta + intb
intsubstr = inta - intb
intmult = inta * intb
dbldiv1 = inta / intb
intdiv = inta \ intb
intmod = inta Mod intb
intsqr = inta ^ 3
dblsqrt1 = inta ^ (1 / 3)
dblsum = dblx + dbly
dblsubstr = dblx - dbly
dblmult = dblx * dbly
dbldiv = dblx / dbly
dblsqr = dblx ^ 3
dblsqrt = dblx ^ (1 / 3)
Text5.Text = Str(intsum)
Text7.Text = Str(intsubstr)
Text9.Text = Str(intmult)
Text11.Text = Str(dbldiv1)
Text13.Text = Str(intdiv)
Text15.Text = Str(intmod)
Text17.Text = Str(intsqr)
Text19.Text = Str(dblsqrt1)
Text6.Text = Str(dblsum)
Text8.Text = Str(dblsubstr)
Text10.Text = Str(dblmult)
Text12.Text = Str(dbldiv)
Text18.Text = Str(dblsqr)
Text20.Text = Str(dblsqrt)
End Sub

Private Sub Command2_Click()


End
End Sub



10.12-rasm

10.13-rasm


10.11. Visual Basicning matematik funksiyalari
Matematik funksiyalar qiymatlar qaytaradi. Ular o‘zgaruvchiga qiymat o‘zlashtirilganda va ifodalarni hisoblashda ishlatilishi mumkin. n – funksiya argumenti va funksiya tarkibi sonli qiymatga ega bo‘lishi yoki sonli turdagi o‘zgaruvchi bo‘lishi mumkin.



Funksiyalar

Bajaradigan vazifalari

Abs(n)

n ning absolyut qiymatini qaytaradi

Atn(n)

n ning arktangensini radianlarda qaytaradi

Cos(n)

n ning kosinus burchagini qaytaradi. n burchak radianlarda beriladi.


Yüklə 2,09 Mb.

Dostları ilə paylaş:
1   ...   88   89   90   91   92   93   94   95   ...   111




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin