19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

60 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 4<br />

The following program shows how Int is used to carry out long division. When the integer m is divided<br />

into the integer n with long division, the result is a quotient <strong>and</strong> a remainder. See Figure 2-29.<br />

FIGURE 2-29 Long Division<br />

Private Sub cmdDivide_Click()<br />

Dim divisor As Single, dividend As Single<br />

Dim quotient As Single, remainder As Single<br />

‘Long division<br />

picResult.Cls<br />

divisor = Val(txtDivisor.Text)<br />

dividend = Val(txtDividend.Text)<br />

quotient = Int(dividend / divisor)<br />

remainder = dividend - quotient * divisor<br />

picResult.Print “The quotient is”; quotient<br />

picResult.Print “The remainder is”; remainder<br />

End Sub<br />

[Run, type 14 <strong>and</strong> 256 into the text boxes, <strong>and</strong> then click the comm<strong>and</strong> button.]<br />

■ STRING FUNCTIONS: LEFT, MID, RIGHT, UCASE,TRIM<br />

The functions Left, Mid, <strong>and</strong> Right are used to extract characters from the left end, middle, <strong>and</strong><br />

right end of a string. Suppose str is a string <strong>and</strong> m <strong>and</strong> n are positive integers. Then Left(str,<br />

n) is the string consisting of the first n characters of str <strong>and</strong> Right(str, n) is the string consisting<br />

of the last n characters of str. Mid(str, m, n) is the string consisting of n characters of str,<br />

beginning with the mth character. UCase(str) is the string str with all of its lowercase letters<br />

capitalized. Trim(str) is the string str with all leading <strong>and</strong> trailing spaces removed. Some examples<br />

are as follows:<br />

Left(“fanatic”, 3) is “fan”. Right(“fanatic”, 3) is “tic”.<br />

Left(“12/15/99”, 2) is ”12”. Right(“12/15/99”, 2) is ”99”.<br />

Mid(“fanatic”, 5, 1) is “t”. Mid(“12/15/99”, 4, 2) is ”15”.<br />

UCase(“Disk”) is “DISK”. UCase(“12three”) is “12THREE”.<br />

Trim(“ 1 2 ”) is “1 2”. Trim(”-12 “) is ”-12”.<br />

The strings produced by Left, Mid, <strong>and</strong> Right are referred to as substrings of the strings<br />

from which they were formed. For instance, “fan” <strong>and</strong> “t” are substrings of “fanatic”. The<br />

substring “fan” is said to begin at position 1 of “fanatic” <strong>and</strong> the substring “t” is said to begin<br />

at position 5.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!