达内C教程MKV72080CoreCProgrammingnew教案资料

上传人:yulij****0329 文档编号:136935352 上传时间:2020-07-03 格式:PPT 页数:505 大小:1.36MB
返回 下载 相关 举报
达内C教程MKV72080CoreCProgrammingnew教案资料_第1页
第1页 / 共505页
达内C教程MKV72080CoreCProgrammingnew教案资料_第2页
第2页 / 共505页
达内C教程MKV72080CoreCProgrammingnew教案资料_第3页
第3页 / 共505页
达内C教程MKV72080CoreCProgrammingnew教案资料_第4页
第4页 / 共505页
达内C教程MKV72080CoreCProgrammingnew教案资料_第5页
第5页 / 共505页
点击查看更多>>
资源描述

《达内C教程MKV72080CoreCProgrammingnew教案资料》由会员分享,可在线阅读,更多相关《达内C教程MKV72080CoreCProgrammingnew教案资料(505页珍藏版)》请在金锄头文库上搜索。

1、TheC+ProgrammingLanguageChapter1,C+ProgramminginUNIX,课程介绍C+语法基础面向对象程序设计的概念大量的编程实践目标熟练掌握C+语法具有面向对象程序设计的概念与能力能熟练阅读复杂的C+程序源代码能独立的设计与完成面向对象的C+程序,课程内容简介1,C+语言基础保留字变量,常量表达式语句函数程序的结构数据结构与算法数组、指针、引用、结构、链表与栈,课程内容简介2,C+面向对象编程类构造函数与析构函数静态成员与友员函数重载继承与多态I/O流模板异常,程序设计语言介绍2,BlockStructuredLanguageEncapsulationFlex

2、ibledatascopingModularizationObject-OrientedLanguageInheritancePolymorphismAbstractdatatypes,C+程序设计语言,1972,AT/mainfunctionintmain()coutHelloworld!endl;coutThisismyfirstC+program.n;,g的常用参数,-c编译成目标文件.o-o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺省的输出文件名为a.out。-g产生有调试信息的可执行文件-w不产生警告信息-l连接指定的库文件-L指定库文件的路径-i要包

3、含的头文件-I头文件的路径-E显示预处理后的程序文件到屏幕上,可以用-o指定输出到文件-S产生汇编程序如果没有c、E、S就会生成可执行文件,编译hello.cc,%g+-chello.cc%ls,连接hello.o,%g+-ohellohello.o%ls%g+hello.o%ls,运行hello程序,%hello%a.out,C+程序的基本结构1,/*thefirstC+program*/#includeusingnamespacestd;/mainfunctionintmain()coutHelloworld!endl;coutThisismyfirstC+program.n;,C+程序的

4、基本结构2,#include与#includeNamespace:提供了一个全局标识符和全局变量所在的作用域。intmain()注释函数函数的调用cout语句,头文件,#include语句#include与#include,使用#includeusingnamespacestd;少用#include,Main函数,main函数的作用StandardC+main()格式:intmain()return0;/thedefaultreturnvalueis0;,注释,C+的注释,基本的输出语句,cout,练习程序hi.cc,#includeusingnamespacestd;intmain()cou

5、tHiJian!endl;coutHaveaniceday.endl;return0;,练习程序myself.cc,编写一个程序,打印出自己的:姓名性别年龄家庭住址电话号码爱好每一条信息输出为一行,在hi.cc中使用字符串,#includeusingnamespacestd;intmain()charname=John;coutHiname!endl;coutHaveaniceday.endl;return0;,字符与字符串类型,字符,字符串/字符数组charch=A;charstr120=Helloworld!;charstr2=Haveaniceday!;,不同的main()格式,命令行参

6、数%lsl(orls-al)%vihello.cc在程序中使用命令行参数intmain(intargc,char*argv),命令行参数程序cmdline.cc,#includeusingnamespacestd;intmain(intargc,char*argv)for(inti=0;iargc;i+)coutargvi=argviendl;,使用命令行参数的hi.cc,使用命令行参数,重新编写练习程序cmdline.cc%hiJohn%hiLisa%hiG.Bush,基本输入语句,cin语句使用cin语句的hi.cc重写hi.cc程序,不带命令行参数程序自动提示用户输入字符串来获得姓名与年

7、龄,练习程序age.cc,#includeusingnamespacestd;intmain()unsignedintage;charname50;coutname;coutage;coutyournameis:nameendl;coutYouwereage-2yearsoldtwoyearsago.n;,条件语句,if语句ifelse语句不同if的等价与不等价形式charch;cinch;if(ch=y)/notethedifference:if(ch=y)coutgoodendl;elsecouttryagain.endl;,练习程序grade.cc,Thisisyourassignmen

