收藏 分销(赏)

C++.ppt

上传人:a199****6536 文档编号:2046919 上传时间:2024-05-14 格式:PPT 页数:54 大小:584KB
下载 相关 举报
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页
点击查看更多>>
资源描述

1、2、C+programming A simple C+program Types,variables,expressions1精选课件How to write a programoFind the whole steps in the real modeloUse some graph or nature language to describeoRealize with computer language2精选课件workflow3精选课件Pseudo CodeoA first idea:int main()variableswhile(condition)analyze the expre

2、ssionevaluate the expressionprint the result4 44精选课件main Functionint main(void)return 0;cout“Hello,Tom!”;int x=5;int y=8;int z=x+y;coutx“+”y“=“zendl;55精选课件/2.1.cpp#include using namespace std;/*A simple program for demonstrating the basics of a C+project.*It does a good job of demonstrating C+fundam

3、entals,but a*terrible job with the poetry.*/int 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;66精选课件cout expressionscout“hello,world!”;cout 5;cout x;cout x+5;cout x“+5=”x+5;”n”,“t”,“,endl,setw(n)setw(n):#include cout7

4、7精选课件C+Tokens oA token is the smallest element of a C+program that is meaningful to the compiler.oKinds of tokens:nidentifiers,keywords,literals,operators,punctuators,and other separators.oTokens are usually separated by white space.White space can be one or more blanks,horizontal or vertical tabs,n

5、ew lines,form feeds or comments.88精选课件C+Keywords auto const double float int short struct unsigned unsignedbreak break continue elsefor long switch void case sizeoftypedef char do if returnstatic union while,ETC.99精选课件Commenting/*name of program*information of author*function of program*/a sampleint

6、 main()/*this is in the comment this is also in the comment*/.1010精选课件Constants o1,2,3o1.2,4.50o“name”,“your_phonenumber”oture,falseo0 x12o1,A,$,xhh,dddo#define PI 3.141592o#define PRICE 100oconst int pi=3.141592;1111精选课件/2.2.cpp#include using namespace std;int main()int x;int y;x=3;y=4;cout x+y end

7、l;return 0;1212精选课件Variables TypesoBuilt-in typesnBoolean typeobool 1bytenCharacter typesochar 1bytenInteger typesoint 2-4bytes(2)-3276832767oshort(2)&long(4)-21474836482147483647nFloating-point typesodouble 8bytesofloat 4bytes131 byte(8 bits)2 bytes(16 bits)4 bytes(32 bits)8 bytes(64 bits)16 bytes(

8、128 bits)shortcharboolintfloatlongdoublelong double13精选课件Variables LiteralsoBoolean literals:bool t;ntrue,falseoCharacter literals:char c;na,x,4,n,$oInteger literals:int x;n0,1,123,-6,0 x34,0 xa3oFloating point literals:double d;float f;n1.2,13.345,.3,-0.54,1.2e3,.3F,.3FoString literals:string s;nas

9、df,Howdy,all yall!”1414精选课件Variables NamesoChoose meaningful namesnconfuseomtbf,TLA,myw,nbvnShort names can be meaningfulox is a local variableoi is a loop indexnDont use long namesoOk:npartial_sum,element_count,staple_partitionoToo long:nthe_number_of_elementsremaining_free_slots_in_the_symbol_tabl

10、e1515精选课件Not Variables NamesoA name in a C+programnStarts with a letter,contains letters,digits,and underscores(only)ox,number_of_elements,Fourier_transform,z2nNot names:o12x,time$to$market,main lineoNot start names with underscores:_foonNot use keywordsointoifowhile1616精选课件Declaration and initializ

11、ationint a=7;int b=9;char c=a;double x=1.2;string s1=Hello,world;string s2=1.2;179a1.2 13 Hello,world4|1.2b:c:x:s1:s2:7a:|17精选课件Constant variablesconst int i=5;i=6;/error1818精选课件Think about:oint a,b,c=2;oint x,y,z,10;oint m=2;int n=3;olong int sum=0,add;olong hello;ochar a=m;ochar b,c,d;ochar m=65,n

12、=a+1;ofloat a,b,ccc=3.1415;ofloat sum=0.0;odouble f1,f2=1.414e121919精选课件Assignment and incrementint a=7;a=9;a=a+a;a+=2;+a;2079182021a:20精选课件Think aboutoint a=10,b;b=a;ofloat x;int k=300;x=k;ofloat x=3.14;int n;n=x+6;ofloat x=3.14;int n;n=3;ocout x+n;o3.0/9 or (float)3/921精选课件/2.3.cpp A program to il

