Tải bản đầy đủ - 0 (trang)
Appendix F. High-Level and Low-Level Languages

Appendix F. High-Level and Low-Level Languages

Tải bản đầy đủ - 0trang

; --------------------; Printing instructions

; --------------------; get number of characters to print

mov cx,Msg_Len

; get location of message

mov bx,offset Message

; get printer number (first printer is printer 0)

mov dx,0

Print_Loop:

; send character to printer 0

mov ah,0

mov al,[bx]

int 17h

; do next character

inc bx

loop Print_Loop



For comparison, let us see how this same task would be accomplished in the BASIC programming

language:

LPRINT "Happy printing"



The difference is pretty obvious.

As we have discussed, high-level languages are usually designed for a specific purpose. Generally,

this purpose is to write software applications of a specific type. For instance, Visual C++ and

Visual Basic are used primarily to write standalone Windows applications. Indeed, Microsoft

Excel itself is written in Visual C++. As another example, FORTRAN (which is a contraction of

Formula Translation) is designed to write scientific and computational applications for various

platforms (including Windows). COBOL is used to write business-related applications (generally

for mainframe computers).

At the highest level in the programming language hierarchy, we find programs such as Excel VBA,

whose primary purpose is not to manipulate the operating system or hardware, nor to write

standalone Windows applications, but rather to manipulate a high-level software application (in

this case Microsoft Excel).

Just for fun, let us take a brief look at a handful of the more common programming languages.



F.1 BASIC

The word BASIC is an acronym for Beginners All-Purpose Symbolic Instruction Code, the key

word here being Beginners. BASIC was developed at Dartmouth College in 1963, by two

mathematicians: John Kemeny and Thomas Kurtz. The intention was to design a programming

language for liberal arts students, who made up the vast majority of the student population at

Dartmouth. The goal was to create a language that would be friendly to the user and have a fast

turn-around time so it could be used effectively for homework assignments. (In those days, a

student would submit a program to a computer operator, who would place the program in a queue,



455



awaiting processing by the computer. The student would simply have to wait for the results—there

were no PCs in the 1960s!)

The first version of BASIC was very simple; indeed, it was primitive. For example, it had only

one data type: floating-point. (Data types are discussed in Chapter 5.) Since then BASIC has made

tremendous strides as a personal computer programming language, due mostly to the embrace of

the language by Microsoft.

Even to this day, however, the BASIC language, and its offshoot Visual Basic, do not get much

respect among computer scientists or academicians. The BASIC language gets a bad rap on two

fronts. First, it is generally considered a weak language in the sense that it does not provide very

much control over the computer's hardware (or operating system), at least as compared to other

languages such as C. Second, BASIC has a reputation for not "forcing" (or in some cases even

allowing) programmers to use good programming style.

For comparison with some other languages, here is a BASIC program that asks the user for some

numbers and then returns their average. Lines beginning with an apostrophe are comment lines

that are ignored by the computer.

' BASIC program to compute the average

' of a set of at most 100 numbers

' Ask for the number of numbers

INPUT "Enter the number of numbers: ", Num

' If Num is between 1 and 100 then proceed

' IF Num > 0 AND Num <= 100 THEN

Sum = 0

' Loop to collect the numbers to average

FOR I = 1 TO Num

' Ask for next number

INPUT "Enter next number: ", NextNum

' Add the number to the running sum

Sum = Sum + NextNum

NEXT I

' Compute the average

Ave = Sum / Num

' Display the average

PRINT "The average is: "; Ave

END IF

END



F.2 Visual Basic

Microsoft took the BASIC programming language to new heights when it developed Visual Basic.

In the early 1990s, Microsoft faced a dilemma. Windows was (and is) a considerably more

complex operating system than DOS, so much so that only professional programmers could

effectively use Microsoft's main programming tool for creating Windows-based applications—the

Microsoft Windows SDK, which is based on the C language. (These days, this tool has given way



456



to a more modern tool, Microsoft Foundation Classes, which is still not for the casual

programmer.)

But Microsoft wanted more people to be able to create Windows applications, since it was good

for business. So in 1991, the company introduced Visual Basic (VB for short), which essentially

combined the BASIC language with a visual programming environment so users could easily

create graphical components, such as windows, command buttons, text boxes, option buttons, and

menus that are required by Windows applications. The underlying language for VB is called

