C++课件(精华版).ppt

上传人:牧羊曲112 文档编号:6154038 上传时间:2023-09-30 格式:PPT 页数:54 大小:341KB
返回 下载 相关 举报
C++课件(精华版).ppt_第1页
第1页 / 共54页
C++课件(精华版).ppt_第2页
第2页 / 共54页
C++课件(精华版).ppt_第3页
第3页 / 共54页
C++课件(精华版).ppt_第4页
第4页 / 共54页
C++课件(精华版).ppt_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《C++课件(精华版).ppt》由会员分享,可在线阅读,更多相关《C++课件(精华版).ppt(54页珍藏版)》请在三一办公上搜索。

1、2、C+programming,A simple C+program Types,variables,expressions,How to write a program,Find the whole steps in the real modelUse some graph or nature language to describeRealize with computer language,workflow,Pseudo Code,A first idea:int main()variableswhile(condition)analyze the expressionevaluate

2、the expressionprint the result,4,main Function,int main(void)return 0;,int x=5;int y=8;int z=x+y;coutx“+”y“=“zendl;,5,#include using namespace std;/*A simple program for demonstrating the basics of a C+project.*It does a good job of demonstrating C+fundamentals,but a*terrible job with the poetry.*/i

3、nt main()cout Dont you just feel like a louse;cout endl;cout To learn that your new theorem was proved by Gauss?;cout endl;return 0;,6,cout,cout,7,C+Tokens,A token is the smallest element of a C+program that is meaningful to the compiler.Kinds of tokens:identifiers,keywords,literals,operators,punctu

4、ators,and other separators.Tokens are usually separated by white space.White space can be one or more blanks,horizontal or vertical tabs,new lines,form feeds or comments.,8,C+Keywords,auto const double float int short struct unsigned unsignedbreak break continue elsefor long switch void case sizeoft

5、ypedef char do if returnstatic union while,ETC.,9,Commenting,/*name of program*information of author*function of program*/a sampleint main()/*this is in the comment this is also in the comment*/.,10,Constants,1,2,31.2,4.50“name”,“your_phonenumber”ture,false0 x121,A,$,xhh,ddd#define PI 3.141592#defin

6、e PRICE 100const int pi=3.141592;,11,/2.2.cpp,#include using namespace std;int main()int x;int y;x=3;y=4;cout x+y endl;return 0;,12,Variables Types,Built-in typesBoolean typebool 1byteCharacter typeschar 1byteInteger typesint 2-4bytes(2)-3276832767short(2)&long(4)-21474836482147483647Floating-point

7、typesdouble 8bytesfloat 4bytes,13,Variables Literals,Boolean literals:bool t;true,falseCharacter literals:char c;a,x,4,n,$Integer literals:int x;0,1,123,-6,0 x34,0 xa3Floating point literals:double d;float f;1.2,13.345,.3,-0.54,1.2e3,.3F,.3FString literals:string s;asdf,Howdy,all yall!”,14,Variables

8、 Names,Choose meaningful namesconfusemtbf,TLA,myw,nbvShort names can be meaningfulx is a local variablei is a loop indexDont use long namesOk:partial_sum,element_count,staple_partitionToo long:the_number_of_elementsremaining_free_slots_in_the_symbol_table,15,Not Variables Names,A name in a C+program

9、Starts with a letter,contains letters,digits,and underscores(only)x,number_of_elements,Fourier_transform,z2Not names:12x,time$to$market,main lineNot start names with underscores:_fooNot use keywordsintifwhile,16,Declaration and initialization,int a=7;int b=9;char c=a;double x=1.2;string s1=Hello,wor

10、ld;string s2=1.2;,17,9,a,1.2,13 Hello,world,4|1.2,b:,c:,x:,s1:,s2:,7,a:,|,Constant variables,const int i=5;i=6;/error,18,Think about:,int a,b,c=2;int x,y,z,10;int m=2;int n=3;long int sum=0,add;long hello;char a=m;char b,c,d;char m=65,n=a+1;float a,b,ccc=3.1415;float sum=0.0;double f1,f2=1.414e12,19

11、,Assignment and increment,int a=7;a=9;a=a+a;a+=2;+a;,20,7,9,18,20,21,a:,Think about,int a=10,b;b=a;float x;int k=300;x=k;float x=3.14;int n;n=x+6;float x=3.14;int n;n=3;cout x+n;3.0/9 or(float)3/9,/2.3.cpp A program to illustrate integer overflow,#include using namespace std;/*A program to illustrat