13、lustrate integer overflow#include using namespace std;/*A program to illustrate 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;2222精选课件A type-safety violation(“imp

14、licit narrowing”)int main()int a=20000;char c=a;int b=c;if(a!=b)cout oops!:a !=b n;elsecout Wow!We have large charactersn;2320000a?c23精选课件C+character set o0 1 2 3 4 5 6 7 8 9 oA 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 oa 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 o_$#()%:;.?*+/&|!=,

15、2424精选课件ASCII code2525精选课件Chars to intsoConvert between a char and its ASCII intnint num=a;nchar let=97;oCycle through the ASCII table with math!nchar bee=a+1;2626精选课件Operatorso+,+o-,-o*o/o%o2727精选课件Arithmetic Assignment OperatorsSymbolExampleEquivalent+=x+=1;x=x+1;*=doub*=2;doub=doub*2;-=n-=5;n=n-5

16、;/=third/=3;third=third/3;%=odd%=2;odd=odd%2;28Think about:int a=12;a+=a-=a*a;cout a;28精选课件2929ExpressionsoBoolean type:bool (true and false)nEquality operators:=(equal),!=(not equal)nLogical operators:&(and),|(or),!(not)nRelational operators:(greater than),=oCharacter type:char (a,7,and)oInteger ty

17、pes:short,int,longn arithmetic operators:+,-,*,/,%oFloating-point types:float,double (12.45 and 1.234e3)narithmetic operators:+,-,*,/29精选课件/2.5.cpp input and output#include using namespace std;/A program to investigate the behavior of the mod(%)operation.int main()int a,b;cout ;cin a;cout ;cin b;cou

18、t a%b =a%b endl;return 0;3030精选课件Think about:o17/3=5?o5/9=0?oint n;n%2+(n+1)%2=?on=2;n+;n=n+1on=2:n+;n-;+n;-n;or=2;m=-n;p=r+;om=10,n=100;op=(n+n,n*n,n-2);op=n+n,n*n,n-2;3131精选课件Think about:oint i=2;o(i+)+(i+)+(i+);couti;oi=2;o(-i)+(-i);couti;oi=2;oi=(i+i+i);couti;oi=2;oi=(i-i);couti;32精选课件/2.6.cpp S

19、tring input#include#include using namespace std;int main()cout first second;string name=first+second;cout Hello,name n;333333精选课件Think about:the output?int x,y;x=10;y=x+;cout y endl;int a,b;a=10;b=+a;cout b 2)falsen(x y)&(y 0)truen(x 0)falsen(x 0)true363636精选课件Think aboutoint a,b,c,d,k=1,h=0;oa=!k&h

20、|h+=k;ob=kh&!h+=!k|1&h;oc=h&k!=!h|!(k&h)=h+55|(h=(k=(h=3)*5)*2-+k);373737精选课件3838Statementsoa=b;odouble d2=2.5;oif(x=2)y=4;owhile(true)cout“hello”;ofor(int i=0;i8;i+)cout 0.0)return(int)(x+1.0);else return(int)x;4040精选课件To be continued41精选课件3、C+programmingControl statements42精选课件Conditions1 if(condi

21、tion)/do thisvoid main()int x;cin x;if(x0)x=-x;cout x;coutsqrt(x);43精选课件/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&y%400)return fals

22、e;return true;44精选课件Conditions2 if(condition)/do thiselse /do thatif(x0)cout-x;else coutb)cout a;else cout b;45精选课件Example void main()char a;couta;if(a=a&a=A&a96)a-=32;else a+=32;coutb)if(bc)coutabc)coutacb;else coutcac)coutbac)coutbca;else coutcb0 y=f(x)=0 x=0 -1 x0)k=1;else if(x=0)k=0;else k=-1;co

23、utx y)?x:y;oGet the max one of a and bint max(int a,int b)int m=a b?a:b;return m;49精选课件if(condition)/do this else if(condition)/do thisconditions450精选课件Switches switch(expression)case i:/do this break;case j:/do that break;default:/do other thing51精选课件Switch example#include using namespace std;int m

24、ain()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;52精选课件/weekdayvoid 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;53精选课件For Loops for(initializations;condition;updates)/do this again and againfor(int i=0;i 10;i+)cout i;54精选课件

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服