{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chap08 - QBASIC Chapter 8 QBASIC Chapter 8 More on Strings...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QBASIC Chapter 8 QBASIC Chapter 8 More on Strings and Functions Created by Richard D. Taylor Illustrated by Saundra J. Taylor Introduction Introduction ­ Numeric functions to handle common Numeric mathematical calculations mathematical ­ User-defined function - a function defined by User-defined the user, numeric or string, can be created to perform a task often needed by the programmer perform ­ System Event Trapping - the user pressing one System of the function keys transfers control to an event-assigned subroutine event-assigned String Functions and Statements String Functions and Statements ­ Concatenation, Substrings, and Character Concatenation, Counting Revisited - +, LEN, LEFT$, RIGHT$, MID$ MID$ ­Concatenation - joins two strings to form a new Concatenation string string ­LEN function - returns the length of the argument String Functions and Statements String Functions and Statements ­LEFT$ - extracts a substring starting with the LEFT$ leftmost character (position 1) of the string X$ leftmost ­RIGHT $ - extracts a substring starting with the RIGHT rightmost character of the string argument X$ rightmost ­MID$ - extracts a substring beginning with the MID$ character in position P of X$ character String Functions and Statements String Functions and Statements ­ Substring Searching and Replacement Substring INSTR Function and MID$ Statement INSTR ­INSTR function to search a string argument for INSTR a particular substring - the search begins at position P of X$ position String Functions and Statements String Functions and Statements ­ Form: MID$(X$, P, N) - S$ ­X$ is the string in which the replacement takes X$ place place ­P is the position at which the replacement begins ­N is the number of characters to replace ­S$ is the replacement substring ­ Purpose: to replace a substring within a string String Functions and Statements String Functions and Statements ­ Examples ­MID$(Phrase$, 3, 4) = Substr$ ­MID$(Word1$, 1, 5) = “Y” ­MID$(Wd1$, 30, 2) = “abcde” ­MID$(E$, 4, 5) = A$ + B$ String Functions and Statements String Functions and Statements ­ String Functions and StatementsConverting String Character Codes - ASC and CHR$ Character ­ASC and CHR$ functions facilitate the ASC manipulation of individual characters manipulation ­ASC (X$) function returns a two-digit numeric ASC value that corresponds to the ASCII code for the first character of the string argument X$ first ­PRINT ASC (“C”) ­67 String Functions and Statements ­ CHR$(N) can be described as the reverse of CHR$(N) the ASC function - it returns a single string character equivalent in ASCII code to the numeric argument N numeric ­PRINT CHR$(66) ­B String Functions and Statements String Functions and Statements ­ Changing Case - LCASE$ and UCASE$ ­functions are used to convert alphabetic functions characters in a string expression to lowercase or uppercase uppercase String Functions and Statements String Functions and Statements ­ Modifying Data Types - STR$ and Val ­The PC cannot add a string value to a numeric value ­The STR$(N) function returns the string equivalent The of the numeric value N of STR$(52.3) returns “52.3” ­ VAL(“310.23”) returns 310.23 ­ ­VAL (X$) returns the numeric equivalent of the VAL string X$ string String Functions and Statements String Functions and Statements ­ Duplicating Strings - SPACE$ and Duplicating STRING$ STRING$ ­Functions used to duplicate string data ­SPACE$(N) function returns N spaces or blank SPACE$(N) characters characters ­ PRINT “DEC”; SPC(4); “Micro VAX” ­ PRINT “DEC”; SPACE$(4); “Micro Vax” ­The advantage of the SPACE$ function over the SPC The function is that SPACE$ may be used in statements other than the PRINT or LPRINT statements other String Functions and Statements String Functions and Statements ­ Duplicating Strings - SPACE$ and Duplicating STRING$ STRING$ ­The STRING$(N, X$) function returns N times The the first character of the string X$ The STRINGS function may be used to duplicate any character any ­ PRINT STRINGS (72,*) displays a line of 72 PRINT asterisks asterisks String Functions and Statements String Functions and Statements ­ Trimming Blank Characters - LTRIM$ and Trimming RTRIM$ RTRIM$ ­LTRIM$ and RTRIM$ functions remove LTRIM$ leading or trailing blank characters from a string expression string ­LTRIM$(X$) returns X$ with leading (left) LTRIM$(X$) blank characters removed blank ­RTRIM$(X$) return X$ with trailing (right) RTRIM$(X$) blank characters removed blank String Functions and Statements String Functions and Statements ­ Accessing the System Time and Date - DATE$ Accessing and TIME$ and ­DATE$ is equal to the current system date as a string DATE$ value in the form mm-dd-yyyy value ­TIME$ is equal to the system’s time of day, in 24-hour TIME$ notation, as a string value in the form hh:mmm:ss notation, ­ The DATE$ and TIME$ functions are The frequently used to display the date and time as part of report headings part String Functions and Statements String Functions and Statements ­ Setting the Time and Date - The DATE$ and Setting TIME$ Statements TIME$ ­ Form: DATE$ = string expression ­ Purpose: to set the system date ­ Examples ­DATE$ = “07-06--96” ­DATE$ = “7/6/1996” ­DATE$ = “06-22-2005” String Functions and Statements String Functions and Statements ­ Form: TIMES$ = string expression ­ Purpose: to set the system time ­ Examples: ­TIME$ = “10” ­TIME$ = “1.23” ­TIME$ = “20:00:23” String Functions and Statements String Functions and Statements ­ Accepting String Data - The LINE INPUT Accepting Statement and the INKEY$ and INPUT$ Functions Functions ­The LINE INPUT statement accepts a line The entered from the keyboard as a string value and assigns it to a specified variable assigns ­The LINE INPUT statement ignores the usual The delimiters, namely, the quotation mark and comma comma String Functions and Statements String Functions and Statements ­ Form: Form: LINE INPUT string variable LINE LINE INPUT “input prompt message”; LINE string variable string ­ LINE INPUT #n, string variable String Functions and Statements String Functions and Statements ­ Purpose: Purpose: Provides for the assignments to a string variable of an entire line (up to 255 characters), including commas and quotation marks, entered from an external source such as the keyboard or auxiliary storage storage ­ Examples ­LINE INPUT Cus.Rec$ ­LINE INPUT “What? “; Stat$ String Functions and Statements String Functions and Statements ­ Major difference between the LINE INPUT Major statement and the INPUT statement: statement ­The LINE INPUT statement does not The automatically prompt the user with the question mark and trailing space, as the INPUT statement does statement ­The LINE INPUT statement can accept data for The only one string variable The INPUT statement can have more than one variable in the list can String Functions and Statements String Functions and Statements ­ INKEY$ function does not suspend INKEY$ execution of the program; instead, it checks the keyboard to determine whether a character is pending, that is, whether a key was pressed since the last time it executed an expression with INKEY$ or since the beginning of the program beginning Numeric Functions Numeric Functions ­ Arithmetic Functions - ABS, FIX, INT, Arithmetic CINT, and SGN CINT, ­ABS Function - takes any numeric expression ABS and returns its positive value and ­ ABS (N) equals 4 where N equaled -4 Numeric Functions Numeric Functions ­FIX (N) Function - returns the truncated integer FIX portion of the argument N - when the argument is positive, the FIX function is identical to the INT function function ­ FIX (N) equals -4 and INT (N) returns -5 where N = -4.45 ­CINT (N) Function - returns an integer, returns the CINT rounded integer portion of the argument rounded ­SGN (N) function returns a value of + 1 if the SGN argument N is positive, 0 if the argument is 0, and - 1 if the argument N is negative if Numeric Functions Numeric Functions ­ Generalized Procedures for Rounding and Generalized Truncation Truncation ­CINT and FIX functions may be used to write CINT generalized expressions for rounding or truncating a number to any decimal place truncating Numeric Functions Numeric Functions ­ Exponential Functions - SQR, EXP, and LOG ­SQR (N) function (square root) - computes the square root SQR of the argument N of ­EXP function (exponential) - used to represent this EXP constant, which is raised to the power given as the argument in parentheses following the function name argument ­LOG function (logarithmic) - used to determine the LOG logarithm to the base 10 by multiplying the LOG function by 0.434295 by Numeric Functions Numeric Functions ­ Trigonometric Functions - SIN, COS, TAN, and Trigonometric ATN ATN ­Radians - angle X expression ­1 radian = 180 / π Degrees = 180 / 3.141593 radian degrees degrees ­1 degree = π / 180 radians = 3.141593 / 180 degree radians radians Numeric Functions Numeric Functions ­ When using these three functions, remember that if the When argument is in units of degrees it must first be multiplied by 3.141593 /180 to convert it into units of radians before the function can evaluate it radians To find the Consecant Secant Cotangent Use 1 / SIN (X) 1 / COS (X) 1 / TAN (X) Numeric Functions Numeric Functions Utility Functions - POS, CSRLIN, and SCREEN POS Function - returns the current column POS position of the cursor relative to the left edge of the display screen of CSRLIN Function - equal to the current row CSRLIN (line) the cursor is relative to the top of the display screen display SCREEN function - allows you to determine SCREEN Numeric Functions Numeric Functions Performance Testing - The TIMER Function TIMER function - returns a singleprecision numeric value representing the precision number of seconds that have elapsed since midnight since Benchmarking - the activity of comparing Benchmarking the performance of algorithms or applications that are running under similar conditions on one or more Numeric Functions Numeric Functions Random Number Function and the RANDOMIZE Random STATEMENT STATEMENT RND Function - returns an unpredictable RND decimal fraction number between 0 (inclusive) and 1 (exclusive) (inclusive) Pseudo-Random Numbers - random numbers Pseudo-Random generated by the PC are provided by a repeatable process repeatable Numeric Functions Numeric Functions Random Number Function and the RANDOMIZE Random STATEMENT STATEMENT INT and RND function - can be combined to INT create random digits over a specified range create š – ™ œ Ÿ Numeric Functions Numeric Functions ­ Form: Randomize ­ Randomize numeric expression ­ Purpose: Purpose: To supply a new seed for the generation of random numbers by the RND function function ­ Examples: ­RANDOMIZE ­RANDOMIZE 396.5 User­Defined Functions User­Defined Functions ­ User-Defined Function - allows you to User-Defined define new string or numeric functions that relate to a particular application relate ­Function Parameter - parentheses following the Function name of the function surround a simple variable name ­ Form: DEF FNx(P1…Px) = expression User­Defined Functions User­Defined Functions ­ Purpose: To define a function that is Purpose: relevant to a particular application that can be referenced as frequently as needed in the program in which it is defined program ­ Examples: ­DEF FNCUBE(Y, Z) = Y ^ 3 + 2 ^ 3 ­DEF FNPI = 3.141593 DEF User­Defined Functions User­Defined Functions ­ Dummy variables - parameters in a user- defined function ­ Referencing User-Defined Functions ­referenced in the same way as numeric referenced functions functions Trapping Events Trapping Events ­ Some applications require that the PC halt Some execution of a routine and execute a subroutine when a specific event has occurred occurred Summary Slide Summary Slide ­ Introduction ­ String Functions and Statements ­ Numeric Functions ­ User-Defined Functions ­ Trapping Events ...
View Full Document

{[ snackBarMessage ]}