(程式语言).ppt

上传人:s****8 文档编号:82755928 上传时间:2023-03-26 格式:PPT 页数:49 大小:448KB
返回 下载 相关 举报
(程式语言).ppt_第1页
第1页 / 共49页
(程式语言).ppt_第2页
第2页 / 共49页
点击查看更多>>
资源描述

《(程式语言).ppt》由会员分享,可在线阅读,更多相关《(程式语言).ppt(49页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Programming Language(程式語言程式語言)Chapter 6Data Types(資料型態)TopicsIntroductionPrimitive data types(基本資料型態)Character string types(符號字串型態)User-defined ordinal types(使用者定義順序型態)Array types(陣列型態)Associative arrays(關聯陣列)Record types(紀錄型態)Union types(聯合型態)Pointer and reference types(指標與參考型態)2IntroductionData ty

2、pe definesA collection of data objects and a set of predefined operations on those objects一群資料物件與再這物件上被預先定義運算集合3Primitive Data TypesAlmost all programming languages provide a set of primitive data typesPrimitive data typesThose not defined in terms of other data types沒有被定義再其他的資料型態中Some primitive dat

3、a types are merely(只是)reflections(反映)of the hardwareInteger typeOthers require(需要)only a little non-hardware4IntegerThe most common privative numeric(數字的)data type is integerJavas signed integer sizesbyte,short,int,longC+and C#include unsigned integer typesUnsigned types are often used for binary da

4、ta5Floating PointModel real numbers,but only as approximations(近似值)Languages for scientific(科學)use support at least two floating-point typesFloat and doubleIEEE Floating-PointStandard 7546IEEE Standard 754 Floating-Point(單精確度)(倍精確度、雙精確度)7Decimal(10進位進位)For business applications(money)Essential(基本)to

5、 COBOLC#offers a decimal data typeStore a fixed number of decimal digits AdvantageAccuracy(準確)DisadvantagesLimited range(有限的範圍)Wastes memory(消耗記憶體)8BooleanSimplest(最簡單)of allRange of values:two elementsTrueFalseCould be implemented as bits,but often as bytesAdvantageReadability9Character(字元字元)Stored

6、 as numeric(數值)codingsMost commonly used coding:ASCIIAn alternative,16-bit coding:UnicodeIncludes characters from most natural languagesOriginally used in JavaC#and JavaScript also support Unicode10Character String Types Values are sequences of charactersDesign issuesShould strings be simply a speci

7、al kind of character array or a primitive type?Should strings have static or dynamic length?11Character String Types OperationsTypical operationsAssignment and copyingComparison(=,etc.)Catenation(串接)Substring reference(子字串參考)Pattern matching(樣式匹配)12Character String Type in Certain LanguagesC and C+N

8、ot primitive(基本)Use char arrays and a library of functions that provide operationsJavaPrimitive via the String class13Character String Length OptionsStatic Javas String classLimited dynamic lengthC and C+In C-based language,a special character is used to indicate the end of a strings characters,rath

9、er than maintaining(維持)the lengthDynamic(no maximum)Perl,JavaScriptAda supports all three string length options14Character String ImplementationStatic lengthCompile-time descriptorLimited dynamic lengthMay need a run-time descriptor for length(but not in C and C+)Dynamic lengthNeed run-time descript

10、or;allocation/de-allocation is the biggest implementation problem15Compile-and Run-Time DescriptorsCompile-time descriptor for static stringsRun-time descriptor for limited dynamic strings16Ordinal TypesAn ordinal type is one in which the range of possible values can be easily associated(組合)with the

11、 set of positive integers(正整數)Examples of primitive ordinal types in Javaintegercharboolean17User-Defined Ordinal TypesIn some language,users can define two kinds of ordinal typeEnumeration(列舉)Subrange(子範圍)18Enumeration TypesAll possible values,which are named(被命名)constants(常數),are provided in the d

12、efinitionC#exampleenum days mon,tue,wed,thu,fri,sat,sun;Design issuesAre enumeration values coerced(強制)to integer?Any other type coerced to an enumeration type?19Subrange TypesAn ordered contiguous(連續)subsequence of an ordinal typeExample:12.18 is a subrange of integer typePascal,AdaAdas designtype

13、Days is(mon,tue,wed,thu,fri,sat,sun);subtype Weekdays is Days range mon.fri;subtype Index is Integer range 1.100;Day1:Days;Day2:Weekday;Day2:=Day1;20Implementation of User-Defined Ordinal TypesEnumeration types are implemented as integersSubrange types are implemented like the parent types with code

14、 inserted(by the compiler)to restrict(限制)assignments to subrange variables21Array TypesAn array is a homogeneous(同質)aggregate(集合體)of data elements(元素)in which an individual(單獨的)element is identified by its position in the aggregate,relative(相對)to the first element一個陣列是一個同質的資料元素集合體,那個獨立的元素(指陣列)表示集合體的

15、位置,相對於第一個元素22Array IndexingIndexing(or subscripting)is a mapping from indices(索引)to elements array_name(index_value_list)an elementIndex SyntaxFORTRAN,PL/I,Ada use parentheses(圓括號)()Most other languages use brackets(方括號)23Arrays Index(Subscript)TypesFORTRAN,C,JavaInteger onlyPascalAny ordinal type(i

16、nteger,Boolean,char,enumeration)AdaInteger or enumeration(includes Boolean and char)C,C+,Perl,and Fortran do not specify range checkingJava,ML,C#specify range checking24Subscript Binding and Array CategoriesStatic array(靜態陣列)Fixed stack-dynamic array(固定堆疊動態陣列)Stack-dynamic array(堆疊動態陣列)Fixed heap-dy

17、namic array(固定堆積動態陣列)Heap-dynamic array(堆積動態陣列)25Subscript Binding and Array CategoriesStatic array(靜態陣列)Subscript ranges are statically bound and storage allocation is static(before run-time)Advantage:efficiency(no dynamic allocation)Fixed stack-dynamic array(固定堆疊動態陣列)Subscript ranges are staticall

18、y bound,but the allocation is done at declaration(宣告)timeAdvantage:space efficiency 26Subscript Binding and Array CategoriesStack-dynamic array(堆疊動態陣列)Subscript ranges are dynamically bound and the storage allocation is dynamic(done at run-time)Advantage:flexibility(the size of an array need not be

19、known until the array is to be used)Fixed heap-dynamic array(固定堆積動態陣列)Similar to fixed stack-dynamicStorage binding is dynamic but fixed after allocation(i.e.,binding is done when requested and storage is allocated from heap,not stack)27Subscript Binding and Array CategoriesHeap-dynamic array(堆積動態陣列

20、)Binding of subscript ranges and storage allocation is dynamic and can change any number of timesAdvantage:flexibility(arrays can grow or shrink during program execution)28Subscript Binding and Array CategoriesC and C+arraysInclude static modifier are staticWithout static modifier are fixed stack-dy

21、namicProvide fixed heap-dynamic arraysmalloc,free,new,deleteJava arraysAll are fixed heap-dynamic arraysC#Fixed heap-dynamic arraysA second array class ArrayList that provides fixed heap-dynamicPerl and JavaScript support heap-dynamic arraysAda arrays can be stack-dynamic29Array Initialization(陣列初始化

22、陣列初始化)Some language allow initialization at the time of storage allocationC,C+,Java,C#exampleint list =4,5,7,83 Character strings in C and C+char name =“freddie”;Arrays of strings in C and C+char*names =“Bob”,“Jake”,“Joe”;Java initialization of String objectsString names=“Bob”,“Jake”,“Joe”;30Impleme

