天主教輔仁大學護理系 - ltcpa.org.t · 蕭伃伶 天主教輔仁大學護理系 助理教授 林立峯 衛生福利部雙和醫院復健醫學部 技術長 劉 芳 臺北醫學大學高齡健康管理學系
Chapter 7 Program Development & Programming Languages 國立聯合大學 電子工程學系...
-
date post
22-Dec-2015 -
Category
Documents
-
view
262 -
download
2
Transcript of Chapter 7 Program Development & Programming Languages 國立聯合大學 電子工程學系...
Chapter 7Program Development & Programming Languages
國立聯合大學 電子工程學系蕭裕弘
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 2 / 45
Chapter Goals
介紹利用電腦解決問題的方法與開發
軟體的程序
介紹程式語言的分類與歷史
介紹程式語言的特性
介紹數種常見的程式語言
介紹語言的轉譯方式
介紹一些不屬於程式語言的電腦語言
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 3 / 45
1. Introduction
Problem solving
The act of finding a solution to the perplexing ( 令人費解的 ), distressing
( 令人煩惱的 ), vexing ( 傷腦筋的 ), or unsettled question.
To solving the problem, you must answer the following questions:
What do I know about the problem?
What is the information that I have to process in order to find the solution?
What does the solution look like?
What sort of special cases exist?
How will I recognize that I have found the solution?
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 4 / 45
Computer Problem-Solving
問題描述與分析
設計演算法
開發軟體
程式執行與維護
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 5 / 45
Problem Specification and Analysis
Problem specification is to have a clear description of the problem Easily done in CS courses for small problems. More difficult to obtain in the real world for large problems.
Analysis is to obtain a clear understanding of the problem Carefully state the objectives of the program What output should the program produce
and in what format? What is the nature and format of
the required input? Identify the computations required
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 6 / 45
Divide and Conquer
Break up a large problem into smaller units that we can handle. Example: search 15 in the following sequence
Method 1: Linear search from the first number 7 comparisons needed
Method 2: Binary search 4 comparisons needed
1 2 3 4 5 6 7 8 9 10
3 5 8 9 10 13 15 16 18 20
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 7 / 45
2. Algorithms
We use algorithms every day Arithmetic operations Dialing a phone Looking up a phone number in the phone bo
ok Changing a tire …
Usually, algorithm means a precise method in information processing.
拿起話筒
按下一個號碼鍵
是否按了九個號碼
?
No
Yes
Start
Stop
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 8 / 45
Definition of Algorithm
An algorithm is a well-defined set of instructions, finite in number, for accomplishing some task which, given a set of inputs, will result in some recognizable end-state.
Every algorithm must satisfy the following criteria: Input: there are zero or more quantities which are externally
supplied; Output: at least one quantity is produced; Definiteness: each instruction must be clear and unambiguous; Finiteness: if we trace out the instructions of an algorithm, then for
all cases the algorithm will terminate after a finite number of steps; Effectiveness: every instruction must be sufficiently basic that it can
in principle be carried out by a person using only pencil and paper.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 9 / 45
Algorithm Design
Algorithm design tools include Structure charts Flowcharts Pseudocode
Algorithms + Data Structures = Programs
Program control
Input Calculate Output
Start
Input data
Calculate result
Output result
Stop
Input the data;
Calculate the result;
Output the result;
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 10 / 45
常用的流程圖符號
開始 / 結束
決策判斷
印表機輸出
磁碟
儲存資料
處理
輸入 / 輸出
流程方向
連結點
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 11 / 45
Example of Flowchart
以迴圈 (loop) 方式計算
1 + 2 + ... + N 的值。 執行過程 :
1 (sum = 0), 2 (N = 3), 3 (i = 0)
4 (Yes), 6 (i = 1, sum = 1),
4 (Yes), 6 (i = 2, sum = 3),
4 (Yes), 6 (i = 3, sum = 6),
4 (No), 5 (sum = 6)
Start
Input N
sum = 0
i = 0
i < N ?i = i + 1sum = sum + i
Output sum
Stop
1
2
3
4
5
6
Yes
No
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 12 / 45
Example of Pseudocode
Goal: find the largest number in a number list
1. Pretend the first number in the list is the largest number.
2. Look at the next number, and compare it with this largest number.
3. Only if this next number is larger, then keep that as the new largest number.
4. Repeat steps 2 and 3 until you have gone through the whole list.
Given: a list "List"
largest = List[1];counter = 2;while (counter <= length(List))
if (List[counter] > largest)largest = List[counter];
counter = counter + 1;print largest;
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 13 / 45
Software Development
Five phases are common in software development
Problem Specification and Analysis
Design of solution
Implementation or Coding
Testing, Execution, and Debugging
Maintenance
Software
Development
Life
Cycle
(SDLC)
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 14 / 45
Software Development Methods - 1
In the top-down model, an overview of the system is formulated, without going into detail for any part of it. Each part of the system is then refined
by designing it in more detail. Each new part may then be refined
again, defining it in yet more detail until the entire specification is detailed enough to begin development.
Top down approaches emphasize planning, and a complete understanding of the system. It is inherent that no coding can begin until a sufficient level of detail has been reached on at least some part of the system.
Command interpret system
Readcommand
Evaluatecommand
Displayresult
Parsecommand
Dispatchcommand
Look-up Type file Delete file
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 15 / 45
Software Development Methods - 2
In the bottom-up design, individual parts of the system are specified in
detail, and may even be coded. The parts are then linked together to form
larger components, which are in turn linked until a complete system is
arrived at.
Bottom up emphasizes coding, which can begin
as soon as the first module has been specified.
However bottom-up coding runs the risk that
modules may be coded without having a clear
idea of how they link to other parts of the system,
and that such linking may not be as easy as first
thought.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 16 / 45
Choosing a Programming Language
Factors that may affect the decision of choosing a programming language: Suitability Integration Standards Programmer availability Portability Speed
CJava
PHP
C++
COBOLFORTRAN
BASIC
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 17 / 45
2. Programming Languages
A programming language or computer language is a
standardized communication technique for expressing
instructions to a computer.
A programming language is a set of syntactic and semantic
rules used to define computer programs.
A programming language enables a programmer to precisely
specify what data a computer will act upon
how these data will be stored/transmitted
precisely what actions to take under various circumstances
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 18 / 45
Purpose of Programming Languages
A primary purpose of programming languages is to enable programmers to
express their intent for a computation more easily than they could with a
lower-level language or machine code.
For this reason, programming languages are generally designed to use a
higher-level syntax, which can be easily communicated and understood by
human programmers.
Programming languages are important tools for helping
software engineers write better programs faster.
Understanding programming languages is crucial for
those engaged in computer science, because all types
of computation are done with computer languages today.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 19 / 45
History of Programming Languages
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 20 / 45
Generations of Programming Languages - 1
Generations Representative Features Examples
1 Machine language Numerically coded instructions to
the CPU
Machine-specifically
0101011010
2Assembly languages
Mnemonics for the designation of
machine instructions
elementary math. and logs.
Functions
branches, subroutines
ADD, SUB, MUL, DIV, JUMP, CALL, …
3 (1st phase)Procedural languages
First high-level languages
processing of character strings,
higher mathematical functions
Fortran, Basic
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 21 / 45
Generations of Programming Languages - 2
Generations Representative Features Examples
3 (2nd phase)Structured languages
Structured programming
iterations,
case branches etc.
Pascal, C
4Object-oriented languages
Object-oriented programming data encapsulation inheritance polymorphism
C++, Object Pascal
5Component-based (visual) languages
Visual programming components, events, two way tools (rapid application development - RAD)
Delphi, Java, C++ Builder, (Visual Basic)
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 22 / 45
Generations of Programming Languages - 3
Generation Year Representative Examples
1 1945 Machine language
2 Mid-1950s Assembly language
3 Early 1960s High-level languagesFORTRAN, COBOL, BASIC, C, Ada
4 Early 1970s Very-high-level languages SQL
5 Early 1980s Natural languages
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 23 / 45
3. Features of a Programming Language
Each programming language can be thought of as a set of formal specifications concerning syntax, vocabulary, and meaning.
These specifications usually include: Data type and data structures Instruction and control flow Reference mechanisms and
re-use Design philosophy
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 24 / 45
Data Types and Data Structures
The particular system by which data are organized in a program is the type system of the programming language.
Primitive data types Data types with which values have a one-to-o
ne correspondence to data objects stored in computer memory.
Primitive types are also known as built-in types or basic types.
In C: char, int, float, double Structured data types or composite data types
Data type made up of more primitive types. Values with a composite type are stored in th
e memory in such a way that each attribute is followed by another attribute.
In C: array, struct, …
Data type Memory used
char 1 bytes
int 2 or 4 bytes
float 4 bytes
double 8 bytes
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 25 / 45
Variables
In computer science, a variable is a symbol
denoting a quantity or symbolic
representation.
A variable can be thought of as a place to
store a value in computer memory.
When one begins using a given variable, the
language interpreter or compiler typically
sets aside a space in memory to store the
value given to that variable.
char ch;
int total;
float average;
int chi;
int eng;
ch
total
average
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 26 / 45
Instructions and Statements
Once data has been specified, the machine must be instructed how to
perform operations on the data.
Elementary statements may be specified using keywords or may be
indicated using some well-defined grammatical structure.
Each language takes units of these well-behaved statements and combines
them using some ordering system. Depending on the language, differing
methods of grouping these elementary statements exist.
total = chi + eng + ari;average = total / 3.0;
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 27 / 45
Control Flow
Furthermore, beyond the data manipulation instructions, other typical
instructions in a language are those used for control flow.
Branches
Loops
Score >= 60?
Yes
No
Output ‘pass’ Output ‘fail’
Repeat?
No
Yes
Statement 2
Statement 1
Statement 1
Statement 2
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 28 / 45
Reference Mechanisms and Re-use
The core of the idea of reference is that there must be a method of indirectly designating storage space.
The most common method is through named variables. Depending on the language, further indirection may include references that are
pointers to other storage space stored in such variables or groups of variables. Similar to this method of naming storage is the method of naming groups of
instructions. Most programming language use procedure calls or function calls as the statements
that use these names. Using symbolic names in this way allows a program to achieve significant
flexibility, as well as a high measure of reusability.
Calls function A Body offunction A
SubroutineProcedureFunction
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 29 / 45
Design Philosophy
For different purposes, each language has been developed using a special design or philosophy.
Some aspect or another is particularly stressed by the way the language uses data structures, or by which its special notation encourages certain ways of solving problems or expressing their structure.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 30 / 45
4. Language Translators
Since the computer can only execute the program in machine code, the
programs written in assembly or high-level language should be translated
into machine code before execution.
Types of language translators:
Assembler
Compiler
Interpreter
Sourceprogram Translator
Object codeor results
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 31 / 45
Assembler
Assembly language is a human-readable notation for the machine language that a specific computer architecture uses.
Every computer architecture has its own machine language, and therefore its own assembly language.
An assembler is a computer program for translating assembly language into object code.
mov al, 0x61 10110000 01100001
Instruction = Operation code + Operand(s)
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 32 / 45
Compiler
A compiler is a computer program that
translates a computer program written in
one computer language (called the source
language) into a program written in
another computer language (called the
output or the target language).
Most compilers translate source code
written in a high level language to object
code or machine language that may be
directly executed by a computer or a
virtual machine.
The first completed compiler: 1950s
FORTRAN
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate code generation
Optimization
Code generation
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 33 / 45
Process of Compilation
int a, b, c;
float d;
c = a + b;
d = (a + b) / (a - b);
int a, b, c;
float d;
c = a + b;
d = ( a + b ) / ( a - b );
Allocate.I aAllocate.I bAllocate.I cAllocate.F dLoad r1, aLoad r2, bAdd r3, r1, r2Store c, r3Load r4, aLoad r5, bAdd r6, r4, r5Sub r7, r4, r5Div.F r8, r6, r7Store d, r8
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 34 / 45
Linkage Editor
A linker or linkage editor is a program that takes one or more objects generated by compilers and assembles them into a single executable program.
CompilerSource #1 Object #1
CompilerSource #2 Object #1
CompilerSource #n Object #n
System library
LinkerLoad
module
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 35 / 45
Interpreter
An interpreter is a computer
program that executes other
programs.
Interpreting code is slower than
running the compiled code because the
interpreter must analyze each statement
in the program each time it is executed
and then perform the desired action
whereas the compiled code just
performs the action.
High-levelsource
program
LoadSource
ProgramIn memory
Run
Interpretertranslates
oneinstruction
Machine-code
instruction
Execute
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 36 / 45
5. Popular PLs - FORTRAN
The name is short for Formula Translator/Translation. Fortran is mainly used for scientific computing and numerical
analysis. Although originally a procedural language, recent versions of
Fortran have included some features to support object-oriented programming.
PROGRAM HELLO
PRINT *, 'hello, world'
END
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 37 / 45
COBOL
Its name is an acronym, for COmmon Business Oriented Language.
Its primary domain was in business, finance, and administrative systems for companies and governments.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
BEGIN.
DISPLAY " " LINE 1 POSITION 1 ERASE EOS.
DISPLAY "HELLO, WORLD." LINE 15 POSITION 10.
STOP RUN.
EXIT.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 38 / 45
Pascal
Pascal is based on the Algol programming language and is named in honor of mathematician and philosopher Blaise Pascal.
Pascal is one of the landmark programming languages on which generations of students cut their teeth and variants of which are still widely used today.
program HelloWorld;
Begin
WriteLn('Hello World!')
end.
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 39 / 45
BASIC, Visual BASIC
BASIC's name stands for Beginner's All-purpose Symbolic Instruction Code.
Originally devised as an easy-to-use tool, it became widespread on home microcomputers in the 1980s, and remains popular to this day in a handful of heavily evolved dialects.
BASIC is available on nearly every microprocessor platform made: Interpret version Compile version
10 PRINT “Hello World!”
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 40 / 45
C, C++, C#
C has since spread to many other operating systems, and is one of the most
widely used programming languages.
C is prized for its efficiency, and is the most popular programming
language for writing system software, though it is also used for writing
applications.
It is also commonly used in computer science education, despite not being
designed for novices. #include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 41 / 45
Java
The Java language is an object-oriented programming language created by James Gosling and other engineers at Sun Microsystems in 1991.
There were four primary goals in the creation of the Java language: It is object-oriented. It is independent of the host platform (more or less). It contains language facilities and libraries for networking. It is designed to execute code from remote sources securely.
import java.io.*
public class Print {
public static void main(String args[]) {
System.out.println(“Hello, World!”);
}
}
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 42 / 45
6. Other Languages - HTML
HyperText Markup Language (HTML) is a markup language designed for c
reating web pages, that is, information presented on the World Wide Web.
HTML tags can be used to perform such tasks as:
Declaring titles for page, <TITLE>
Identifying the size of headings, <H1>, <H2>
Marking the ends of paragraphs, <P>
Establishing such text styling as italic and boldfaced type, <I>, <B>
Setting up hyperlinks to other documents, <A>
Identifying complex elements to be inserted into a document, such as image
s, video clips, and sound files, <IMG>
Specifying the layout of tables and frames, <TABLE>
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 43 / 45
JavaScript
JavaScript is an object-oriented scripting language commonly used in websi
tes.
It was originally developed by Brendan Eich of Netscape Communications
under the name "Mocha" and then "LiveScript" but then renamed to "JavaS
cript" and given a syntax closer to that of Sun Microsystems’ Java language.
<HTML>
<SCRIPT LANGUAGE="JavaScript">
document.write("Hello World!");
</SCRIPT>
</HTML>
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 44 / 45
VBScript
VBScript (short form of Microsoft's Visual Basic Scripting Edition) is a sub
set of Visual Basic used in Active Server Pages and in Windows Scripting
Host as a general-purpose scripting language.
VBScript is interpreted by a script engine:
ASP in a web environment
wscript.exe in a Windows environment
cscript.exe in a command-line environment.
msgbox "Hello world!"
國立聯合大學電子工程學系 – 計算機概論 – 蕭裕弘 Chapter 7: Page 45 / 45
PHP
PHP (a recursive acronym for "PHP: Hypertext Preprocessor") is a widely-
used open-source programming language primarily for server-side applicati
ons and developing dynamic web content.
PHP's ease of use and similarity with the most common structured program
ming languages – most notably C and Perl.
<HTML>
<BODY>
<?
echo "Hello World!";
?>
</BODY>
</HTML>