Visual Basic for Applications, or VBA for short, although this term was not coined until later in

the development of VB.

The first version of Visual Basic was little more than an interesting toy. It did not really have the

power to create serious Windows applications. However, it provided a necessary starting point for

further development of the product. Each successive version of Visual Basic has taken major steps

forward in sophistication, so that now VB is by far the most widely used programming language

for PCs. (Microsoft estimates that over three million people use some form of Visual Basic, about

half of whom program using some form of Office VBA, the rest using the standalone VB product.)



AM

FL

Y



While Visual Basic has become a very respectable tool for creating standalone Windows

applications, the real popularity of Visual Basic for Applications (VBA) lies in the fact that it is

the underlying programming language for the Microsoft Office application suite, which probably

has closer to 100 million users, each of whom is a potential VBA programmer. Indeed,

presumably the reason that you are reading this book is that you want to join the group of VBA

programmers.

VBA is a high-level programming language that underlies several important Windows

applications, including Microsoft Word, Excel, Access, and PowerPoint, as well as Microsoft

Visual Basic. In addition, companies other than Microsoft can license VBA and use it as the

underlying programming language for their applications.



TE



Each so-called host application provides extensions to VBA to accommodate its own needs and

idiosyncrasies. For instance, since Microsoft Word deals with documents, Word VBA needs to

understand such things as headers and footers, grammar checking, page numbering, and so on. On

the other hand, since Excel deals with worksheets, Excel VBA needs to understand such things as

cells, formulas, charts, and so on. Finally, since Visual Basic is designed for writing standalone

Windows applications, it must excel at manipulating Windows controls (text boxes, command

buttons, list boxes).



F.3 C and C++

The C programming language, a descendant of several older languages (including B), was

developed by Dennis Ritchie at Bell Laboratories in 1972. C is a simple language in its syntax,

with relatively few instructions. However, it has been extended considerably by Microsoft (among

others) for use in the PC environment.

The strength of C and its descendants, such as C++, are that it combines the advantages of a highlevel programming language, such as relative readability, with the ability to reach down to the

operating system and hardware levels of the computer. Unfortunately, the power of C can

sometimes be dangerous in the hands of, shall we say, programmers of only modest capability.

Also, the syntax of C allows for what some programmers consider "clever" or "elegant"

programming style, but which may be more accurately termed "highly unreadable."



457 ®

Team-Fly



For comparison purposes, here is the C language version of the BASIC program that computes the

average of some numbers. I think you will agree that it is not quite as readable as the earlier

BASIC version. Lines beginning with // are comment lines that are ignored by the computer.

// C program to compute the average

// of a set of at most 100 numbers

#include

void main(void)

{

// Declare some variables

int Num, i;

float Sum, NextNum, Ave;

// Ask for the number of numbers

printf( "Enter number of numbers: " );

scanf( "%u", &Num );

// If Num is between 1 and 100 then proceed

if( (Num > 0) && (Num <= 100) )

{

Sum = 0.0;

// Loop to collect the numbers to average

for( i = 1; i <= Num; i++ )

{

// Ask for next number

printf( "Enter next number: " );

scanf( "%f", &NextNum );

// Add the number to the running sum

Sum += NextNum;

}

// Compute the average

Ave = Sum / Num;

//Display the average

printf ("The average is: %f\n", Ave );

}

}



An object-oriented extension to C, known as C++, was developed in the early 1980s by Bjarne

Stroustrup (also at Bell Labs).



F.4 Visual C++

Despite the significant strides that Visual Basic has taken, it is not, in general, the preferred

language for creating complex standalone Windows applications. That role belongs to Microsoft's

Visual C++.

Actually, this is a good thing. Microsoft must guard against trying to make any single language the

solution for too many diverse programming needs. Such an effort can only be counterproductive.

By increasing the power of Visual Basic (and VBA) in order to handle more diverse and

sophisticated application programming, the language becomes more complex and difficult to learn

and use. This will result in the language being used by fewer people.



458



Visual C++ is a marriage between the C++ programming language and the Windows graphical

environment. Visual C++ is not nearly as user-friendly as Visual Basic. This is due in part to the

nature of the underlying language (C is less friendly than BASIC), in part to the fact that C++ is a

fully object-oriented language and therefore naturally more complicated, and in part to the fact