23、ntation of ArraysAccess function maps subscript expressions to an address in the array Access function for single-dimensioned arrays:address(listk)=address(listlower_bound)+(k-lower_bound)*element_size)31Accessing Multi-dimensioned ArraysTwo common waysRow(列)major order(by rows)Used in most language

24、sColumn(行)major order(by columns)Used in Fortran32Locating an Element in a Multi-dimensioned ArrayGeneral formatLocation(aI,j)=address of a row_lb,col_lb+(I-row_lb)*n)+(j-col_lb)*element_size33Compile-Time DescriptorsSingle-dimensioned arrayMulti-dimensional array34Record TypesA record is a possibly

25、 heterogeneous(異種)aggregate(集合體)of data elements in which the individual elements are identified by names一個紀錄可以是異種資料元素的集合體,那個獨立的元素(指紀錄)以名稱表示COBOLC,C+,and C#Records are supported with the struct data type35Implementation of Record TypeOffset address relative to the beginning of the records is associa

26、ted with each field36Unions Types(聯合型態聯合型態)A union is a type that may store different type values at different times during program execution聯合型態可以程式執行時的不同時間點中儲存不同類型的值節省記憶體,所有型態共用同一記憶體區塊取得變數中不同部位的數值37Evaluation of UnionsPotentially unsafe construct(潛在危險)Do not allow type checkingFortran,C,and C+Java