8、t.,Qintmain()inti;i=5;couti=iendl;i=8;couti=iendl;return0;,另一个使用变量的例子程序,#includeusingnamespacestd;intmain()i=5;/seewhathappenscouti=iendl;i=8;couti=iendl;return0;inti;,变量与变量的size,变量都有类型变量在内存中的大小inti;doubled;coutsizeofiissizeof(i)endl;coutsizeofintissizeof(int)endl;coutsizeofdissizeof(d)endl;coutsize

9、ofdoubleissizeof(double)endl;,程序size.cc,编写一个程序,打印出所有C+基本类型的大小#includeusingnamespacestd;intmain()coutsizeofcharis:sizeof(char)endl;coutsizeofunsignedcharis:sizeof(unsignedchar)endl;coutsizeofsignedcharis:sizeof(signedchar)endl;coutsizeofintis:sizeof(int)endl;coutsizeofunsignedintis:sizeof(unsignedint

10、)endl;coutsizeofsignedintis:sizeof(signedint)endl;coutsizeofshortintis:sizeof(shortint)endl;,变量的取值范围,变量的类型与值的范围常用类型的取值范围,常量,常量与常量的数据类型constdoublepi=3.14,const限定符,限定一个常量或者函数方便编译器来检测非法的修改操作,运算符,运算符+,-,*,/,%,+,-,=,=,结合性优先级:seetable3-1inpage35intherecommendedbook1.,运算符的使用,if(demo=2)与if(demo=2)if(2=demo)

11、/左值与右值。if(demo!=2)与if(demo=!2),运算符的优先级,inta=8,b=4,c=5;cout(a%b?b:c);couta%b?b:c;,变量的赋值,赋值表达式变量的初始化一次声明多个变量声明并初始化变量,无符号类型的值,无符号整数类型的回绕unsignedshortintsnum;snum=65535;coutsnum=snumendl;snum=snum+1;coutsnum=snumendl;,有符号类型的值,有符号整数类型的回绕intinum=2147483647;coutinum=inumendl;inum=inum+1;coutinum=inumendl;,

12、常用类型的取值范围,常用类型的取值范围inti=65535;intj=65535;couti*j/9endl;,,为表示如下数据,应该使用什么类型的变量:年龄姓名工资电话号码身份证号码西三环到东三环的距离,练习程序bin.cc,#includeusingnamespacestd;intmain()inta=10;couta;unsignedintr;intk;unsignedintj;charstr33;memset(str,0,33);str32=0;r=a;inti=32;,练习程序bin.cc,doj=r;r=r/2;k=j-r*2;if(k)str-i=1;elsestr-i=0;wh

13、ile(r!=0);coutstrendl;,枚举类型,enumcolorRED,GREEN,BLUE,WHITE,BLACK;colortvColor=GREEN;Enumconstantvariabledoesnotneedmemoryallocation.ThedefaultvalueinEnumis:0,1,2,3,4.Specifytheelementvalues:enumcolorRED=100,GREEN=200,BLUE,WHITE=300,BLACK=400,表达式,左值与右值floata;a=5/2;+a;求值顺序特殊表达式:(?:)逗号表达式:inta,b,c;a=1,b

14、=a=2,c=b+3;,表达式的求值顺序,求值顺序副作用,表达式的左值与右值,inta,b,c,d;inte=(a=1,b=a,c=a+b,d=c+5);(a=1,b=a,c=a+b,d=c+5)=8;e=(a=0,b=a+5,b+2);(a=0,b=a+5,b+2)=9;/ohps,youmaygetproblem.,练习程序comma.cc,#includeusingnamespacestd;intmain()inta,b,c,d;inte=(a=1,b=a,c=a+b,d=c+5);(a=1,b=a,c=a+b,d=c+5)=8;coutd=dendl;,程序语句,控制语句表达式语句空语

15、句语句块,控制语句,条件判断语句if()if()else循环控制语句while()dowhile();for()多路选择语句switch()case:,循环语句,for语句dowhile语句while语句break语句continue语句,分支语句,switch语句switch(ss)case1:break;case2:break;default:break;,循环语句程序例子chengFa.cc,编写一个程序,打印出乘法口诀表分别使用for语句,dowhile语句,while语句来实现,Theoutputis:1x1=11x2=2,2x2=41x3=3,2x3=6,3x3=91x4=4,2x4=8,3x4=12,4x4=161x5=5,2x5=10,3x5=15,4x5=20,5x5=251x6=6,2x6=12,3x6=18,4x6=24,5x6=30,6x6=361x7=7,2x7=14,3x7=21,4x7=28,5x7=35,6x7=42,7x7=491x8=8,2x8=16,3x8=24,4x8=32,5x8=40,6x8=48,7x8=56,8x8=641x9=9,2x9=18,3x9=27,4x9=36,5x9=45,6x9=54,7x9=63,8x9=72,9x9=81,练习程序ye

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

当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号