《C精华学习教程.pptx》由会员分享,可在线阅读,更多相关《C精华学习教程.pptx(54页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、How to write a programFind the whole steps in the real modelUse some graph or nature language to describeRealize with computer language第1页/共54页workflow第2页/共54页Pseudo CodeA first idea:int main()variableswhile(condition)analyze the expressionevaluate the expressionprint the result3 3第3页/共54页main Funct
2、ionint main(void)return 0;cout“Hello,Tom!”;int x=5;int y=8;int z=x+y;coutx“+”y“=“zendl;4第4页/共54页/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+fundamentals,but a*terrible job with the poetry.*/int main()cout
3、Dont you just feel like a louse;cout endl;cout To learn that your new theorem was proved by Gauss?;cout endl;return 0;5第5页/共54页cout expressionscout“hello,world!”;cout 5;cout x;cout x+5;cout x“+5=”x+5;”n”,“t”,“,endl,setw(n)setw(n):#include cout6第6页/共54页C+Tokens A token is the smallest element of a C+
4、program that is meaningful to the compiler.Kinds of tokens:identifiers,keywords,literals,operators,punctuators,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.7第7页/共54页C+Keywords auto
5、const double float int short struct unsigned unsignedbreak break continue elsefor long switch void case sizeoftypedef char do if returnstatic union while,ETC.8第8页/共54页Commenting/*name of program*information of author*function of program*/a sampleint main()/*this is in the comment this is also in the
6、 comment*/.9第9页/共54页Constants 1,2,31.2,4.50“name”,“your_phonenumber”ture,false0 x121,A,$,xhh,ddd#define PI 3.141592#define PRICE 100const int pi=3.141592;10第10页/共54页/2.2.cpp#include using namespace std;int main()int x;int y;x=3;y=4;cout x+y endl;return 0;11第11页/共54页Variables TypesBuilt-in typesBoole
7、an typebool 1byteCharacter typeschar 1byteInteger typesint 2-4bytes(2)-3276832767short(2)&long(4)-21474836482147483647Floating-point typesdouble 8bytesfloat 4bytes121 byte(8 bits)2 bytes(16 bits)4 bytes(32 bits)8 bytes(64 bits)16 bytes(128 bits)shortcharboolintfloatlongdoublelong double第12页/共54页Vari
8、ables LiteralsBoolean 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!”13第13页/共54页Variables NamesChoose meaningful namescon
9、fusemtbf,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_table14第14页/共54页Not Variables NamesA name in a C+programStarts with a letter,contai
10、ns 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 keywordsintifwhile15第15页/共54页Declaration and initializationint a=7;int b=9;char c=a;double x=1.2;string s1=Hello,world;string s2=1.2;169a
11、1.2 13 Hello,world4|1.2b:c:x:s1:s2:7a:|第16页/共54页Constant variablesconst int i=5;i=6;/error17第17页/共54页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.414e1218第18页/共54页Assignment an
12、d incrementint a=7;a=9;a=a+a;a+=2;+a;1979182021a:第19页/共54页Think aboutint 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第20页/共54页/2.3.cpp A program to illustrate integer overflow#include using namespace std;/*A program to illustrate what
13、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;21第21页/共54页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
14、;elsecout Wow!We have large charactersn;2220000a?c第22页/共54页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 _$#()%:;.?*+/&|!=,23第23页/共54页ASCII code24第24页/共54页Chars to intsConvert between a char and its ASCII i
15、ntint num=a;char let=97;Cycle through the ASCII table with math!char bee=a+1;25第25页/共54页Operators+,+-,-*/%26第26页/共54页Arithmetic Assignment OperatorsSymbolExampleEquivalent+=x+=1;x=x+1;*=doub*=2;doub=doub*2;-=n-=5;n=n-5;/=third/=3;third=third/3;%=odd%=2;odd=odd%2;27Think about:int a=12;a+=a-=a*a;cout
16、 a;第27页/共54页2828ExpressionsBoolean type:bool (true and false)Equality operators:=(equal),!=(not equal)Logical operators:&(and),|(or),!(not)Relational operators:(greater than),=Character type:char (a,7,and)Integer types:short,int,long arithmetic operators:+,-,*,/,%Floating-point types:float,double (1
17、2.45 and 1.234e3)arithmetic operators:+,-,*,/第28页/共54页/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;cout a%b =a%b endl;return 0;29第29页/共54页Think about:17/3=5?5/9=0?int n;n%2+(n+1)%2=?n=2;n
18、+;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;30第30页/共54页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;第31页/共54页/2.6.cpp String input#include#include using namespace std;int main()cout first second;string name=first
19、+second;cout Hello,name n;3232第32页/共54页Think about:the output?int x,y;x=10;y=x+;cout y endl;int a,b;a=10;b=+a;cout b 2)false(x y)&(y 0)true(x 0)false(x 0)true3535第35页/共54页Think aboutint a,b,c,d,k=1,h=0;a=!k&h|h+=k;b=kh&!h+=!k|1&h;c=h&k!=!h|!(k&h)=h+55|(h=(k=(h=3)*5)*2-+k);3636第36页/共54页3737Statements
20、a=b;double d2=2.5;if(x=2)y=4;while(true)cout“hello”;for(int i=0;i8;i+)cout 0.0)return(int)(x+1.0);else return(int)x;39第39页/共54页To be continued第40页/共54页3、C+programmingControl statements第41页/共54页Conditions1 if(condition)/do thisvoid main()int x;cin x;if(x0)x=-x;cout x;coutsqrt(x);第42页/共54页/bool leapye
21、ar(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 false;return true;第43页/共54页Conditions2 if(condition)/do thiselse /do thatif(x0)cout-x;el
22、se coutb)cout a;else cout b;第44页/共54页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;coutx y)?x:y;Get the max one of a and bint max(int a,int b)int m=a b?a:b;return
23、m;第48页/共54页if(condition)/do this else if(condition)/do thisconditions4第49页/共54页Switches switch(expression)case i:/do this break;case j:/do that break;default:/do other thing第50页/共54页Switch example#include using namespace std;int main()int x=6;switch(x)case 1:cout x is 1n;break;case 2:case 3:cout x i
24、s 2 or 3;break;default:cout x is not 1,2,or 3;return 0;第51页/共54页/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;第52页/共54页For Loops for(initializations;condition;updates)/do this again and againfor(int i=0;i 10;i+)cout i;第53页/共54页谢谢您的观看!第54页/共54页