that Visual C++ is designed to control the Windows environment at a more fundamental level than

Visual Basic. For instance, Visual Basic does not provide ways to create a text box whose text can

use more than one color, set the tabs in a list box, or change the color of the caption in a command

button, and so on. Simply put, when programming in VB (or VBA), we must sacrifice power in

some directions in favor of power in other directions and a simpler programming environment.



F.5 Pascal

Pascal was developed by Niklaus Wirth (pronounced "Virt") in the late 1960s and early 1970s.

The goal was to produce a language that could be easily implemented on a variety of computers

and that would provide students with a model teaching language. That is to say, Pascal is full of

features that encourage well-written and well-structured programs. Indeed, many universities

teach Pascal to their computer science students as a first language. Pascal has also migrated to the

personal computer arena, first with Borland's Turbo Pascal and more recently with Borland's

visual programming environment called Delphi.

For contrast, here is how our program to compute the average would look in Pascal. Text

contained within curly braces ({,}) are comments that are ignored by the computer.

{ Pascal program to compute the average

of a set of at most 100 numbers }

program average (input, output);

{ Declare some variables }

var

Num, i : integer;

Ave, Sum, NextNum : real;

begin

{ Ask for the number of numbers }

writeln('Enter the number of numbers');

readln(Num);

{ If Num is between 1 and 100 then proceed }

if ((Num > 0 ) and (Num <= 100)) then

begin

Sum := 0;

{ Loop to collect the numbers to average }

for i := 1 to Num do

begin

{ Ask for next number }

writeln('Enter next number');

readln(NextNum);

{ Add the number to the running sum }

Sum := Sum + NextNum;

end

{ Compute the average }

Ave := Sum / Num;

{ Display the average }

writeln('The average is: ', Ave);

end

end



459



F.6 FORTRAN

FORTRAN is a contraction of Formula Translation, a name that comes from a technical report

entitled "The IBM Mathematical FORmula TRANslating System," written by John Backus and his

team at IBM in the mid-1950s. FORTRAN is primarily designed for scientific calculations and

has the distinction of being the first widely used high-level programming language. Backus made

some rather interesting claims about FORTRAN; for instance, it was not designed for its beauty (a

reasonable statement) but it would eliminate coding errors and the consequent debugging process!

Here is the FORTRAN version of our little averaging program. (Lines that begin with a C are

comments.)

C FORTRAN PROGRAM TO COMPUTE THE AVERAGE

C OF A SET OF AT MOST 100 NUMBERS

Real SUM, AVE, NEXTNUM

SUM = 0.0

C Ask for the number of numbers

WRITE(*,*) 'Enter the number of numbers: '

READ(*,*) NUM

C If Num is between 1 and 100 then proceed

IF NUM .GT. 0 .AND. NUM .LE. 100 then

C Loop to collect the numbers to average

DO 10 I = 1, NUM

C Ask for next number

WRITE(*,*) 'Enter next number: '

READ(*,*) NEXTNUM

C Add the number to the running sum

SUM = SUM + NEXTNUM

10

CONTINUE

C Compute the average

AVE = SUM/NUM

C Display the average

WRITE(*,*) 'The average is: '

WRITE(*,*) AVE

ENDIF

STOP

END



F.7 COBOL

COBOL is an acronym for Common Business Oriented Language and it was developed in the late

1950s by Grace Hopper for the purpose of writing business-related programs, which she felt

should be written in English. However, it seems rather that the language was developed with the

express purpose of avoiding all mathematical-like notation. The inevitable consequence is that

conciseness and readability is also avoided.

At any rate, I could only bring myself to code a COBOL sample program that adds two numbers.

* COBOL PROGRAM TO ADD TWO NUMBERS



460



IDENTIFICATION DIVISION.

PROGRAM-ID.

ADD02.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01

FIRST-NUMBER

01

SECOND-NUMBER

01

SUM



PIC IS 99.

PIC IS 99.

PIC IS 999.



PROCEDURE DIVISION.

PROGRAM-BEGIN.

DISPLAY "ENTER FIRST NUMBER ".

ACCEPT FIRST-NUMBER.

DISPLAY "ENTER SECOND NUMBER ".

ACCEPT SECOND-NUMBER.

COMPUTE SUM = FIRST-NUMBER + SECOND-NUMBER