12、e what happens when large integers*are multiplied together.*/int main()int million=1000000;int trillion=million*million;cout According to this computer,million squared is trillion.endl;,22,A type-safety violation(“implicit narrowing”),int main()int a=20000;char c=a;int b=c;if(a!=b)cout oops!:a!=b n;

13、elsecout Wow!We have large charactersn;,23,C+character set,0 1 2 3 4 5 6 7 8 9 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 _$#()%:;.?*+/&|!=,24,ASCII code,25,Chars to ints,Convert between a char and its ASCII intint num=a;char let=97;Cycle

14、through the ASCII table with math!char bee=a+1;,26,Operators,+,+-,-*/%,27,Arithmetic Assignment Operators,28,Think about:int a=12;a+=a-=a*a;cout a;,29,Expressions,Boolean type:bool(true and false)Equality operators:=(equal),!=(not equal)Logical operators:&(and),|(or),!(not)Relational operators:(grea

15、ter than),=Character type:char(a,7,and)Integer types:short,int,long arithmetic operators:+,-,*,/,%Floating-point types:float,double(12.45 and 1.234e3)arithmetic operators:+,-,*,/,/2.5.cpp input and output,#include using namespace std;/A program to investigate the behavior of the mod(%)operation.int

16、main()int a,b;cout;cin a;cout;cin b;cout a%b=a%b endl;return 0;,30,Think about:,17/3=5?5/9=0?int n;n%2+(n+1)%2=?n=2;n+;n=n+1n=2:n+;n-;+n;-n;r=2;m=-n;p=r+;m=10,n=100;p=(n+n,n*n,n-2);p=n+n,n*n,n-2;,31,Think about:,int i=2;(i+)+(i+)+(i+);couti;i=2;(-i)+(-i);couti;i=2;i=(i+i+i);couti;i=2;i=(i-i);couti;,

17、/2.6.cpp String input,#include#include using namespace std;int main()cout first second;string name=first+second;cout Hello,name n;,33,Think about:the output?,int x,y;x=10;y=x+;cout y endl;int a,b;a=10;b=+a;cout b endl;,34,Boolean expressions,a b a&btrue true true true false false false true false fa

18、lse false false,a b a|b true true true true false true false true true false false false,a!a true false false true,35,Think about,assume x=6 and y=2!(x 2)false(x y)&(y 0)true(x 0)false(x 0)true,36,Think about,int a,b,c,d,k=1,h=0;a=!k,37,38,Statements,a=b;double d2=2.5;if(x=2)y=4;while(true)cout“hell

19、o”;for(int i=0;i8;i+)cout i;int average=(length+width)/2;return x;,Floor,int floor(float x)return(int)x;,39,Ceiling,int ceiling(float x)if(x(int)x 0.0)return(int)(x+1.0);else return(int)x;,40,To be continued,3、C+programming,Control statements,Conditions1,if(condition)/do this,void main()int x;cin x;

20、if(x0)x=-x;cout x;coutsqrt(x);,/bool leapyear(int y),/*any year divisible by 4 except centenary years not divisible by 400*/bool leapyear(int y)/any year divisible by 4 except centenary years not divisible by 400if(y%4)return false;if(y%100=0,Conditions2,if(condition)/do thiselse/do that,if(x0)cout-

21、x;else coutx;,void main()int a=5,b=8;if(ab)cout a;else cout b;,Example,void main()char a;couta;if(a=a,Conditions3,if(condition)/do thiselse if(condition)/do thatelse/do other thing,void main()int a=3,b=17,c=5;if(ab)if(bc)coutc)coutc)coutc)coutbca;else coutcba;,1 x0 y=f(x)=0 x=0-1 x0)k=1;else if(x=0)

22、k=0;else k=-1;coutx k;,Example,(xy)?x:y;,Get the max one of a and bint max(int a,int b)int m=a b?a:b;return m;,if(condition)/do this else if(condition)/do this,conditions4,Switches,switch(expression)case i:/do this break;case j:/do that break;default:/do other thing,Switch example,#include using nam

23、espace std;int main()int x=6;switch(x)case 1:cout x is 1n;break;case 2:case 3:cout x is 2 or 3;break;default:cout x is not 1,2,or 3;return 0;,/weekday,void weekday(int d)switch(d)case 7:cout Sunday;break;case 1:cout Monday;break;case 2:cout Tuesday;break;case 3:cout Wednesday;break;case 4:cout Thursday;break;case 5:cout Friday;break;case 6:cout Saturday;break;return;,For Loops,for(initializations;condition;updates)/do this again and again,for(int i=0;i 10;i+)cout i;,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号