27、 and C#do not support unions38Pointer and Reference TypesA pointer type variable has a range of values that consists of memory addresses and a special value,nil指標型態變數是一個由記憶體位置與特殊值 nil 所構成Provide the power of indirect addressing提供強大的間接定址Provide a way to manage dynamic memory提供管理動態記憶體的方法A pointer can

28、be used to access a location in the area where storage is dynamically created(usually called a heap)指標可以被用來存取區域變數在被動態建立時39Pointer OperationsTwo fundamental operationsAssignment(配置)Dereferencing(解照)Assignment is used to set a pointer variables value to some useful addressDereferencing yields(產生)the v

29、alue stored at the location represented by the pointers valueDereferencing can be explicit or implicitC+uses an explicit operation via*j=*ptrsets j to the value located at ptr40Pointer Assignment IllustratedThe assignment operation j=*ptr41Problems with Pointers Dangling pointers(懸空的指標)(dangerous)A

30、pointer points to a heap-dynamic variable that has been de-allocatedint*arrayPtr1;int*arrayPtr2=new int100;arrayPtr1=arrayPtr2;delete arrayPtr2;42Problems with Pointers Lost heap-dynamic variableAn allocated heap-dynamic variable that is no longer accessible(可使用)to the user program(often called garb

31、age(垃圾)1.Pointer p1 is set to point to a newly created heap-dynamic variable2.Pointer p1 is later set to point to another newly created heap-dynamic variable43Pointers in C and C+Extremely flexible but must be used with care極度的彈性但使用必須小心Pointers can point at any variable regardless of when it was all

32、ocated指標可以指向任何變數,不論他如何被配置Used for dynamic storage management and addressing用來動態儲存管理與定址Pointer arithmetic is possible指標的運算式可行的44Pointers in C and C+Explicit dereferencing and address-of operators明確的解照與位置運算子Domain type need not be fixed(void*)型態可以不固定void*can point to any type and can be type checked(c

33、annot be de-referenced)可用來指向任意型態45Pointer Arithmetic(計算計算)in C and C+float stuff100;float*p;p=stuff;*(p+5)is equivalent to stuff5 and p5*(p+i)is equivalent to stuffi and pi46Reference TypesC+includes a special kind of pointer type called a reference type that is used primarily for formal parametersC

34、+包含一種特別的指標型態稱為參考型態主要被用在正規參數&Java extends C+s reference variables and allows them to replace(取代)pointers entirely(完全)References refer to call instancesC#includes both the references of Java and the pointers of C+47SummaryThe data types of a language are a large part of what determines that languages

35、style and usefulnessThe primitive data types of most imperative languages include numeric,character,and Boolean typesThe user-defined enumeration and subrange types are convenient and add to the readability and reliability of programsArrays and records are included in most languagesPointers are used for addressing flexibility and to control dynamic storage management48The End49

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 生活常识

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com