Learn Pascal
description
Transcript of Learn Pascal
![Page 1: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/1.jpg)
Learn Pascal
![Page 2: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/2.jpg)
Content
Backgournd Basics Input/Output Program Flow Data Types Subroutines
![Page 3: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/3.jpg)
About Pascal Origins: ALGOL Inventor: Dr. Niklaus Wirth Date of Birth: 1971 Feature: Simple, Precise, Procedure-o
riented Classic Compilers: Turbo Pascal, Borla
nd Pascal, Free Pascal Extension: Object Pascal, Delphi
![Page 4: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/4.jpg)
First Pascal Program
program Hello; begin WriteLn('Hello, world!'); end.
![Page 5: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/5.jpg)
Program Structure
program ProgramName; const { Constant Declarations } type { Type Declarations } var { Variable Declarations }
![Page 6: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/6.jpg)
Program Structure
{ Subroutine definitions } begin { Main program} end.
![Page 7: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/7.jpg)
Comments
{ Comment } (* Comment *) // Line Comment, New in Delphi &
Free Pascal
![Page 8: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/8.jpg)
Identifiers Name of programs, procedures, functi
ons, types, constants, variables, etc. Must begin with a letter or an undersc
ore(_) Can be followed by letters, digits, and
underscores NOT case-sensitive (i.e. ID=id=Id=iD)
![Page 9: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/9.jpg)
Reserved Words
and array begin case
const div do downtoelse end file for
forward function goto if
in label mod nil
not of or packed
procedure
program record repeat
set then to type
until var while with
![Page 10: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/10.jpg)
Types
Simple Type String Type Structured Type Pointer Type Procedural Type
![Page 11: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/11.jpg)
Simple Types
Ordinary Types Integer Types Char Types Boolean Types
Real Types
![Page 12: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/12.jpg)
Integer Types
Type Size Range
ShortInt 1 -128 .. 127
Integer 2 -32768 .. 32767
LongInt 4 -2147483648 .. 2147483647
Byte 1 0 .. 255
Word 2 0 .. 65535
![Page 13: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/13.jpg)
Integer Types (Extension)
Type Size Range
SmallInt 2 -32768 .. 32767
LongWord
4 0 .. 4294967295
Int64 8 -263 .. 263-1
QWord 8 0 .. 264-1
Integer ? Platform Dependant
Cardinal ? Platform Dependant
![Page 14: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/14.jpg)
Real Types
Type Size
Range Digits
Real ? Platform Dependant ?
Single 4 1.5E-45 .. 3.4E38 7-8
Double 8 5.0E-324 .. 1.7E308 15-16
Extended
10 1.9E-4951 .. 1.1E4932
19-20
Comp 8 The Same as Int64 19-20
![Page 15: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/15.jpg)
Constants
const Identifier = value; // Cannot be modified Identifier : Type = value; // Can be modified
![Page 16: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/16.jpg)
Constants Example: const Name = 'Han Wentao'; // string Message = 'That''s OK.'; // string FirstLetter = 'a'; // Char NewLine = #10; // Char Year = 2003; // Integer Pi = 3.141592653589793; // Real IsInDebugMode = True; // Boolean a : Real = 12; // Typed Real
![Page 17: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/17.jpg)
Variables
var Identifier : Type; // Can be modified Example: var i, j, Count : Integer; FileName : string;
![Page 18: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/18.jpg)
Assignment
Symbol: := Format: Variable := expression; Example: i := $FF; Count := 0; FileName := 'berry10.out';
![Page 19: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/19.jpg)
Operators
Arithmetic Operators (+, -, *, /, mod, div)
Logical Operators (not, and, or, xor, shl, shr)
Boolean Operators (not, and, or, xor) Relation Operators (=, <>, <, >, <=, >=)
![Page 20: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/20.jpg)
Assignment & Operators Examples
i := (123 + 456) * 789; q1 := (1 + 10) div 3; // q1=3,Integer q2 := (1 + 10) / 3; // q2=3.666667 Value := 123; Radix := 16; Remainder := Value mod Radix; // Remainder=11
![Page 21: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/21.jpg)
Standard Routine
Read ReadLn Write
WriteLn Ord Chr
Pred Succ SizeOf
Concat Copy Delete
Insert Length Pos
Str Val UpCase
![Page 22: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/22.jpg)
Standard Math Routine
Abs ArcTan Cos
Dec Exp FracHi Inc IntLn Lo Odd
Pi Random Randomize
Round Sin SqrSqrt Swap Trunc
![Page 23: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/23.jpg)
Punctuation & Indentation You MUST have a semicolon(;)
following: the program heading each constant definition each variable declaration almost all statements
Indentation is not necessary but useful. It makes your program readable.
![Page 24: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/24.jpg)
Program Task 1 Input 3 real numbers, a, b, c, from
keyboard, print out the 2 roots of the equation ax2+bx+c=0 on the screen. It is guaranteed that the roots must exist.
Sample Input 1 -3 2
Sample Output 2 1
![Page 25: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/25.jpg)
My Solution for Task 1 program Task1; var a, b, c, Delta: Real; begin Read(a, b, c); Delta := Sqr(b) - 4 * a * c; WriteLn((-b + Sqrt(Delta)) / (2 * a)); WriteLn((-b - Sqrt(Delta)) / (2 * a)); end.
![Page 26: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/26.jpg)
Input
Read(<Variable1>, <Variable2>, …); // does not skip to the next line unless
necessary ReadLn(<Variable1>, <Variable2>, …); // just a Read procedure that skips to t
he next line at the end of reading
![Page 27: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/27.jpg)
Input ExampleWe input from keyboard:45 97 31 2 3
Statement(s) a b c d
Read(a);Read(b);
45 97
ReadLn(a);Read(b);
45 1
Read(a, b, c, d); 45 97 3 1
ReadLn(a, b);ReadLn(c, d);
45 97 1 2
![Page 28: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/28.jpg)
Output
Write(<Expression1>, <Expression2>, …);
WriteLn(<Expression1>, <Expression2>, …);
// skips to the next line when done
![Page 29: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/29.jpg)
Formatting Output
<Expression> : <Field Width> for reals: <Real Expression> : <Field Width> : <D
ecimal Field Width>
![Page 30: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/30.jpg)
Output Example
Write('Hi' : 10, 5 : 4, 5673 : 2); ********Hi***55673 Write(573549.56792 : 20 : 2); ***********573549.57 Write(123.345 : 0 : 2); 123.35 // * represents a space
![Page 31: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/31.jpg)
Text Files
Declaration: var FileVar : Text;
![Page 32: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/32.jpg)
Access a File
Assign(<FileVar>, 'File Path & Name'); for input: Reset(<FileVar>); for output: Rewrite(<FileVar>); and DO NOT forget: Close(<FileVar>); at last!
![Page 33: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/33.jpg)
Read and Write File
Read/ReadLn(<FileVar>, <Variable1>, <Variable2>, …);
Write/WriteLn(<FileVar>, <Expression1>, <Expression2>, …);
![Page 34: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/34.jpg)
A Shortcut for input:
Assign(Input, 'File Path & Name'); Reset(Input);
for output: Assign(Output, 'File Path & Name'); Rewrite(Output); … Close(Output); // DO NOT FORGET
![Page 35: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/35.jpg)
Test End of Line & End of File
Eoln(FileVar : Text) : Boolean; Eof(FileVar : Text) : Boolean;
![Page 36: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/36.jpg)
Program Task 2
Read an angle from file trigono.in in degrees. Write the function sin, cos, tan, cot, sec and csc of the angle to the file trigono.out, rounded to 4 decimals. It is guaranteed that the angle is valid.
![Page 37: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/37.jpg)
Program Task 2 Sample Input (trigono.in):
30 Sample Output (trigono.out):
0.5000 0.8660 0.5774 1.7321 1.1547 2.0000
![Page 38: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/38.jpg)
My Solution for Task 2 program TrigonometricFunctions; const InputFileName = 'trigono.in'; OutputFileName = 'trigono.out'; var Deg, Rad, SinA, CosA: Real; begin Assign(Input, InputFileName); Reset(Input); Assign(Output, OutputFileName); Rewrite(Output); Read(Deg);
![Page 39: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/39.jpg)
My Solution for Task2 Rad := Deg / 180 * Pi; SinA := Sin(Rad); CosA := Cos(Rad); WriteLn(SinA : 0 : 4); WriteLn(CosA : 0 : 4); WriteLn(SinA / CosA : 0 : 4); WriteLn(CosA / SinA : 0 : 4); WriteLn(1 / CosA : 0 : 4); WriteLn(1 / SinA : 0 : 4); Close(Input); Close(Output); end.
![Page 40: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/40.jpg)
Sequential Control
Sequential control is the default. The computer executes each statement and goes on to the next statement until it sees an end.
![Page 41: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/41.jpg)
Boolean Expressions Precedence
- not * / div mod and shr shl + - or xor = <> < > <= >=
Complex Boolean 3 > 5 or 650 < 1 // WRONG (3 > 5) or (650 < 1) // CORRECT
![Page 42: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/42.jpg)
Real Value Comparison
DO NOT compare two real values with operator =. Small round-off errors may cause two equivalent expressions to differ.
Instead, use this comparison: const Epsilon = 1E-10; Abs(x1 - x2) <= Epsilon
![Page 43: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/43.jpg)
Statement
Assignment Subroutine Call goto Compound Statement Conditional Statement Repetitive Statement
![Page 44: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/44.jpg)
Compound Statement
begin Statement1; Statement2; … end;
![Page 45: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/45.jpg)
IF Statement
if <BooleanExpression> then Statement; OR if <BooleanExpression> then Statement1 else Statement2;
![Page 46: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/46.jpg)
IF Statement Pitfall
if <BooleanExpression1> then if <BooleanExpression2> then Statement1 else Statement2;
![Page 47: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/47.jpg)
IF Statement Pitfall Correction
if <BooleanExpression1> then begin if <BooleanExpression2> then Statement1 end else Statement2;
![Page 48: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/48.jpg)
IF Statement Extension if <BooleanExpression1> then Statement1 else if <BooleanExpression2> then Statement2 else if … … else Statement n;
![Page 49: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/49.jpg)
CASE Statement
case <OrdinalExpression> of <OrdinalValueList1>: Statement1; <OrdinalValueList2>: Statement2; … else Statement end;
![Page 50: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/50.jpg)
FOR Statement
for <OrdinalVariable>:=<LowerBound> to <UpperBound> do
Statement; OR for <OrdinalVariable>:=<UpperBound
> downto <LowerBound> do Statement;
![Page 51: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/51.jpg)
WHILE Statement
while <BooleanExpression> do Statement;
![Page 52: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/52.jpg)
REPEAT Statement
repeat Statement1; Statement2; … until <BooleanExpression>;
![Page 53: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/53.jpg)
Break & Continue
Break jumps to the statement following the end of the current repetitive statement.
Continue jumps to the end of the current repetitive statement.
![Page 54: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/54.jpg)
Program Task 3 Fibonacci sequence is defined by:
a1=1 a2=1 ai=ai-1+ai-2
Input a positive integer n(n<=20) from keyboard, print an.
Sample Input: 10 Output: 55
![Page 55: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/55.jpg)
My Solution for Task 3 program Task3; var i, a, b, c, n: Integer; begin Read(n); a := 1; b := 1; for i := 3 to n do begin c := b; b := a + b; a := c; end; WriteLn(b); end.
![Page 56: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/56.jpg)
Program Task 4 Display all powers of 2 that are <
2,000,000,000. Display the list in a properly formatted manner, with commas between the numbers. Display five numbers per line. The output should look like:
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, …
![Page 57: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/57.jpg)
My Solution for Task 4 program Task4; const N = 31; var i: Integer; begin for i := 1 to N do begin Write(1 shl (i - 1)); if i <> N then Write(','); if i mod 5 = 0 then WriteLn else Write(' '); end; if N mod 5 <> 0 then WriteLn; end.
![Page 58: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/58.jpg)
Type Definition
type TypeIdentifier = TypeSpecification;
For example: type TName = string[25];
![Page 59: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/59.jpg)
Enumerated Type
Format: (id1, id2, …) For example: type TWeek = (Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday);
![Page 60: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/60.jpg)
Subrange Type Format: LowerBound .. UpperBound For example: type TDay = 1 .. 31; TWorkDay = Monday .. Friday;
![Page 61: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/61.jpg)
Array Type
Format: array [LowerBound .. UpperBound]
of SomeType; For example: a: array [1 .. 100] of Integer; FillChar(a, SizeOf(a), 0); a[1] := 100;
![Page 62: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/62.jpg)
Array Type
Multidimensional array: array [LowerBound1 .. UpperBound1,
LowerBound2 .. UpperBound2, …] of DataType;
![Page 63: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/63.jpg)
Record Type
Format: record FieldNameList1: Type1; FieldNameList2: Type2; … end;
![Page 64: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/64.jpg)
Record Type For example: TPerson= record Name: TName; Gender: (Male, Female); Age: 1 .. 200; Height, Weight: Real; end;
![Page 65: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/65.jpg)
Set Type
Format: set of OrdinalType For example: set of Char; set of 1 .. 100;
![Page 66: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/66.jpg)
Set Type a, b: set of 1 .. 100; Assignment:
a := [1 .. 10, 20]; b := [];
Operatiors: Union + Difference – Intersection * Add Element Include(S, E); Delete Element Exclude(S, E);
![Page 67: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/67.jpg)
Procedure Definition procedure Id[(ArgList)]; const … type … var … begin … end;
![Page 68: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/68.jpg)
Function Definition function Id[(ArgList)]: DataType; const … type … var … begin … Id := Expression; end;
![Page 69: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/69.jpg)
Argument List
[var] VarList: DataType; if no var, call-by-value if var, call-by-variable For example: procedure ConvertRadix(n, Radix: Inte
ger; var R: string);
![Page 70: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/70.jpg)
Exit & Halt
Exit exits the current subroutine, and returns control to the calling routine.
Halt stops program execution, and returns control to the calling program.
![Page 71: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/71.jpg)
Scope For example: program Scope; A, B, C
procedure Alpha; A, F, G procedure Beta; V, B, C
procedure Beta1; F function Beta2; F2
![Page 72: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/72.jpg)
Recursion Recursion means allowing a subroutine to call itsel
f directly or indirectly. For example: function Factorial(n: Integer): Integer; begin if n = 0 then Factorial := 1 else Factorial := n * Factorial(n - 1); end;
![Page 73: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/73.jpg)
Forward Referencing procedure A; forward; procedure B; begin … A; … end; procedure A; begin … B; … end;
![Page 74: Learn Pascal](https://reader033.fdocuments.us/reader033/viewer/2022061514/56815874550346895dc5d329/html5/thumbnails/74.jpg)
Program Task 5
Towers of Hanoi