DISPLAY "THE SUM IS: " SUM.

PROGRAM-DONE.

STOP RUN.



In BASIC, the preceding program would be:

INPUT "Enter first number: ", n1

INPUT "Enter second number: ", n2

PRINT "The sum is: ", n1 + n2



This clearly points out the extreme verbosity of COBOL.



F.8 LISP

BASIC, C, Pascal, and FORTRAN are in many ways quite similar. Also, programs written in

these languages can be made quite readable, especially if the programmer intends to make it so.

There are other languages that seem not to be readable under any circumstances. For instance,

LISP was developed in the late 1950s by John McCarthy and Marvin Minsky at MIT, for the

purpose of doing list processing (hence the name) in connection with artificial intelligence

applications.

In LISP, everything is a list. Here is a sample:

; LISP sample program to define a predicate

;

that takes two lists and returns the value

; T (for true) if the lists are equal and F otherwise

(DEFINE (

'(equal (LAMDBA (list1 list2)

(COND

((ATOM list1) (EQ list1 list2))

((ATOM list1 NIL)

((equal (CAR list1) (CAR list2))



461



(equal (CDR list1) (CDR list2)))

(T NIL)

)

))

))



This sample points out one fact. Whatever else we might think of Microsoft, we can at least thank

them for choosing BASIC (VBA) as the underlying language for the Microsoft Office suite!



462



Appendix G. New Objects in Excel XP

Excel XP contains 37 new objects. This appendix presents a list, along with paths and a brief

description of each object. I also include a list of the object's members (omitting the almost

ubiquitous Application, Creator, and Parent properties).



AllowEditRange Object

Path: Application -- Range -- Worksheet

-- AllowEditRanges - AllowEditRange



Object

AllowEditRange

Object



AllowEditRanges

Object



--



Protection



Methods









ChangePassword(Password As String)

Delete()

Unprotect([Password] As Variant)







Add(Title As String, Range As Range,

[Password] As Variant) As

AllowEditRange

















Properties

Range() As Range

Title() As String

Users() As

UserAccessList

Count() As Long

Item(Index As

Variant) As

AllowEditRange



The AllowEditRange object allows a specified range of cells on a worksheet to be password

protected from editing. Once a range has been protected in this way, and the entire worksheet has

been protected, any attempt at editing cells in that range will require the password.

The AllowEditRanges collection object holds the current AllowEditRange objects.



AutoRecover Object

Path: Application - AutoRecover



Object

AutoRecover











Properties

Enabled() As Boolean

Path() As String

Time() As Long



The AutoRecover feature allows for the possible recovery of data if Excel XP stops responding (or

should I say when Excel XP stops responding?). The user can set the location in which Excel will

save AutoRecover files as well as the time between saving.



463



The AutoRecover object implements automatic recovery. Its Path property sets the location where

Excel will place the AutoRecover file and its Time property sets the time (in minutes) interval for

backing up the workbook to the AutoRecover file, as in:

Application.AutoRecover.Path = "d:\temp"

Application.AutoRecover.Time = 10' minutes



CalculatedMember Object

Path: Application -- Range -- PivotTable

CalculatedMembers - CalculatedMember



Object



Methods



CalculatedMember



CalculatedMembers





















Delete()













Add(Name As String

Formula As String

[SolveOrder] As Variant

[Type] As Variant) As

CalculatedMember









--



Properties

Formula() As String

IsValid() As Boolean

Name() As String

SolveOrder() As Long

SourceName() As String

Type() As

XlCalculatedMemberType



Count() As Long

Item(Index As Variant) As

CalculatedMember



The CalculatedMember object programmatically manipulates the calculated fields and calculated

items for PivotTables that are connected to external OLAP (Online Analytical Processing) data

sources. We will not discuss these data sources further in this book.



CellFormat Object

Path: Application - CellFormat



Object



CellFormat



Methods







Clear()



























Properties

AddIndent() As Variant

Borders() As Borders

Font() As Font

FormulaHidden() As Variant

HorizontalAlignment() As Variant

IndentLevel() As Variant

Interior() As Interior

Locked() As Variant

MergeCells() As Variant

NumberFormat() As Variant

NumberFormatLocal() As Variant



464



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix F. High-Level and Low-Level Languages

Tải bản đầy đủ ngay(0 tr)

×