Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 8: Crunching Numbers and Playing with Strings 101

PROMPT “Type in a number”; AnyNumber

PRINT “The ABS value = “; ABS(AnyNumber)

PRINT “The ACS value = “; ACS(AnyNumber)

PRINT “The ASN value = “; ASN(AnyNumber)

PRINT “The ATN value = “; ATN(AnyNumber)

PRINT “The COS value = “; COS(AnyNumber)

PRINT “The EXP value = “; EXP(AnyNumber)

PRINT “The INT value = “; INT(AnyNumber)

PRINT “The LOG value = “; LOG(ABS(AnyNumber))

PRINT “The SIN value = “; SIN(AnyNumber)

PRINT “The SQR value = “; SQR(AnyNumber)

PRINT “The TAN value = “; TAN(AnyNumber)

PRINT

END

You can use only positive numbers with the LOG function or in calculating a square root.

The arcsine and arccosine functions can accept only a number between 0 and 1.0. If you choose a higher or lower number, neither function works, and Liberty BASIC displays an error message.

Manipulating Strings

Besides manipulating numbers, computers can also manipulate strings. A string is anything that you can type from the keyboard, including letters, symbols (such as #, &, and +), and numbers.

In Liberty BASIC, a string is anything that appears inside quotation marks, as in the following example:

PRINT “Everything enclosed in quotation marks”

PRINT “is a string, including the numbers below:”

PRINT “72 = 9 * 8”

PRINT “You can even mix letters and numbers like this:”

PRINT “I made $4,500 last month and still feel broke.”

END

In the preceding program, the formula 72 = 9 * 8 is actually a string, even though it consists of numbers. That’s because Liberty BASIC treats anything inside quotation marks as a string, including any numbers inside of quotation marks.

102 Part II: Learning Programming with Liberty BASIC

How C/C++ handles strings

Unlike BASIC (and many other languages such as Pascal and Java), the C/C++ language doesn’t use a string data type. Instead, C/C++ programs use a more primitive data type known as a character (abbreviated char).

A character data type can hold only one character (such as a letter, symbol, or number), so to manipulate strings, C/C++ programs must use an array of characters. (Don’t worry. Read more about arrays in Chapter 16. The important thing right now is to realize that C/C++ programs must handle strings differently from BASIC.)

Just to give you an idea of how C/C++ programs handle strings, look at the following program. In

this example, this C program defines the variable myrelative and defines it as an array that can hold 10 characters:

main ()

{

char myrelative[10]; printf (“Type the name of a

male relative you hate.\n”);

scanf (“%s”, &myrelative); printf (“%s”, myrelative); printf (“ says he doesn’t like

you either!”);

}

Declaring variables as strings

As with numbers, you can use strings directly in your program, as follows:

PRINT “Print me.”

PRINT 54

END

Just as with numbers, you may want to store strings in variables so you can reuse that particular string over and over again by typing just the variable rather than the entire string. That way, your program can receive a string, store the string in a variable, and then manipulate the string to create a useful result, such as displaying a message to the user on-screen.

As you create a variable, you must tell Liberty BASIC, “Hey, I want this variable to hold only strings!” In technical terms, you’re declaring a string as a string data type. To create a variable to hold a string, Liberty BASIC enables you to create a variable and add the dollar sign ($) to the end of the variable name, as in the following example:

StringVariable$ = “This variable can hold only strings.”

Chapter 8: Crunching Numbers and Playing with Strings 103

If you fail to declare a variable as a string data type, but you still try to stuff a string into the variable, Liberty BASIC displays an error message and prevents your program from running. By ensuring that you store the correct data in variables, compilers such as Liberty BASIC try to make sure that you write programs that won’t have any unexpected bugs.

Smashing strings together

Unlike with numbers, you can’t subtract, divide, or multiply strings. But you can add strings (which is technically known as concatenating strings). To concatenate two strings, you use the plus sign (+) to essentially smash two strings into a single string, as the following example shows:

PROMPT “What is your name?”; MyName$

PRINT “Hello, “ + MyName$ + “. Isn’t it time to take”

PRINT “an early lunch from your job now?”

END

This Liberty BASIC program tells the computer to do the following:

1.The first line tells the computer to print the message What is your name? on-screen and then wait for the user to type something. Liberty BASIC stuffs whatever the user types into the string variable MyName$.

2.The second line tells the computer to create one big string that uses the string it’s storing in the MyName$ variable. If it’s storing the name “Tasha” in the MyName$ variable, the third line prints, Hello, Tasha. Isn’t it time to take.

3. The third line prints, an early lunch from your job now?.

4. The fourth line tells the computer that the program is at an end.

If you concatenate strings, make sure that you leave a space between the two strings so that they don’t appear smashed together (likethis). In the previous example, notice the space in the second line following the word Hello and the comma.

Playing with Liberty BASIC’s

String Functions

If just storing and concatenating strings were all that you could do, Liberty BASIC may seem pretty boring to you. That’s why Liberty BASIC includes a bunch of built-in functions to give you all sorts of fun ways to manipulate strings.

104 Part II: Learning Programming with Liberty BASIC

Playing with UPPERCASE and lowercase

If strings consist of letters, they can appear in the following three ways:

in all lowercase, like this

IN ALL UPPERCASE, LIKE THIS (WHICH CAN LOOK ANNOYING AFTER A WHILE)

As a mix of UPPERCASE and lowercase letters, Like This

To convert every character in a string to lowercase, you can use the special function LOWER$. To convert every character in a string to uppercase, you can use another function — UPPER$.

Both functions take a string and convert it to either uppercase or lowercase, as in the following example:

UPPER$(“hello”) ‘ HELLO

LOWER$(“GOOD-BYE”) ‘ good-bye

Run the following program to see how these two functions work:

PROMPT “What would you like to convert”; ThisString$

PRINT “This is what happens when you use LOWER$:”

PRINT LOWER$(ThisString$)

PRINT

PRINT “This is what happens when you use UPPER$:”

PRINT UPPER$(ThisString$)

END

If you run this program and type in the string I Don’t Like Anyone Who

Copies My Work, the program displays the following:

This is what happens when you use LOWER$: i don’t like anyone who copies my work

This is what happens when you use UPPER$:

I DON’T LIKE ANYONE WHO COPIES MY WORK

Both the LOWER$ and UPPER$ functions work only with letters. They don’t do anything with symbols (such as $, %, or @) or numbers.

Counting the length of a string

If you need to manipulate strings, you may want to know the actual length of a string. To count the number of characters in a string, you can use the LEN function, which looks as follows: