




已阅读5页,还剩55页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 Chapter2C SyntaxandSemantics andtheProgramDevelopmentProcess 2 KnowledgeGoals 知识要点 TounderstandhowaC programiscomposedofoneormoresubprograms function Toknowwhatametalanguage 元语 is andhowitisused Tounderstandtheconceptofadatatype 3 2 1TheElementsofC Program theremustbeafunctioncalledmain executionalwaysbeginswiththefirststatementinfunctionmain anyotherfunctionsintheprogramaresubprogramsandarenotexecuteduntiltheyarecalled 2 1 1ConstructtheC program 程序构成 4 ProgramWithSeveralFunctions mainfunction squarefunction cubefunction 5 includeusingnamespacestd intSquare int declaresthesetwointCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n OutputofprogramThesquareof27is729Thecubeof27is19683 include 6 ShortestC Program intmain return0 typeofreturnedvalue nameoffunction saysnoparameters 7 Question TheMain functioncallstheotherfunctions Whocallsthemain functionandtowhomthereturnvalueisdeliveredbythemain function 8 2 1 2BasicConcepts Syntax 语法 Theformalrulesgoverninghowvalidinstructionsarewritteninaprogramminglanguage 指示如何有效的用编程语言撰写指令的正式规则 Semantics 语义 Thesetofrulesthatdeterminesthemeaningofinstructionswritteninaprogramminglanguage 9 Alanguagethatcandefineacomputerprogramminglanguage whichthecomputermayusetoverifythevalidity 合法性 ofasourceprogram Metalanguage 元语 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 10 Anidentifieristhenameassociatedwithadataobject avariableoraconstant orafunction ItisusedtorefertothatdataobjectorfunctioninaC program 标识符是一个和数据对象或函数相关联的名字 C isacase sensitivelanguage 区分大小写 usingmeaningfulidentifiersisagoodprogrammingpractice 用有意义的名字作为标识符是一个好习惯 2 1 3Identifier 标识符 11 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 2 1 3Identifier 标识符 IdentifierSyntaxTemplates 标识符语法模板 Theshadingindicatesapartofthedefinitionthatisoptional 阴影表明该定义的部分是可选的 12 anidentifiermuststartwithaletterorunderscore 字符或下划线 andbefollowedbyzeroormoreletters A Z a z digits 0 9 orunderscoresVALIDage of dogtaxRateY2KPrintHeadingageOfHorseNOTVALID Why age 2000TaxRateAge Of Catint 2 1 3Identifier 标识符 13 AwordthathasspecialmeaninginC itcannotbeusedasprogrammerdefinedidentifier 2 1 4C Reservedword 保留字 14 autobreakcasecharclassconstcontinuedefaultdeleteelseenumexplicitexternfloatforfriendgotoifinlineintlongnewoperatorprivateprotectedpublicregisterreturnshortsignedsizeofstaticstructswitchthistypedefunionunsignedvirtualvoidwhile 2 1 4C Reservedword 保留字 15 Case sensitivelanguage 区分大小写的语言 InC uppercaselettersaredifferentfromlowercaseletters inti intI iandIaredifferentidentifiers 16 2 1 5DataandDatatype 数据和数据类型 Built inType 基本类型或标准类型 Intcharfloatdoublevoid User definedType 用户自定义类型 enumstructunionclass 17 1 charDataType 字符型 Eachcharacterisenclosedinsinglequotes A a 8 Thesequence orpredefinedorderofallthecharacters makesitpossibletocomparetwodataofchartype a b 0 1 18 2 stringDataType 字符串类型 astringisasequenceofcharactersenclosedindoublequotesstringsamplevalues Hello Year2000 1234 theemptystring nullstring containsnocharactersandiswrittenas 19 MoreAboutString stringisnotabuilt in standard typeitisaprogrammer defineddatatypeitisprovidedintheC standardlibrarystringoperationsincludecomparing2stringvalues 串比较 searchingastringforaparticularcharacter 在串中查找特定的字符 joiningonestringtoanother 串的连接 20 Astatementthatassociates 关联 anidentifierwithadataobject afunction oradatatypesothattheprogrammercanrefertothatitembyname intNum floatx1 3 VariableDeclarations 变量声明 21 Avariableisalocationinmemorywhichwecanrefertobyanidentifier andinwhichadatavaluethatcanbechangedisstored 变量是指定类型的存储空间并以指定标识符命名 存储的变量值可以改变 declaringavariablemeansspecifyingbothitsnameanditsdatatype 声明变量意味着指定了它的名字和数据类型 Datatypeconversionmaybeperformed 可以进行类型转换 3 VariableDeclarations 变量声明 22 WhatisaLiteralValue 字面常量 Anyconstantvaluewritteninaprogram A C Programming 168 23 4 ConstantDeclarations 声明符号常量 Anamedconstantisalocationinmemorythatwecanrefertobyanidentifier andinwhichadatavaluethatcannotbechangedisstored constfloatNORMAL TEMP 98 6 constcharBLANK constintVOTING AGE 18 constfloatMAX HOURS 40 0 24 DifferencebetweenLiteralValueandNamedConstant字面常量和符号常量的区别 NamedConstant Alocationinmemory referencedbyanidentifier thatcontainsadatavaluethatcannotbechanged LiteralValue Anyconstantvaluewritteninaprogram 25 ConventionsofNamingIdentifiers 本教材标识符的命名约定 Variables beginwithalowercaseletter andcapitalizeeachsuccessiveword 小写字母开头 lengthInYardsProgrammer definedfunctions beginwithacapitalletter 大写字母开头 butmainfunctionisallinlowercaseletters CalculatePayment Namedconstants capitalizeeveryletteranduseunderscoretoseparatethewords 大写每一个字符 用下划线分割每一个词 BOOK TITLE 26 Astatementthatstoresthevalueofanexpressionintoavariable 5 AssignmentStatement 赋值语句 SyntaxTemplate Variable Expression Anexpressionisavalidarrangementofvariables constants andoperators 27 Youcanassign give avaluetoavariablebyusingtheassignmentoperator VariableDeclarationsstringfirstName charmiddleletter charletter intage ValidAssignmentStatementsfirstName Fido middleletter X letter middleletter age 12 1 Expressiononrightisevaluated 2 TheresultingvalueisstoredinthememorylocationofVariableonleft NOTE Anautomatictypecoercionoccursafterevaluationbutbeforethevalueisstored ifthetypesdifferforExpressionandVariable ExecutionofAssignment 赋值语句的执行 Variable Expression 29 6 StringConcatenation 字符串连接 concatenationisabinaryoperationthatusesthe operatoratleastoneoftheoperandsmustbeastringvariableornamedconstant theotheroperandcanbestringtypeorchartype 30 ConcatenationExample 字符串连接举例 conststringWHEN Tomorrow stringmessage1 stringmessage2 message1 Yesterday message2 and message1 message1 message2 WHEN 31 7 OutputStatements 输出语句 variablecoutispredefinedtodenoteanoutputstreamthatgoestothestandardoutputdevice displayscreen 变量cout已经定义为和标准输出设备 如 显示器 相联系的输出流 theinsertionoperator called putto takes2operands 插入操作符有两个操作数 theleftoperandisastreamexpression suchascout Therightoperandisanexpressionofsimpletypeorastringconstant 左操作数是流表达式 如cout 右操作数是一个简单类型表达式或字符串常量 32 Example cout Theansweris cout 3 4 endl cout Theansweris 3 4 SyntaxTemplates 33 AtthebeginningoftheprogramVariabledeclarationBeforeeachmajorstep functionblocks Wherethestatementsaredifficulttoread 8 AddingCommentstoprogram 为程序添加注释 34 8 AddingCommentstoprogram 为程序添加注释 C commentscomeintwoforms Thefirstisanysequenceofcharactersenclosedbythe pair Thesecondformsbeginswithtwoslashes 1 thisisanexample 2 thisisanexample 35 2 2ProgramConstruction 程序结构 Thesyntaxtemplateforaprogramlookslikethis Program 36 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody 37 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody Example doublemax doublex doubley if x y returnx elsereturny 38 1 FunctionDefinition 函数定义 Afunctiondefinitionconsistsofthefunctionheadinganditsbody Example voidprintmessage cout Howdoyoudo endl 39 2 Block CompoundStatement 块语句 Thisisthesyntaxtemplateforablock Ablockisjustasequenceofzeroormorestatementsenclosedbya pair 40 Statementsyntaxtemplate NullStatementDeclarationAssignmentStatementOutputStatementBlock 41 3 TheC Preprocessor 预处理 Afilewhosenameappearsinan includedirectiveiscalledaheaderfile Headerfilescontainconstant variable datatype andfunctiondeclarationneededbyaprogram include includeusingnamespacestd 42 3 TheC Preprocessor 预处理 Afilewhosenameappearsinan includedirectiveiscalledaheaderfile Headerfilescontainconstant variable datatype andfunctiondeclarationneededbyaprogram include include 43 compiler 3 TheC Preprocessor 预处理 44 4 AnIntroductiontoNamespace 名空间介绍 Theheaderfileiostream infact everystandardheaderfile declaresallitsidentifierstobeinanamespacecalledstd Wecanaccessanidentifierthatishiddeninsideanamespacewithtwooptions 45 4 AnIntroductiontoNamespace 名空间介绍 includeintmain std cout good std endl return0 Firstoptionexample 46 4 AnIntroductiontoNamespace 名空间介绍 Secondoptionexample includeusingnamespacestd intmain cout good endl return0 47 includeusingnamespacestd intSquare int declaresthesetwointCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n 48 includeusingnamespacestd intSquare int declaresthesetwo intCube int value returningfunctionsintmain cout Thesquareof27is Square 27 endl functioncallcout Thecubeof27is Cube 27 endl functioncallreturn0 intSquare intn returnn n intCube intn returnn n n include 49 2 3Moreaboutoutput MovetoanewlinetooutputThemanipulator 操纵符 endlcanmaketheoutputonthescreenmovetothebeginningofanewline OutputadoublequoteinastringIfadoublequoteisoutputinastring itmustbewri
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 更新钢管井施工方案(3篇)
- 道路石材面板施工方案(3篇)
- 严管厚爱系列活动方案策划(3篇)
- 景区许愿活动策划方案模板(3篇)
- 长宁企业年会活动策划方案(3篇)
- 机械模具考试题库及答案
- 教学课件里能用党旗党徽
- 小学学科融合题目及答案
- 改写寻隐者不遇作文14篇
- 文化娱乐行业节目分类表
- 3-1接车及库内作业作业《机车乘务员业务》教学课件
- SL+258-2017水库大坝安全评价导则
- 全国计算机等级考试二级Python复习备考题库(含答案)
- 婚内债务夫妻方承担协议书
- 食品仓储库房温湿度控制
- 部编小学语文四年级上册第8单元省级获奖大单元作业设计
- SMT-快速换线推进报告-.课件11
- 环保配套设施技术改造项目可行性研究报告
- 楼板下加钢梁加固施工方案
- 斜坡脚手架搭设施工方案
- 大学试题(财经商贸)-博弈论笔试(2018-2023年)真题摘选含答案
评论
0/150
提交评论