VISUAL BASIC 0.1
1
1. 2.
Visual Basic 5.0 ……………. 3 .
:
Visual Basic.
3.
…….. 10
Visual Basic ........................................ 13
4.
For…Next If…Then
5.
Do…Loop ......................................................................... 26
6.
(Strings) ................................................................ 33
7.
(Arrays) .......................................................... 40
8.
........... 46
SelectCase ............................................ 20
1
2006-2007
2007-2008
2
Visual Basic 5.0 Visual Basic 5.0
Visual Basic 5.0
Programs
!
Start Microsoft Visual Basic 5.0
" #
$ &'
(
&'
Microsoft Windows Visual Basic 5.0 CCE
Visual Basic 5.0 Visual Basic 5.0 CCE % New Project " Standard EXE"
&' $
Open Visual Basic
%
)
3
Visual Basic
** )
Basic
Visual &' Visual
!
)
Basic
( +
(
Menubar" , Windows' -
( % )
File, Edit, View
( (
Toolbar" (
)
.
( " View
( Toolbar
% Toolbox" ( Text Box"
! Command Button"
Standard Image"
Label" Scroll Bar"
+
" Toolbox Toolbox
View ! (
"
&'
Form Window"
Object
% Project Explorer Window" &' /
Code
+
View Object
4
View %
"
View
0
&'
!
.vbp"
&'
" .frm"
.bas"
%
%
&'
% +
% Project Explorer
" View
Explorer
Properties Window" + "
!
%
Properties Window Properties Window
View !
Form Layout Window" ( &'
%
Visual Basic
"
(
) +
%
&' %
!
+
%
Visual Basic
"
$
%
View Code
.
%
) Form
% (
!
% (
View Object"
(
5
+
Form Layout Window Form Layout
View
Window
*1
Project
General
Code
- ( Load Form_Load
%
%
(
Procedure"
Private Sub Form_Load() End Sub , .
% (
Activate
&'
!
(
% %
+
!
2
%
Form_Activate
( 2"
a=2 b=3 c=a+b Print c $
%
+ -
F5
( 1 3
&' Run 4#
Start Form1"
! ( 6
Start (
Print
Print
& %
+
&' Close Run
!
End
End
$ Visual Basic
% (
23 − 9 3 ⋅ 7 45 : 5 2
5
(
+
16 − (2 + 5) (12 − 7) ⋅ (4 + 3) 12 − 72 : 3 ⋅ 2 4 3 ⋅ 2 − 3 4 ⋅ 5 ( ( % ( * / ^
, % 25
$
Visual Basic
( 2^5"
&'
5
Visual Basic -
(
&' &' " 1 + 2 + 2 2 + 23 +
#
New Project 6
+ 219
File ( No $
) &' ) OK
Standard EXE" $
&'
%
*1
3
% Form_Activate
%
Private Sub Form_Activate() End Sub .
1:
S=1' i=1 i=i+1 S = S + 2 ^ (i - 1) If i < 20 Then GoTo 1 Print S ' -
20Visual Basic
( %
7
%
&' *7894:4
!
%
$
5 (
S = S + 2^ (i − 1) 1
i
, . i=2 i=3
17
( S = 1 + 2^1 = 3 ( S = 3 + 2^3 = 7
i = 20
( S = 524287 + 2^19 = 1048575
;
&'
Project1.vbp
&'
File
$
Create New Folder
( &'
Form1.frm
&
Save Project As…
"
(
4
Suma (
Save &'
Remove Project
( -
! (
*3
&' Visual Basic #
(
-
File %
+
.
%
)
&'
Visual Basic
&' &'
Remove Project
8
File
&' (
"
*777
&'
13 1> 3* 3: 8* 83 8: ( ( ? 1.2
10
%
47 +
4 47 VB'
VB'
;
Save Project As…
"
File
VB'
& (
VB'
( $
(
Create New Folder
(
VB'
Save Primer 2 (
(
Form1.frm
VB'
)
n
( +
Project1.vbp
n 6 (
! %
4
11
%
(
Form_Activate
%
n = Val(InputBox("Vnesi priroden broj n, n=")) Print "Site prosti broevi do brojot "; n br = 0 For m = 1 To n b=0 For j = 1 To m If m Mod j = 0 Then b = b + 1 Next j If b = 2 Then Print m, br = br + 1 If br = 5 Then Print br = 0 End If End If Next m ( (
( 2
2
*1'
; Save Form1.frm As…
x2 x1 = x2
x1 < x2 x2 ≠ 0
x1 / x2 , x1
.
x2
.
*
% x1
. ! .
,
% *
:
x1 < x2
Dim x1, x2, y As Double Private Sub Form_Activate() x1 = Val(InputBox("x1=")) x2 = Val(InputBox("x2=")) Print "Za x1="; x1; " i x2="; x2; ", "; If x1 > x2 Then y = x1 + x2 Print "y=x1+x2="; y ElseIf x1 = x2 Then y = x1 * x2 Print "y=x1*x2="; y ElseIf x1 < x2 And x2 0 Then y = x1 / x2 Print "y=x1/x2="; y Else Print "y ne e definiran." End If End Sub ! " # 6.
23
x2 ,
If…Then. $ y x2 = 0 ,
y
,
a b
! " # 7.
. +
d
c d
,
.
,
(
(
End). .
.
b, d ≠ 0 .
a, b, c, d
*
a, b, c, d , MsgBox)
, ,
,
, br = ad + bc
a b
b
+ c = ad + bc , d
bd
im = bd
!
%
1.
,
(zd),
1,
,
:
,
Dim a, b, c, d, br, im, nzd, zd, br1, im1 As Integer Private Sub Form_Activate() a = Val(InputBox("a=")) b = Val(InputBox("b0, b=")) c = Val(InputBox("c=")) d = Val(InputBox("d0, d=")) If b = 0 Or d = 0 Then MsgBox ("Ne moze imenitelot na dropka da e nula") End End If Print a; "/"; b; "+"; c; "/"; d; "="; br = a * d + b * c im = b * d Print br; "/"; im; If br < im Then nzd = br Else nzd = im For zd = nzd To 1 Step -1 If br Mod zd = 0 And im Mod zd = 0 Then Exit For Next zd nzd = zd If zd 1 Then br1 = br / nzd im1 = im / nzd Print "="; br1; "/"; im1 End If End Sub ! " # 7.
a=3, b=5, c=2, d=8,
+
:
24
,
,
a=7, b=3, c=2, d=5,
,
:
,
+
, $ SelectCase
/ Select Case [Case
... [Case
[
[ [Case Else [ End Select . 1) 2)
-
( )
.
VISUAL BASIC. :
( ) -1]]
-1
( ) -n]]
-n
]] :
% %
(
-
, Case 2, 6, 8
-1 -2,
Case Is= 1 Then Do Print n n=n+1 Loop While n max Then max = a(i) indexmax = i End If Next i Print " "; min; " indexmin; "," Print " "; max; " indexmax; "." End Sub
, n="))
"; _ "; _
! " # 15. +
7-
8, -4, 2, 1.5, -5.5, 10, 1 41
(
7,
.
),
%
:
,
.
. ! " # 16. )
(
. ."
+
.
,
,
. . (
i.
15,
% .
n-
,
i-
i-
i(
, ),
:
Dim a() As Double Dim b() As Double Dim n, i, j, indexmin As Integer Dim min, pb As Double Private Sub Form_Activate() n = Val(InputBox(" ReDim a(1 To n) As Double ReDim b(1 To n) As Double Print " " For i = 1 To n a(i) = Val(InputBox("a(" & i & ")=")) b(i) = a(i) Print a(i); ", "; Next i Print For i = 1 To n min = b(i): indexmin = i For j = i + 1 To n If b(j) < min Then min = b(j) indexmin = j End If Next j pb = b(i): b(i) = min: b(indexmin) = pb Next i Print " " For i = 1 To n Print b(i); ", "; Next i End Sub
! " # 16. 42
. +
n min
, n="))
b
/ b
.
%
b(indexmin)
7-
:
+
n ). +
b
,
min
a, pb b(i)
ib(indexmin).
, b(i)
i-
pb, (
, (
( %
.
,
, –
&
. ! " # 17. "
10
a(1), a(2), …, a(n).
( 2
5
.$
XXXIII-1
nzd.
#)
Dim a() As Integer Dim n, i, p, nzd, min As Integer Private Sub Form_Activate() n = Val(InputBox(" ReDim a(1 To n) As Integer Print " ("; For i = 1 To n a(i) = Val(InputBox("a(" & i & ")=")) Print a(i); ", "; Next i min = a(1) For i = 2 To n If a(i) < min Then min = a(i) Next i nzd = min p = 1 Do While p = 1 p = 0 For i = 1 To n If a(i) Mod nzd 0 Then p = 1 Next i If p = 1 Then nzd = nzd - 1 End If Loop Print ")="; nzd End Sub
! " # 17.
43
.
%
nzd a(1), a(2), …, a(n) :
.
n
nzd.
, n="))
p
"
1
a(1), a(2), …, a(n) Do…Loop
. +
nzd. +
(
"
2 ! " # 18.
n
n
)
"p
%
, *
0
72,
,
.
1
?
..
n
1
n
. – 194 ,
. . ) *
) %
. .
6 *
"
60, 24, 144
,
,
". $
. a(1), a(2), …, a(n) a(1), a(2), …, a(n). 2 %
XXXII-2
(276 . . , , "(
% nzd
4-
:
%
6 "
"
%
, .
6
)
, % ). "
3 5 n ), .
n(
2 2
2. "
5, 5. .
. . 2 3
1 3.
[ n] (
n ,
% 4 " [ 50 ] = [7,07106781...] = 7 , 2, 3, 5 7 , 5" " . , : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 35, 27, 41, 43 47.
44
50.
0
.#
" 50
(
0
"
"
, ).
:
Dim a() As Integer Dim n, i, j, br As Integer Private Sub Form_Activate() n = Val(InputBox(" n=")) ReDim a(2 To n) As Integer For i = 2 To n a(i) = i Next i For i = 2 To Int(Sqr(n)) If a(i) 0 Then For j = i + 1 To n If a(j) Mod a(i) = 0 Then a(j) = 0 Next j End If Next i Print "