1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 6 data types

51 278 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 667,3 KB

Nội dung

Chapter 6 Data Types ISBN 0-321-33025-0 Chapter 6 Topics • Introduction • Primitive Data Types • Character String Types • User-Defined Ordinal Types • Array Types Array Types • Associative Arrays RdT • R ecor d T ypes •Union Types Copyright © 2006 Addison-Wesley. All rights reserved. 1-2 • Pointer and Reference Types Introduction •A data type defines a collection of data values and a set of predefined operations on those objects • Some data types are specified by type operators which are used to form type expressions (‘[]’, ‘*’, ‘()’ in C for array, pointer, function, respectively) •A descriptor is the collection of the attributes of a variable. It is used for type checking and by allocation and deallocation operations •D es i g n i ssue : Wh at ope r at i o n s a r e p r o vi ded f o r Copyright © 2006 Addison-Wesley. All rights reserved. 1-3 es g ssue at ope at o s a e p o ded o variables of the type and how are they specified? Primitive Data Types • Almost all programming languages provide a set of primitive data types – Primitive data t yp es: Those not defined in terms yp of other data types • Some primitive data types are merely reflections • Some primitive data types are merely reflections of the hardware • Others require little software support Copyright © 2006 Addison-Wesley. All rights reserved. 1-4 Primitive Data Types: Integer • Almost always an exact reflection of the hardware hiiiil so t h e mapp i ng i s tr i v i a l – Java: byte, short, int, long –Ada: SHORT INTEGER, INTEGER and LONG INTEGER • The leftmost bit is set to indicate negative and the remainder of the bit string represents the absolute value of the number • Most computers now use a notation called two’s complement to store negative integers, which is Copyright © 2006 Addison-Wesley. All rights reserved. 1-5 complement to store negative integers, which is convenient for addition and subtraction Primitive Data Types: Floating Point • Model real numbers but only as approximations • Model real numbers , but only as approximations for most real values ff l • Languages f or scienti f ic use support at l east two floating-point types (e.g., float and double; sometimes more) • Usuall y exactl y like the hardware , but not alwa y s yy , y Copyright © 2006 Addison-Wesley. All rights reserved. 1-6 IEEE Floating Point Formats Copyright © 2006 Addison-Wesley. All rights reserved. 1-7 Primitive Data Types: Decimal • For business applications Sfidbfdildii • S tore a fi xe d num b er o f d ec i ma l di g i ts • The representations of data types are called binar coded decimal (BCD) binar y coded decimal (BCD) – Two digits per byte Ad t • Ad van t age – Accuracy Di d • Di sa d vantages – The range of values is restricted because no exponents are allowed Copyright © 2006 Addison-Wesley. All rights reserved. 1-8 exponents are allowed – Wastes memory Primitive Data Types: Boolean •Sim p lest of all p • Range of values: two elements, one for “true” and one for “ false ” and one for false • Could be implemented as bits, but often as bytes bytes – Advantage: readability Copyright © 2006 Addison-Wesley. All rights reserved. 1-9 Primitive Data Types: Character • Stored as numeric codings • Most commonly used coding: ASCII An alternative 16 bit coding: Unicode • An alternative , 16 - bit coding: Unicode – Includes characters from most natural languages – Originally used in Java – C# and JavaScript also support Unicode Copyright © 2006 Addison-Wesley. All rights reserved. 1-10 Character String Types • Values are sequences of characters • Design issues: – Is it a primitive type or just a special kind of Is it a primitive type or just a special kind of array? – Should the length of strings be static or Should the length of strings be static or dynamic? Copyright © 2006 Addison-Wesley. All rights reserved. 1-11 Character String Types Operations • Typical operations: – Assignment and copying – Comparison – Comparison – Catenation – Substring reference – Pattern matching Copyright © 2006 Addison-Wesley. All rights reserved. 1-12 Character String Type in Certain Languages •Ada Nt i iti – N o t pr i m iti ve – Assignment, comparison, catenation, substring reference reference NAME1 : STRING(1 30); NAME1 := NAME1 & NAME2; (catenation) NAME1 := NAME1 & NAME2; (catenation) NAME1(2:7) (substring reference) • C C – Not primitive – Use char arrays and a library of functions that Copyright © 2006 Addison-Wesley. All rights reserved. 1-13 – Use char arrays and a library of functions that provide operations Character String Type in Certain Languages • Java – Primitive via the String class (not arrays of char). Objects cannot be changed lfh bl b – StringBuffer is a c l ass f or c h angea bl e string o b jects • SNOBOL4 (a string manipulation language) –Primitive – Many operations, including elaborate pattern hi matc hi ng LETTER = ‘abcdefghijklmnopqrstuvwxyz’; WORDPAT BREAK(LETTER) SPAN(LETTER) WORD Copyright © 2006 Addison-Wesley. All rights reserved. 1-14 WORDPAT = BREAK(LETTER) SPAN(LETTER) . WORD TEXT WORDPAT Character String Length Options • Static length string: FORTRAN 90, COBOL, Pascal, J’ i l J ava ’ s Str i ng c l ass, … – FORTRAN 90: CHARACTER (LEN = 15) NAME1, NAME2 Limited dynamic length string: C/C++ • Limited dynamic length string: C/C++ – In C-based language, ‘\0’ is used to indicate the end of a string ’ scharacters end of a string s characters • Dynamic length string: SNOBOL4, Perl, JavaScript It requires the overhead of dynamic storage – It requires the overhead of dynamic storage allocation and deallocation but provides maximum flexibility Copyright © 2006 Addison-Wesley. All rights reserved. 1-15 flexibility • Ada supports all three string length options Character String Implementation • Static length: compile-time descriptor • Limited dynamic length: may need a run-time descriptor for length (but not in C/C++) descriptor for length (but not in C/C++) • Dynamic length: need run-time descriptor bdlkdl –Strings can b e store d in a l in k e d l ist, or – Strings can be stored completely in adjacent storage cells Copyright © 2006 Addison-Wesley. All rights reserved. 1-16 Character String Implementation Static string Limited dyn. string Dyn. string Length Max length Current length Length Max . length Current length Address Current length Address Address Compile - time Run - time descriptor Run - time Compile time descriptor for static strings Run time descriptor for limited dynamic strings Run time descriptor for dynamic strings Copyright © 2006 Addison-Wesley. All rights reserved. 1-17 User-Defined Ordinal Types • An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers • Examples of primitive ordinal types in Java – integer integer – char bl – b oo l ean • In many languages, users can define two kinds Copyright © 2006 Addison-Wesley. All rights reserved. 1-18 of ordinal types: enumeration and subrange Enumeration Types • All possible values, which are named constants, are provided in the definition •Ada: type DAYS is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); Design issues • Design issues – Is an enumeration constant allowed to appear in more th t d fi iti d if h i th t f th an one t ype d e fi n iti on, an d if so, h ow i s th e t ype o f an occurrence of that constant checked? Are en meration al es coerced to integer? Copyright © 2006 Addison-Wesley. All rights reserved. 1-19 – Are en u meration v al u es coerced to integer? Example • Pascal - cannot reuse constants; they can be used for array subscripts for loops case used for array subscripts , for loops , case selectors; no input or output; can be compared p ro g ram test ; pg ; type DAYS = (Mon, Tue, Wed, Thu, Fri, Sat, Sun); var day: DAYS; day: DAYS; begin for day := Mon to Sun do writeln('Hello, World'); day := Wed; if d ay <= S a t then … Copyright © 2006 Addison-Wesley. All rights reserved. 1-20 ay a end. [...]... case; end record; d d Copyright © 20 06 Addison-Wesley All rights reserved 1 -68 Ada Union Type Illustrated form: tag (discriminant) color filled rectangle: side1, side2 circle: diameter triangle: leftside, rightside, angle A discriminated union of three shape variables Copyright © 20 06 Addison-Wesley All rights reserved 1 -69 Ada Union Types (cont.) • Reasons Ada union types are safer than Pascal – Variant... dynamic (field names are static) Copyright © 20 06 Addison-Wesley All rights reserved 1 -63 Unions Types • A union is a type whose variables are allowed to store different type values at different times during execution g • Design issues – Should type checking be required? – Should unions be embedded in records? Copyright © 20 06 Addison-Wesley All rights reserved 1 -64 Discriminated vs Free Unions • Fortran,... GROSS-PAY PICTURE IS 999V99 02 NET-PAY PICTURE IS 999V99 … MOVE CORRESPONDING INPUT-RECORD TO OUTPUT-RECORD Copyright © 20 06 Addison-Wesley All rights reserved 1 -60 Implementation of record types A compile-time descriptor for a record Copyright © 20 06 Addison-Wesley All rights reserved 1 -61 Memory layout – an example in Pascal type element = record type element = packed record name: two_chars; name: two_chars;... HOURLY-RATE PIC 99V99 Copyright © 20 06 Addison-Wesley All rights reserved 1- 56 Definition of Records in Ada • In Ada: type DATE is record Month : INTEGER range 1 12; Day : INTEGER range 1 31; Year : INTEGER range 17 76 2100; end record; ; type PERSON is record Name : STRING(1 15); STRING(1 15); Birthday : DATE; Age : INTEGER; Sex : CHARACTER; end record; Copyright © 20 06 Addison-Wesley All rights reserved... 20 06 Addison-Wesley All rights reserved 1-25 Array Types • An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element Copyright © 20 06 Addison-Wesley All rights reserved 1- 26 Array Design Issues • What types are legal for subscripts? • Are subscripting expressions range checked? • How many subscripts... atomic_weight: atomic weight: real; metallic: Boolean metallic: Boolean end; end; Copyright © 20 06 Addison-Wesley All rights reserved 1 -62 Evaluation and Comparison to Arrays • Arrays are used when all the data values have the same type and are processed in the same way • Records are used when the collection of data values is heterogeneous and the different fields are not processed in the same way • Access... Copyright © 20 06 Addison-Wesley All rights reserved 1-35 Number of subscripts • FORTRAN I allowed up to three • FORTRAN 77 allows up to seven • Others - no limit Copyright © 20 06 Addison-Wesley All rights reserved 1- 36 Array Initialization • Some language allow initialization at the time of storage allocation t ll ti – C, C++, Java, C#: int list[] = {4, 5, 7, 83} – FORTRAN 77 INTEGER LIST(3) DATA LIST /0,... union • Type checking of unions require that each union include a type indicator called a tag (or discriminant – Supported by ALGOL 68 , Ada • Pascal provides both discriminated and nonp discriminated unions Copyright © 20 06 Addison-Wesley All rights reserved 1 -65 Pascal Union Types • In Pascal (and Ada), the discriminated union is called a record variant, or variant part of a record type intreal = record... record type intreal = record tagg : Boolean of true : (blint : integer); false : (blreal : real); end; • Problem with Pascal’s design: type checking is ineffective Copyright © 20 06 Addison-Wesley All rights reserved 1 -66 Pascal Union Types (con.) – User can create inconsistent unions (because the tag can be individually assigned) var x : intreal; y : real; (1) x.tagg := true; { it is an integer } (2) x.blint... literal to it @fruit = (); Copyright © 20 06 Addison-Wesley All rights reserved 1-54 Record Types • A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names y • Design issues: – What is the syntactic form of references to the field? – Are elliptical references allowed? ll l f ll d Copyright © 20 06 Addison-Wesley All rights reserved 1-55 . Chapter 6 Data Types ISBN 0-321-33025-0 Chapter 6 Topics • Introduction • Primitive Data Types • Character String Types • User-Defined Ordinal Types • Array Types Array Types • Associative. of primitive data types – Primitive data t yp es: Those not defined in terms yp of other data types • Some primitive data types are merely reflections • Some primitive data types are . Arrays RdT • R ecor d T ypes •Union Types Copyright © 20 06 Addison-Wesley. All rights reserved. 1-2 • Pointer and Reference Types Introduction •A data type defines a collection of data values and a set of

Ngày đăng: 26/01/2015, 09:58

w